System.Net.WebExeption CR必须后跟LF

时间:2016-09-05 20:35:23

标签: c# web httpresponse

我正在使用此代码从URL中读取内容:

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(urlAddress);
        req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
        using (var resp = req.GetResponse())
        {
            var html = new StreamReader(resp.GetResponseStream()).ReadToEnd();
            return html;
        }

+在app.config中我有:

  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
  </system.net>

在我的电脑中 - 我编写程序的程序很好用,但是当我创建exe文件并尝试在我的网络服务器上运行时,我收到错误:

The server commited a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

如何解决此错误。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用此

if (res.Headers.AllKeys.Contains("Vary"))
    {
        res.Headers["Vary"] = "Accept";
    }
    else
    {
        res.Headers.Add("Vary", "Accept");
    }