The server committed a protocol violation. Section=ResponseStatusLine in c#

时间秒杀一切 提交于 2019-12-02 05:06:51

I had the same issue and I solved it using the following method. I created a custom web client that overrides the GetWebRequestMethod.

  class CustomWebClient : WebClient
{
    /// <summary>
    /// Returns a <see cref="T:System.Net.WebRequest" /> object for the specified resource.
    /// </summary>
    /// <param name="address">A <see cref="T:System.Uri" /> that identifies the resource to request.</param>
    /// <returns>
    /// A new <see cref="T:System.Net.WebRequest" /> object for the specified resource.
    /// </returns>
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).KeepAlive = false;
        }
        return request;
    }
}

Then I made the request in the normal way like this

using (CustomWebClient client = new CustomWebClient())
        {
            client.Headers[HttpRequestHeader.Authorization] = "Basic " + base64String;
            responseData = client.DownloadData(baseUri);
        }

I struggled with that issue for a long time, and eventually the problem was a custom header that I added to IIS... I copied the value from somewhere and it contained {cr}:

Open IIS Manager -> go to the web site -> on content view, choose "Response Headers".

check the custom headers on IIS, remove headers if they are not needed, and also see if a header has a malformed value.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!