如何在代码中设置useUnsafeHeaderParsing
我收到以下异常:
服务器违反了协议。部分 = ResponseHeader 详细信息 = CR 后面必须跟有 LF
从这个问题:
HttpWebRequestError:服务器违反了协议。 Section=ResponseHeader Detail=CR 后必须跟 LF
我知道我需要将 useUnsafeHeaderParsing 设置为 True。
这是我的代码:
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse(); //exception is thrown here
useUnsafeHeaderParsing 是 HttpWebRequestElement 类的属性。
如何将其集成到上面的代码中?
I am getting the following exception:
The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF
From this question:
I understand that I need to set useUnsafeHeaderParsing to True.
Here is my code:
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse(); //exception is thrown here
useUnsafeHeaderParsing is a property of HttpWebRequestElement class.
How do I integrate it in the above code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在 web.config 的
部分中进行设置,如下所示:如果出于某种原因,您不想从配置中执行此操作,您可以通过以编程方式设置配置设置,从代码中完成此操作。请参阅此页面 举个例子。
You need to set this is in your web.config, inside
<system.net>
section, like this:If, for some reason, you do not want to do it from your config, you could do it from code by prgrammatically setting your config settings. See this page for an example.
正如 Edwin 指出的,您需要在 web.config 或 app.config 文件中设置 useUnsafeHeaderParsing 属性。如果您确实想在运行时动态更改该值,则必须求助于反射,因为该值隐藏在 System.Net.Configuration.SettingsSectionInternal 实例中并且无法公开访问。
这是一个代码示例(基于找到的信息
As Edwin has pointed out you need to set the useUnsafeHeaderParsing attribute in your web.config or app.config file. If you really want to change the value dynamically at runtime, then you'll have to resort to reflection as the value is buried in an instance of
System.Net.Configuration.SettingsSectionInternal
and not publicly accessible.Here is a code example (based on the info found here) that does the trick:
如果您不想使用反射,您可以尝试以下代码(System.Configuration.dll参考):
If you did't want use Reflection, you can try this code (System.Configuration.dll reference):