.NET Uri将%3a转换回冒号

时间:2017-01-18 14:09:30

标签: c# .net http url

我需要使用HttpWebRequest类来发布数据。我的目标uri看起来像http://www.foo.com/index.jsp?value=http://www.foo.com。在Urlencode之后,它变为http://www.foo.com/index.jsp?value=http%3A%2F%2Fwww.foo.com。但是当我使用

WebRequest.Create(@"http://www.foo.com/index.jsp?value=http%3A%2F%2Fwww.foo.com") as HttpWebRequest;

它实际上发送http://www.foo.com/index.jsp?value=http:%2F%2Fwww.foo.com(它将%3A转换回冒号),然后出错了。那么我该如何发送http://www.foo.com/index.jsp?value=http%3A%2F%2Fwww.foo.com而不是http://www.foo.com/index.jsp?value=http:%2F%2Fwww.foo.com

更新: 示例代码:

const string queryParam = @"http://www.foo.com";
UriBuilder bulider = new UriBuilder
{
    Scheme = "http",
    Host = @"www.foo.com",
    Path = "index.jsp",
    Query = HttpUtility.UrlEncode(queryParam)
};

Uri uri = bulider.Uri;
Console.WriteLine(uri.ToString());

输出结果:

http://www.foo.com/index.jsp?http:%2f%2fwww.foo.com

更新

不同的.NET版本有不同的行为。在.NET 4.5.2中,输出为http://www.foo.com/index.jsp?http:%2f%2fwww.foo.com,在.NET 4.0中,输出为http://www.foo.com/index.jsp?http://www.foo.com

0 个答案:

没有答案
相关问题