自己写了个程序,利用post获取某网站的信息,当进行到httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()时会提示“服务器提交了协议冲突. Section=ResponseStatusLine”(有时出有时不出,好像跟电脑或网络有关)
百度了下,几个人都说要改webclient等来提交请求,但我仍想利用httpWebRequest.GetResponse(),不想改其他代码了.
关于“”错误,百度上很多人都是说使用下面的代码解决,但是我用了都不成功,不知道是为什么。我估计不是好的解决方法。
WinForm下的app.config文件中添加: <system.net> <settings> <httpWebRequest useUnsafeHeaderParsing="true" /> </settings> </system.net>
好了,下面是我自己解决的办法:
这里可以使用webclient,只要有cookies功能就可以了,我们可以加上cookies功能,如下代码。这样Section=ResponseStatusLine问题也可以解决了。二全其美。
public class WebClientPlus : WebClient { private CookieContainer m_container = new CookieContainer(); public CookieContainer CookieContainer { get { return m_container; } set { m_container = value; } } [System.Security.SecuritySafeCritical]//重要 public WebClientPlus() : base() { } protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = m_container; } return request; } }