【问题标题】:HttpWebRequest - how can I escape the vertical bar (pipe) character in the URIHttpWebRequest - 如何转义 URI 中的竖线(管道)字符
【发布时间】:2015-09-10 19:05:00
【问题描述】:

在我的代码中:

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://example.org/1|2|3");

在 Fiddler 中,我看到 URI 始终编码为: “http://example.com/1%7C2%7C3”而我想发送“http://example.org/1|2|3

我尝试使用 HttpUtility 无济于事。

【问题讨论】:

    标签: c# httpwebrequest encode


    【解决方案1】:

    你可以这样做:

    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-section 有效:

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

    【讨论】:

    • 查看更新的答案。启用 IRI 解析会扩展 URI 中允许的字符集(技术上使其成为 IRI)。这应该允许未编码的管道符号。
    • 那也不起作用,尝试同时针对 4.5 和 4.0。我搜索的越多,我就越认为这在 .Net 中可能是不可能的
    【解决方案2】:

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

    【讨论】:

    • 从我读到的管道中,“|”不是 URL 中的非法字符,所以我应该可以发送它
    猜你喜欢
    • 2023-04-05
    • 2012-01-31
    • 2016-07-24
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2018-07-24
    相关资源
    最近更新 更多