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

前端 未结 2 843
半阙折子戏
半阙折子戏 2021-01-24 04:35

I have spent a whole day trying to resolve this. I have a custom webserver and requests to it from Chrome or POSTman ReST client work fine. As soon a s I use webclient or httpw

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-24 05:18

    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
    {
        /// 
        /// Returns a  object for the specified resource.
        /// 
        /// A  that identifies the resource to request.
        /// 
        /// A new  object for the specified resource.
        /// 
        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);
            }
    

提交回复
热议问题