10

HttpWebRequest 解码我的编码 URL 时出现问题。

var requestUrl = "https://www.google.com/webmasters/tools/feeds/http%3A%2F%2Fwww%2example%2Ecom%2F/crawlissues/";   
var request = (HttpWebRequest)WebRequest.Create(requestUrl);

查看结束请求 URL 时变为:

https://www.google.com/webmasters/tools/feeds/http://www.example.com//crawlissues/

这当然会返回 400 Bad 请求。我猜这与URI类而不是HttpWebRequest有关。我该如何阻止这种情况发生?

4

5 回答 5

5

这是 Uri 类的一个烦人的“安全特性”。如果您使用的是 4.0 或更高版本,您可以在配置文件中将其关闭;否则,您将不得不求助于反射

于 2012-10-23T17:54:54.900 回答
1

我认为您不能请求该网址。

它不会%2F在查询参数中解码。因此,如果编码数据在查询参数中,它将起作用:

requestUrl = "https://google.com/tools?feeds=http%3A%2F%2Fwww%2example%2Ecom%2F/crawlissues/";   
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
于 2012-10-15T03:54:46.890 回答
1

有一种更简单的方法

var request=(HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(requestUrl));
request.Headers.Add("Content-Transfer-Encoding","binary");

对我来说就像一个魅力

于 2017-03-24T08:16:05.423 回答
0

不确定,但可能是HttpServerUtility.UrlEncode方法会有所帮助。

更新。或者,您可以使用 WebClient 类。

于 2010-02-18T09:58:41.017 回答
0

尝试将请求方法从 POST 更改为 GET

于 2010-11-30T07:09:55.277 回答