System.Net.WebException:服务器提交了协议违规

时间:2016-10-07 14:41:20

标签: c# system.net.webexception

我有以下代码进行调用,然后返回xml:

public MainWindow(IModuleCatalog moduleCatalog, IModuleManager moduleManager)
    {
        InitializeComponent();

        this.DataContext = this;
        this.ConfirmationRequest = new InteractionRequest<IConfirmation>();

        this.moduleCatalog = moduleCatalog;
        this.moduleManager = moduleManager;

        UpdateModulesList();

        this.Loaded += (s, e) => this.chWindow.Show();
    }

大部分时间(并非总是)我得到

  

System.Net.WebException:服务器提交了协议违规。

     

Section = ResponseStatusLine at System.Net.HttpWebRequest.GetResponse()

抛出异常。

我已直接在private string Send(string url) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } } } catch (WebException ex) { var _status = ex.Status.ToString(); if (ex.Status == WebExceptionStatus.ProtocolError) { _status = ((HttpWebResponse)ex.Response).StatusCode.ToString(); } Trace.WriteLine(_status.ToString()); } return "error"; } 部分将此添加到我的App.config中:

<configuration>

但我仍然得到错误。

2 个答案:

答案 0 :(得分:2)

request.KeepAlive = false;

http://www.webmonkeys.org.uk/2012/09/c-the-server-committed-a-protocol-violation-sectionresponsestatusline/#comment-60

此外,如果您尝试通过此问题,则粘贴的useUnsafeHeaderParsing配置值将其设置为false,如果您将其设置为true。

答案 1 :(得分:0)

newInstance