HttpWebRequest proper exception handling

2.7k views Asked by At

So I'm using the HttpWebRequest API in the System.Net assembly but because C# has no checked exceptions, I'm not sure where to put my try-catch blocks to properly handle inevitable exceptions caused by common things like a network error. You know, in Java we would call these plain old checked IOExceptions.

This is what I have so far. Are my try-catch blocks properly set up to handle network errors? Am I wrapping the right method calls? Looking at the documentation, I think they are right, but I need a second pair of eyes.

HttpWebRequest request = WebRequest.CreateHttp(url);
request.Method = "POST";
request.BeginGetRequestStream(getRequestResult =>
            {
                HttpWebRequest getRequestRequest = (HttpWebRequest) getRequestResult.AsyncState;
                try
                {
                    Stream requestStream = getRequestRequest.EndGetRequestStream(getRequestResult);
                    requestStream.Write(parametersData, 0, parametersData.Length);
                    requestStream.Dispose();
                    getRequestRequest.BeginGetResponse(getResponseResult =>
                        {
                            HttpWebRequest getResponseRequest = (HttpWebRequest)getResponseResult.AsyncState;
                            try
                            {
                                WebResponse response = getResponseRequest.EndGetResponse(getRequestResult);
                                Stream responseStream = response.GetResponseStream();
                                StreamReader reader = new StreamReader(responseStream);
                                string jsonString = reader.ReadToEnd();
                                reader.Dispose();
                                JObject jsonObject = JObject.Parse(jsonString);
                                onResult(StatusCode.Ok, jsonObject);
                            }
                            catch (WebException)
                            {
                                onResult(StatusCode.NetworkError);
                            }
                        }, getRequestRequest);
                }
                catch (IOException)
                {
                    onResult(StatusCode.NetworkError);
                }
            }, request);
1

There are 1 answers

0
Justin Grant On

First off, unless there's some reason that you need to use HttpWebRequest, then you're better off using WebClient.UploadString instead, or any of WebClient's other UploadXXX overloads for uploading name/value pairs, files, binary data, etc. This will be much easier for you, and easier to troubleshoot and debug. Also, another problem is that you're treating exceptions during JSON parsing or during your onResult handler error as network errors.

Below are three examples of using WebClient that you might want to try: a synchronous version, an "old-style" async version, and a "new-style" async version that uses async/await. All three versions also try to fix the exception handling issue that I noted above. If you don't need async support, then the first version will be easiest.

static void PostSync (string url, string parametersData)
{
    using (WebClient wc = new WebClient())
    {
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; // or "application/json" or ...
        try
        {
            string htmlResult = wc.UploadString(url, parametersData);  // or UploadValues, UploadFile, ... 
            JObject jsonObject = null;
            try
            {
                jsonObject = JObject.Parse(htmlResult);
            }
            catch (JsonException ex)
            {
                onResult(StatusCode.JsonError);
            }
            onResult(StatusCode.Ok, jsonObject);

        }
        catch (System.Net.WebException ex)
        {
            onResult(StatusCode.NetworkError);
        }
    }
}

static void PostAsync(string url, string parametersData)
{
    using (WebClient wc = new WebClient())
    {
        wc.UploadStringCompleted += (Object sender, UploadStringCompletedEventArgs e) =>
        {
            if (e.Error != null)
                onResult(StatusCode.NetworkError);
            JObject jsonObject = null;
            try
            {
                jsonObject = JObject.Parse(e.Result);
            }
            catch (JsonException ex)
            {
                onResult(StatusCode.JsonError);
            }
            onResult(StatusCode.Ok, jsonObject);
        };
        try
        {
            wc.UploadStringAsync(new Uri(url, UriKind.Absolute), parametersData);
        }
        catch (System.Net.WebException ex)
        {
            onResult(StatusCode.NetworkError);
        }
    }
}

static async void PostTaskAsync(string url, string parametersData)
{
    using (WebClient wc = new WebClient())
    {
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; // or "application/json" or ...
        try
        {
            string htmlResult = await wc.UploadStringTaskAsync(url, parametersData);  // or UploadValues, UploadFile, ... 
            JObject jsonObject = null;
            try
            {
                jsonObject = JObject.Parse(htmlResult);
            }
            catch (JsonException ex)
            {
                onResult(StatusCode.JsonError);
            }
            onResult(StatusCode.Ok, jsonObject);

        }
        catch (System.Net.WebException ex)
        {
            onResult(StatusCode.NetworkError);
        }
    }
}