Amit Joshi
Amit Joshi

Reputation: 16389

WebException (Status: Protocol Error)

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)

Upvotes: 3

Views: 27546

Answers (2)

Matias Cicero
Matias Cicero

Reputation: 26281

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();
}

Upvotes: 4

Sameh Awad
Sameh Awad

Reputation: 324

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.

Upvotes: 4

Related Questions