C# HttpWebRequest-如何转义URI中的竖条(管道)字符

C# HttpWebRequest-如何转义URI中的竖条(管道)字符,c#,httpwebrequest,encode,C#,Httpwebrequest,Encode,在我的代码中: HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://example.org/1|2|3"); 在Fiddler中,我看到URI总是编码为: 当我想发送 我尝试使用HttpUtility,但没有效果。您可以这样做: Uri uri = new Uri(Uri.EscapeUriString("http://example.org/1|2|3")); HttpWebRequest myReque

在我的代码中:

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://example.org/1|2|3");
在Fiddler中,我看到URI总是编码为: 当我想发送

我尝试使用HttpUtility,但没有效果。

您可以这样做:

Uri uri = new Uri(Uri.EscapeUriString("http://example.org/1|2|3")); 
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri);
更新:同时尝试启用IRI解析。将此添加到app.config/web.config:

<uri>
    <idn enabled="All" />
    <iriParsing enabled="true" />
</uri>
如果您使用低于4的.NET版本,还必须添加以下内容才能使uri部分有效:

<configSections>
  <section name="uri" type="System.Configuration.UriSection, System,
                      Version=2.0.0.0, Culture=neutral,
                      PublicKeyToken=b77a5c561934e089" />
</configSections>

它将对每个非标准字符进行编码,要读取服务器/接收器中的url,您可以使用HttpUtility.UrlDecode,然后执行任何操作。

请参阅更新的答案。启用IRI解析从技术上扩展URI中允许的字符集,使其成为IRI。这应该允许使用未编码的管道符号。这也不起作用,尝试同时针对4.5和4.0。我搜索得越多,就越觉得这在.NET中不可能。从我正在读取的管道来看,|在URL中不是非法字符,所以我应该能够发送它