开发者

Help with C# HttpWebRequest URI losing its encoding

Having a problem with HttpWebRequest decoding my encoded URL.

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

When looking at end request URL is becomes:

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

Which 开发者_开发问答of course returns a 400 Bad request. I am guessing it is something todo with the URI class rather than HttpWebRequest. How do I stop this from happening?


This is an annoying "security feature" of the Uri class. If you're using 4.0 or later, you can turn it off in your configuration file; otherwise, you'll have to resort to reflection.


I don't think you can request that url.

It won't decode %2F in a query parameter. So, it would work if the encoded data was in a query parameter:

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


There is a much simpler way to this

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

worked like a charm for me


Not sure but may be HttpServerUtility.UrlEncode method will help.

Upd. Alternatively you may use WebClient class.


Try to change the Request method from POST to GET

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜