English 中文(简体)
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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签