C#WebRequest.getResponse():400错误的请求 [英] C# WebRequest.getResponse(): 400 Bad Request

查看:706
本文介绍了C#WebRequest.getResponse():400错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用System.Web从服务器下载文件。
它确实有效,但是某些链接给我带来了麻烦。的链接看起来像这样:

I am trying to download a file from a server using System.Web. It actually works, but some links give me trouble. The links look like this:

http://cdn.somesite.com/r1KH3Z%2FaMY6kLQ9Y4nVxYtlfrcewvKO9HLTCUBjU8IBAYnA3vzE1LGrkqMrR9Nh3jTMVFZzC7mxMBeNK5uY3nx5K0MjUaegM3crVpFNGk6a6TW6NJ3hnlvFuaugE65SQ4yM5754BM%2BLagqYvwvLAhG3DKU9SGUI54UAq3dwMDU%2BMl9lUO18hJF3OtzKiQfrC/the_file.ext

代码基本上看起来像这样:

The code looks basically like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
WebResponse response = request.getResponse();

getResponse()总是抛出异常(错误400错误请求)。
但是,我知道该链接有效,因为我可以使用Firefox下载文件而不会出现问题。

getResponse() always throws an exception (Error 400 Bad Request). However, I know the link works because I can download the file with Firefox without problems.

我也尝试使用Uri.UnescapeDataString(link ),但该链接甚至无法在Firefox中使用。

I also tried to decode the link with Uri.UnescapeDataString(link), but that link wont even work in Firefox.

其他链接可以通过这种方式正常工作..只是这些不起作用。

Other links work perfectly fine this way.. just these won't work.

好吧,我发现了使用Wireshark的东西:

Okay, i found something out using wireshark:

如果我使用Firefox打开链接,则发送此消息:

If i open the link using Firefox, this is sent:

&ME3@"dM*PNyAo PA:]GET /r1KH3Z%2FaMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs4QgbudzcrJivrAaOTYkEnozqmdoSCCY8yb1i22YtEAV/epd_outpost_12adb.flv HTTP/1.1
Host: cdn.somesite.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive

我认为只有第一行就是问题所在,因为WebRequest.Create(link)会解码网址:

I think only the first line is the problem, because WebRequest.Create(link) decodes the url:

&MEz.@!dM/nP9@~P>.GET /r1KH3Z/aMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs6Mmh1EsQQ4vJVYUwtbLBDNx9AwCHlWDfzfSWIHzaaIo/epd_outpost_12adb.flv HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
Host: cdn.somesite.com

(%2F替换为/)

我发现Uri类将网址自动:
Uri uri = new Uri(link); //未解码链接
Debug.WriteLine(uri.ToString()); //链接在这里解码。

I found out that the Uri class decodes the url automatically: Uri uri = new Uri(link); //link is not decoded Debug.WriteLine(uri.ToString()); //link is decoded here.

如何防止这种情况?

在此先感谢您的帮助。

Thanks in advance for your help.

推荐答案

默认情况下, Uri 类不允许转义< URI中的code> / 字符(%2f )(即使在我阅读RFC 3986 )。

By default, the Uri class will not allow an escaped / character (%2f) in a URI (even though this appears to be legal in my reading of RFC 3986).

Uri uri = new Uri("http://example.com/embed%2fded");
Console.WriteLine(uri.AbsoluteUri); // prints: http://example.com/embed/ded

(注意:不要使用Uri.ToString 来打印URI。)

(Note: don't use Uri.ToString to print URIs.)

根据在Microsoft Connect上针对此问题的错误报告,此行为是设计使然,但是您可以通过将以下内容添加到app.config或web.config文件中来解决此问题:

According to the bug report for this issue on Microsoft Connect, this behaviour is by design, but you can work around it by adding the following to your app.config or web.config file:

<uri>
  <schemeSettings>
    <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" />
  </schemeSettings>
</uri>

(因为 WebRequest.Create(string)仅委托给 WebRequest.Create(Uri),无论调用哪种方法,都需要使用此替代方法。)

(Since WebRequest.Create(string) just delegates to WebRequest.Create(Uri), you would need to use this workaround no matter which method you call.)

这篇关于C#WebRequest.getResponse():400错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆