質問

いに対応したアプリケーションを送へPOSTで要求をVBフォーラムソフトウェアおよびログうな設定でクッキーとか付けたやつでも強い

ユーザーがログインを作りたい変数を作成しパスを行います。

c: empfolder\date\username

問題は、一部のユーザ名は投げ"不正な文字を"例外です。たとえば、私のユーザー名でした mas|fenix い例外がスローされ..

Path.Combine( _      
  Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), _
  DateTime.Now.ToString("ddMMyyhhmm") + "-" + form1.username)

いくら文字列、フォルダユーザー名を作成してFTPサーバー.この私の第二の質問です。ばんをフォルダのサーバーたいのですが、"違法字"。まだこのため、サーバはLinux上で動作する、い場合はLinuxを受け入れできます。

編集:この文字列をURLエンコードします。はない..こちらは何をしたいと思い:

old username = mas|fenix
new username = mas%xxfenix

場%xxは、アスキー値またはその他の価値が容易に特定の文字です。

役に立ちましたか?

解決

編集:この答えは出ています。見 Siarhei Kuchukの答え下 よりよい修正

UrlEncodingいうことです。とのスレッドセーフで、C#、まだ利用 HttpUtility, として挙げられる。

また正規表現の不正な文字を置き換え、これがかなり複雑していても状態機械(switch...の場合、例えば)に置き換えると、正しい文字です。以降 UrlEncode このフロントでは、より簡単です。

としてのLinuxとwindowsある文字でも可Linuxではないので、Windowsには、なると、フォルダ名を返すこともできますが読することにより、Urlの文字列を使用 UrlDecode, での往復に変わります。

他のヒント

って実験の目的である。純をURLエンコーディングです。おそらく以下のテーブルに役立つとして出力からテストアプリに書いた):

Unencoded UrlEncoded UrlEncodedUnicode UrlPathEncoded EscapedDataString EscapedUriString HtmlEncoded HtmlAttributeEncoded HexEscaped
A         A          A                 A              A                 A                A           A                    %41
B         B          B                 B              B                 B                B           B                    %42

a         a          a                 a              a                 a                a           a                    %61
b         b          b                 b              b                 b                b           b                    %62

0         0          0                 0              0                 0                0           0                    %30
1         1          1                 1              1                 1                1           1                    %31

[space]   +          +                 %20            %20               %20              [space]     [space]              %20
!         !          !                 !              !                 !                !           !                    %21
"         %22        %22               "              %22               %22              "      "               %22
#         %23        %23               #              %23               #                #           #                    %23
$         %24        %24               $              %24               $                $           $                    %24
%         %25        %25               %              %25               %25              %           %                    %25
&         %26        %26               &              %26               &                &       &                %26
'         %27        %27               '              '                 '                '       '                %27
(         (          (                 (              (                 (                (           (                    %28
)         )          )                 )              )                 )                )           )                    %29
*         *          *                 *              %2A               *                *           *                    %2A
+         %2b        %2b               +              %2B               +                +           +                    %2B
,         %2c        %2c               ,              %2C               ,                ,           ,                    %2C
-         -          -                 -              -                 -                -           -                    %2D
.         .          .                 .              .                 .                .           .                    %2E
/         %2f        %2f               /              %2F               /                /           /                    %2F
:         %3a        %3a               :              %3A               :                :           :                    %3A
;         %3b        %3b               ;              %3B               ;                ;           ;                    %3B
<         %3c        %3c               <              %3C               %3C              &lt;        &lt;                 %3C
=         %3d        %3d               =              %3D               =                =           =                    %3D
>         %3e        %3e               >              %3E               %3E              &gt;        >                    %3E
?         %3f        %3f               ?              %3F               ?                ?           ?                    %3F
@         %40        %40               @              %40               @                @           @                    %40
[         %5b        %5b               [              %5B               %5B              [           [                    %5B
\         %5c        %5c               \              %5C               %5C              \           \                    %5C
]         %5d        %5d               ]              %5D               %5D              ]           ]                    %5D
^         %5e        %5e               ^              %5E               %5E              ^           ^                    %5E
_         _          _                 _              _                 _                _           _                    %5F
`         %60        %60               `              %60               %60              `           `                    %60
{         %7b        %7b               {              %7B               %7B              {           {                    %7B
|         %7c        %7c               |              %7C               %7C              |           |                    %7C
}         %7d        %7d               }              %7D               %7D              }           }                    %7D
~         %7e        %7e               ~              ~                 ~                ~           ~                    %7E

Ā         %c4%80     %u0100            %c4%80         %C4%80            %C4%80           Ā           Ā                    [OoR]
ā         %c4%81     %u0101            %c4%81         %C4%81            %C4%81           ā           ā                    [OoR]
Ē         %c4%92     %u0112            %c4%92         %C4%92            %C4%92           Ē           Ē                    [OoR]
ē         %c4%93     %u0113            %c4%93         %C4%93            %C4%93           ē           ē                    [OoR]
Ī         %c4%aa     %u012a            %c4%aa         %C4%AA            %C4%AA           Ī           Ī                    [OoR]
ī         %c4%ab     %u012b            %c4%ab         %C4%AB            %C4%AB           ī           ī                    [OoR]
Ō         %c5%8c     %u014c            %c5%8c         %C5%8C            %C5%8C           Ō           Ō                    [OoR]
ō         %c5%8d     %u014d            %c5%8d         %C5%8D            %C5%8D           ō           ō                    [OoR]
Ū         %c5%aa     %u016a            %c5%aa         %C5%AA            %C5%AA           Ū           Ū                    [OoR]
ū         %c5%ab     %u016b            %c5%ab         %C5%AB            %C5%AB           ū           ū                    [OoR]

列の表示エンコーディングとしており

  • UrlEncoded: HttpUtility.UrlEncode

  • UrlEncodedUnicode: HttpUtility.UrlEncodeUnicode

  • UrlPathEncoded: HttpUtility.UrlPathEncode

  • EscapedDataString: Uri.EscapeDataString

  • EscapedUriString: Uri.EscapeUriString

  • HtmlEncoded: HttpUtility.HtmlEncode

  • HtmlAttributeEncoded: HttpUtility.HtmlAttributeEncode

  • HexEscaped: Uri.HexEscape

注記:

  1. HexEscape でのみお取扱いを第255文字です。そのためにする ArgumentOutOfRange 例外にラテン拡張文字(例えばĀ).

  2. このテーブル発生した。NET4.0(ヴBotelhoコメント下回るという結果についてのエンコードです。純4.5若干異なります).

編集:

また第二のテーブルのエンコーディングします。純4.5.この答え: https://stackoverflow.com/a/21771206/216440

編集2:

から人を評価しているようであるこれらのテーブル、たいと思いまは、ソースコードを生成するテーブルにできますので遊びます。単純ではC#のコンソールアプリケーションすることができます。NET4.0 4.5:

using System;
using System.Collections.Generic;
using System.Text;
// Need to add a Reference to the System.Web assembly.
using System.Web;

namespace UriEncodingDEMO2
{
    class Program
    {
        static void Main(string[] args)
        {
            EncodeStrings();

            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.Read();
        }

        public static void EncodeStrings()
        {
            string stringToEncode = "ABCD" + "abcd"
            + "0123" + " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" + "ĀāĒēĪīŌōŪū";

            // Need to set the console encoding to display non-ASCII characters correctly (eg the 
            //  Latin A-Extended characters such as ĀāĒē...).
            Console.OutputEncoding = Encoding.UTF8;

            // Will also need to set the console font (in the console Properties dialog) to a font 
            //  that displays the extended character set correctly.
            // The following fonts all display the extended characters correctly:
            //  Consolas
            //  DejaVu Sana Mono
            //  Lucida Console

            // Also, in the console Properties, set the Screen Buffer Size and the Window Size 
            //  Width properties to at least 140 characters, to display the full width of the 
            //  table that is generated.

            Dictionary<string, Func<string, string>> columnDetails =
                new Dictionary<string, Func<string, string>>();
            columnDetails.Add("Unencoded", (unencodedString => unencodedString));
            columnDetails.Add("UrlEncoded",
                (unencodedString => HttpUtility.UrlEncode(unencodedString)));
            columnDetails.Add("UrlEncodedUnicode",
                (unencodedString => HttpUtility.UrlEncodeUnicode(unencodedString)));
            columnDetails.Add("UrlPathEncoded",
                (unencodedString => HttpUtility.UrlPathEncode(unencodedString)));
            columnDetails.Add("EscapedDataString",
                (unencodedString => Uri.EscapeDataString(unencodedString)));
            columnDetails.Add("EscapedUriString",
                (unencodedString => Uri.EscapeUriString(unencodedString)));
            columnDetails.Add("HtmlEncoded",
                (unencodedString => HttpUtility.HtmlEncode(unencodedString)));
            columnDetails.Add("HtmlAttributeEncoded",
                (unencodedString => HttpUtility.HtmlAttributeEncode(unencodedString)));
            columnDetails.Add("HexEscaped",
                (unencodedString
                    =>
                    {
                        // Uri.HexEscape can only handle the first 255 characters so for the 
                        //  Latin A-Extended characters, such as A, it will throw an 
                        //  ArgumentOutOfRange exception.                       
                        try
                        {
                            return Uri.HexEscape(unencodedString.ToCharArray()[0]);
                        }
                        catch
                        {
                            return "[OoR]";
                        }
                    }));

            char[] charactersToEncode = stringToEncode.ToCharArray();
            string[] stringCharactersToEncode = Array.ConvertAll<char, string>(charactersToEncode,
                (character => character.ToString()));
            DisplayCharacterTable<string>(stringCharactersToEncode, columnDetails);
        }

        private static void DisplayCharacterTable<TUnencoded>(TUnencoded[] unencodedArray,
            Dictionary<string, Func<TUnencoded, string>> mappings)
        {
            foreach (string key in mappings.Keys)
            {
                Console.Write(key.Replace(" ", "[space]") + " ");
            }
            Console.WriteLine();

            foreach (TUnencoded unencodedObject in unencodedArray)
            {
                string stringCharToEncode = unencodedObject.ToString();
                foreach (string columnHeader in mappings.Keys)
                {
                    int columnWidth = columnHeader.Length + 1;
                    Func<TUnencoded, string> encoder = mappings[columnHeader];
                    string encodedString = encoder(unencodedObject);

                    // ASSUMPTION: Column header will always be wider than encoded string.
                    Console.Write(encodedString.Replace(" ", "[space]").PadRight(columnWidth));
                }
                Console.WriteLine();
            }
        }
    }
}

きエンコードのユーザー名またはその他のURLの一部が無効とさせていただきます。※URLエンコーディングのURLでブルが発生しやすいという問題がからんようになります:

string url = HttpUtility.UrlEncode("http://www.google.com/search?q=Example");

利益をもたらす

http%3a%2f%2fwww.google.com%2fsearch%3fq%3dExample

これは明らかになったはずなのに、うまくいかない。ようにいくつかの異なるエンコードのみでの値をキーと値のペアのクエリー文字列のようになります:

string url = "http://www.google.com/search?q=" + HttpUtility.UrlEncode("Example");

も役立っています。また、 teedyay 挙げられ、残っている必要があり違法ファイル名の文字を削除または他のファイルシステムのないようにします。

以降 .NET Framework4.5.純標準1.0 のものを使用できる WebUtility.UrlEncode.優選択肢:

  1. です。NET Framework4.5+,.純Core1.0+,.純標準1.0+,UWP10.0+およびすべてのXamarinプラットフォームです。 HttpUtility, をご用意。NETフレームワーク先(.NET Framework1.1+)となり他のプラットフォームにあるこの(.ネコ2.0+,.純標準2.0+)だけでは不可UWP参照 関連する質問).

  2. ます。NETフレームワークで に存在す System.dll, うにするのに必要な追加参考文献とは異なり、 HttpUtility.

  3. 適切にエスケープ文字Url, とは異なり、 Uri.EscapeUriString 参照 コメントをdrweb86の回答).

  4. な制限文字列の長さ, とは異なり、 Uri.EscapeDataString 参照 関連する質問、そのままでは使用できるポストを要求です。

より良い方法は、

Uri.EscapeUriString

には参照ファです。net4.

ヴBotelho委員は、テーブルのエンコーディングでは、従来発生する不正確ます。純4.5以降のエンコーディングと若干の修正がありましたが。純4.0 4.5.そこで再生を表します。純4.5:

Unencoded UrlEncoded UrlEncodedUnicode UrlPathEncoded WebUtilityUrlEncoded EscapedDataString EscapedUriString HtmlEncoded HtmlAttributeEncoded WebUtilityHtmlEncoded HexEscaped
A         A          A                 A              A                    A                 A                A           A                    A                     %41
B         B          B                 B              B                    B                 B                B           B                    B                     %42

a         a          a                 a              a                    a                 a                a           a                    a                     %61
b         b          b                 b              b                    b                 b                b           b                    b                     %62

0         0          0                 0              0                    0                 0                0           0                    0                     %30
1         1          1                 1              1                    1                 1                1           1                    1                     %31

[space]   +          +                 %20            +                    %20               %20              [space]     [space]              [space]               %20
!         !          !                 !              !                    %21               !                !           !                    !                     %21
"         %22        %22               "              %22                  %22               %22              &quot;      &quot;               &quot;                %22
#         %23        %23               #              %23                  %23               #                #           #                    #                     %23
$         %24        %24               $              %24                  %24               $                $           $                    $                     %24
%         %25        %25               %              %25                  %25               %25              %           %                    %                     %25
&         %26        %26               &              %26                  %26               &                &amp;       &amp;                &amp;                 %26
'         %27        %27               '              %27                  %27               '                &#39;       &#39;                &#39;                 %27
(         (          (                 (              (                    %28               (                (           (                    (                     %28
)         )          )                 )              )                    %29               )                )           )                    )                     %29
*         *          *                 *              *                    %2A               *                *           *                    *                     %2A
+         %2b        %2b               +              %2B                  %2B               +                +           +                    +                     %2B
,         %2c        %2c               ,              %2C                  %2C               ,                ,           ,                    ,                     %2C
-         -          -                 -              -                    -                 -                -           -                    -                     %2D
.         .          .                 .              .                    .                 .                .           .                    .                     %2E
/         %2f        %2f               /              %2F                  %2F               /                /           /                    /                     %2F
:         %3a        %3a               :              %3A                  %3A               :                :           :                    :                     %3A
;         %3b        %3b               ;              %3B                  %3B               ;                ;           ;                    ;                     %3B
<         %3c        %3c               <              %3C                  %3C               %3C              &lt;        &lt;                 &lt;                  %3C
=         %3d        %3d               =              %3D                  %3D               =                =           =                    =                     %3D
>         %3e        %3e               >              %3E                  %3E               %3E              &gt;        >                    &gt;                  %3E
?         %3f        %3f               ?              %3F                  %3F               ?                ?           ?                    ?                     %3F
@         %40        %40               @              %40                  %40               @                @           @                    @                     %40
[         %5b        %5b               [              %5B                  %5B               [                [           [                    [                     %5B
\         %5c        %5c               \              %5C                  %5C               %5C              \           \                    \                     %5C
]         %5d        %5d               ]              %5D                  %5D               ]                ]           ]                    ]                     %5D
^         %5e        %5e               ^              %5E                  %5E               %5E              ^           ^                    ^                     %5E
_         _          _                 _              _                    _                 _                _           _                    _                     %5F
`         %60        %60               `              %60                  %60               %60              `           `                    `                     %60
{         %7b        %7b               {              %7B                  %7B               %7B              {           {                    {                     %7B
|         %7c        %7c               |              %7C                  %7C               %7C              |           |                    |                     %7C
}         %7d        %7d               }              %7D                  %7D               %7D              }           }                    }                     %7D
~         %7e        %7e               ~              %7E                  ~                 ~                ~           ~                    ~                     %7E

Ā         %c4%80     %u0100            %c4%80         %C4%80               %C4%80            %C4%80           Ā           Ā                    Ā                     [OoR]
ā         %c4%81     %u0101            %c4%81         %C4%81               %C4%81            %C4%81           ā           ā                    ā                     [OoR]
Ē         %c4%92     %u0112            %c4%92         %C4%92               %C4%92            %C4%92           Ē           Ē                    Ē                     [OoR]
ē         %c4%93     %u0113            %c4%93         %C4%93               %C4%93            %C4%93           ē           ē                    ē                     [OoR]
Ī         %c4%aa     %u012a            %c4%aa         %C4%AA               %C4%AA            %C4%AA           Ī           Ī                    Ī                     [OoR]
ī         %c4%ab     %u012b            %c4%ab         %C4%AB               %C4%AB            %C4%AB           ī           ī                    ī                     [OoR]
Ō         %c5%8c     %u014c            %c5%8c         %C5%8C               %C5%8C            %C5%8C           Ō           Ō                    Ō                     [OoR]
ō         %c5%8d     %u014d            %c5%8d         %C5%8D               %C5%8D            %C5%8D           ō           ō                    ō                     [OoR]
Ū         %c5%aa     %u016a            %c5%aa         %C5%AA               %C5%AA            %C5%AA           Ū           Ū                    Ū                     [OoR]
ū         %c5%ab     %u016b            %c5%ab         %C5%AB               %C5%AB            %C5%AB           ū           ū                    ū                     [OoR]

列の表示エンコーディングとしており

  • UrlEncoded: HttpUtility.UrlEncode
  • UrlEncodedUnicode: HttpUtility.UrlEncodeUnicode
  • UrlPathEncoded: HttpUtility.UrlPathEncode
  • WebUtilityUrlEncoded: WebUtility.UrlEncode
  • EscapedDataString: Uri.EscapeDataString
  • EscapedUriString: Uri.EscapeUriString
  • HtmlEncoded: HttpUtility.HtmlEncode
  • HtmlAttributeEncoded: HttpUtility.HtmlAttributeEncode
  • WebUtilityHtmlEncoded: WebUtility.HtmlEncode
  • HexEscaped: Uri.HexEscape

注記:

  1. HexEscapeでのみお取扱いを第255文字です。そのためにするArgumentOutOfRangeの例外をラテン拡張文字(例えばĀ).

  2. このテーブル発生した。純4.5(回答 https://stackoverflow.com/a/11236038/216440 のエンコーディングに関す。純4.0。

編集:

  1. 結果としての葛藤の答えを加えた新しいWebUtility UrlEncodeとHtmlEncode方法が紹介された。純4.5.

Urlエンコーディングが容易です。います。用途:

System.Web.HttpUtility.UrlEncode(string url)

まだ復号化され、フォルダの名前だまだ必要性を排除できない文字の使用のフォルダの名前(*,?, /など)

できない場合はサービスを開始しました。.Webでの変更をプロジェクトを設定します。目標の枠組みは".NET Framework4"の代わりに".NET4Client Profile"

きます。純の実施 UrlEncode を遵守しないRFC3986.

  1. 一部の文字エンコードしませんがきます。の !()* 文字は、RFC部2.2として貸切文字が必要で符号化されていない。当期純に失敗したエンコードされます。

  2. 一部の文字エンコードされていないでください。の .-_ 文字には載っていないRFC部2.2として貸切文字にするべきではないで符号化されていない。純誤って符号化されます。

  3. RFCを指定することを、一貫した実装を使用上の場合HEXDIG、.純生する場合がHEXDIG.

書いはC#の方法url符号化して記号:

    /// <summary>
    /// !#$345Hf} → %21%23%24%33%34%35%48%66%7D
    /// </summary>
    public static string UrlEncodeExtended( string value )
    {
        char[] chars = value.ToCharArray();
        StringBuilder encodedValue = new StringBuilder();
        foreach (char c in chars)
        {
            encodedValue.Append( "%" + ( (int)c ).ToString( "X2" ) );
        }
        return encodedValue.ToString();
    }

理想的にはこれらのうクラスという"FileNaming"もう名前の変更をコード化"をFileNameEncode".注意:これらの設計にはなっておりませんので取扱いをフルパスでのフォルダやファイル名です。理想い分割("/")フルパスで最初にチェックしていきます。や明らかではなくユきは"%"の文字の字は、Windows、すべてのアルゴリダのホテルを表/が読事実です。Decode()は同じものスイッチに交換(Uriです。HexEscape(s[0]),s)"逃げ"の文字。

public static List<string> urlEncodedCharacters = new List<string>
{
  "/", "\\", "<", ">", ":", "\"", "|", "?", "%" //and others, but not *
};
//Since this is a superset of urlEncodedCharacters, we won't be able to only use UrlEncode() - instead we'll use HexEncode
public static List<string> specialCharactersNotAllowedInWindows = new List<string>
{
  "/", "\\", "<", ">", ":", "\"", "|", "?", "*" //windows dissallowed character set
};

    public static string Encode(string fileName)
    {
        //CheckForFullPath(fileName); // optional: make sure it's not a path?
        List<string> charactersToChange = new List<string>(specialCharactersNotAllowedInWindows);
        charactersToChange.AddRange(urlEncodedCharacters.
            Where(x => !urlEncodedCharacters.Union(specialCharactersNotAllowedInWindows).Contains(x)));   // add any non duplicates (%)

        charactersToChange.ForEach(s => fileName = fileName.Replace(s, Uri.HexEscape(s[0])));   // "?" => "%3f"

        return fileName;
    }

ん@シモン-tewsiに置するテーブルがあります。

まる@団ヘルベルトの回答 またはエンコードの値が一般的です。

分割は、paramsのパラメータの分割('&','=');表現をまず分割によるグ'='で奇要素は、すべての値を符号化することを以下に示します。

public static void EncodeQueryString(ref string queryString)
{
    var array=queryString.Split('&','=');
    for (int i = 0; i < array.Length; i++) {
        string part=array[i];
        if(i%2==1)
        {               
            part=System.Web.HttpUtility.UrlEncode(array[i]);
            queryString=queryString.Replace(array[i],part);
        }
    }
}

と思う方はこちらかsidetrackedのUrlEncodeメッセージ。URLEncodingは ない 何をしたい--したいエンコードもない作品としてファイル名を対象システム。

したいと仮定して一般--お気軽に不正な文字に複数のシステム(MacOS、Windows、LinuxおよびUnix)、ユニオれている文字のセットに。

としての脱出、HexEscapeは(置換のキャラクタ%XX).変換の各文字がUTF-8バイトを符号化すべ>128したい場合は支援システムなunicodeで扱います。がその他の方法を使用するなどバックスラッシュ"\"またはHTMLエンコード""".を構築することが可能です。すべてのシステムが'エンコードのuncompatible文字です。上記のシステムの許可を再現する独自の名称ですが、そのような交換の字がスペース作品。

同じ接していますが、同一の使用

Uriです。EscapeDataString

で符号化しも必要なOAuthされているとは思いませんが、エンコードのものOAuthによる禁止符号化し、符号化のスペースとして%20せ+(もの誓いスペック)参照:RFC3986.AFAIKこのURIの仕様

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top