WebException (Status: Protocol Error)

21,319

Solution 1

Your server is returning HTTP 1201 which is not a standard status code.

WebClient will fail with an exception when facing a non-successful status code (or an unrecognized one, in your case).

I encorauge you to use the new HttpClient class if you can:

public async Task<string> SendWebRequest(string requestUrl)
{
    using (HttpClient client = new HttpClient())
    using (HttpResponseMessage response = await client.GetAsync(requestUrl))
         return await response.Content.ReadAsStringAsync();
}

If you have to do it synchronously:

public string SendWebRequest(string requestUrl)
{
    using (HttpClient client = new HttpClient())
    using (HttpResponseMessage response = client.GetAsync(requestUrl).GetAwaiter().GetResult())
         return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}

Solution 2

Try changing the headers of your webclient. It seems that your response is not compatible with the header types. I would suggest that you add an Accept header to your client client.Headers.Add("Accept", "application/json"); assuming that you are waiting for Json.

Share:
21,319
Amit Joshi
Author by

Amit Joshi

I am working as Dot Net developer (actually multiple Microsoft technologies including SQL Server, IIS etc.) since 2004. My domain is medical imaging and healthcare.

Updated on September 28, 2020

Comments

  • Amit Joshi
    Amit Joshi over 3 years

    I am calling a url in C# using WebClient class. Following is the code: -

        public string SendWebRequest(string requestUrl)
        {
            using (var client = new WebClient())
            {
                string responseText = client.DownloadString(requestUrl);
                return responseText;
            } 
        }
    

    This code fails with following Exception details: -

    System.Net.WebException: The remote server returned an error: (1201).
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
    
    Exception Status: ProtocolError
    

    URL hit the server properly. Actions expected (update database) on server happen properly. Server sends response properly. WebClient is not processing the response.

    I also tried using HttpWebRequest class without success.

    Initially, similar issue was there while request. It was resolved when I modified my app.config with following: -

        <settings>
            <httpWebRequest useUnsafeHeaderParsing = "true"/>
        </settings>
    

    I cannot post URL on this forum and anyway it is not accessible outside network.

    If I copy same URL in browser address bar, it just works fine and returns expected response.

    What could be the problem with windows application then?

    Edit 1

    I implemented suggestions from answer. I also implemented suggestion in accepted answer for this question. Now, my function looks as follows: -

        public string SendWebRequest(string requestUrl)
        {
            using (var client = new WebClient())
            {
                client.Headers.Add("Accept", "text/plain");
                client.Headers.Add("Accept-Language", "en-US");
                client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
                client.Headers["Content-Type"] = "text/plain;charset=UTF-8";
                string responseText = client.DownloadString(requestUrl);
                return responseText;
            } 
        }
    

    It still does not solve the problem. Response is now blank string ("") instead of "Success". This is not fault from server which is confirmed.

    If I remove configuration in app.config, it throws other exception.

    System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
    
    • Matias Cicero
      Matias Cicero over 7 years
      You need to send the appropiate request headers (Accept for instance), and any other header that the server may need (such as User-Agent)
    • Amit Joshi
      Amit Joshi over 7 years
      problem is not on server. problem is on client side when it receives response, it is not able to parse it IMO.
    • TheDude
      TheDude over 7 years
  • Sameh Awad
    Sameh Awad over 7 years
    if you are returning plain text, then try client.Headers.Add("Accept", "text/plain");
  • Sameh Awad
    Sameh Awad over 7 years
    @A_J Can u check it with Fiddler, may be the problem is with your Http Response. If you can post here the response that Fiddler sees may be it would be helpful.
  • Amit Joshi
    Amit Joshi over 7 years
    I am not sure what exactly from Fiddler you are asking. See if following is helpful; otherwise let me know. **RESPONSE BYTES (by Content-Type) -------------- ~headers~: 101 ~???????~: 10 **
  • Amit Joshi
    Amit Joshi over 7 years
    Response Header from Fiddler: -HTTP/1.1 1201 Server: Apache-Coyote/1.1 Content-Length: 10 Date: Tue, 27 Sep 2016 11:10:23 GMT SUCCESS
  • Amit Joshi
    Amit Joshi over 7 years
    Response Auth: No Proxy-Authenticate Header is present. No WWW-Authenticate Header is present.
  • Sameh Awad
    Sameh Awad over 7 years
    @A_J Ok If you are just returning SUCCESS then why is your Content-Length:10 and not Content-Length:7. Also check the Content-Type in the response header on Fiddler whether it is has a charset sent with it. Something like Content-Type: text/plain; charset=UTF-8
  • Amit Joshi
    Amit Joshi over 7 years
    I am doing this synchronously. I get following exception: - An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll on line HttpResponseMessage response = client.GetAsync(requestUrl).GetAwaiter().GetResult()
  • Matias Cicero
    Matias Cicero over 7 years
    @A_J Looks like an issue on the server, not the client. Client code is fine.
  • Sreerag G
    Sreerag G about 4 years
    It's bad practice to dispose HttpClient please fix @MatiasCicero