即使在使用useUnsafeHeaderParsing之后,服务器也提交了协议违规错误

时间:2015-10-05 13:59:15

标签: asp.net-mvc-4 c#-4.0

我试图从MVC中的外部URL获取XML重新发布。

这是我的代码:

        Uri uri = new Uri("http://101.10.3.111:8600/out");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

        request.ServicePoint.Expect100Continue = false;
        request.KeepAlive = false;

        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // as an xml: deserialise into your own object or parse as you wish
                var responseXml = XDocument.Load(response.GetResponseStream());
                Console.WriteLine(responseXml.ToString());
            }
        }
        catch (Exception ex)
        {

        }

并且它给出了这个错误:

{"The server committed a protocol violation. Section=ResponseStatusLine"}

如果我将此添加到web.config:

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

错误改为:

{"The underlying connection was closed: The connection was closed unexpectedly."}

此外,如果我将网址调用(复制\粘贴)到浏览器,它会给我输出文件。

0 个答案:

没有答案