WebException (Status: Protocol Error)

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

Leave a Comment