1

I am posting JSON data to API. If I post wrong data, catch block is not catching error. Controls stops at using (httpResponse = (HttpWebResponse)httpWebRequest.GetResponse()) this point and shows error. What wrong I am doing. Following is my code,

try
        {

            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("ipaddress");
            httpWebRequest.Credentials = new NetworkCredential("", "");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string name = objTSPost.name;
                string servicetype = objTSPost.service_type;
                string json = "{\"name\":\"VMR_" + name + "\"," +
                              "\"service_type\":\"" + servicetype + "\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }


            using (httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
                string str = "{\"name\":\"VMR_" + objTSPost.name + "\"," +
                                  "\"service_type\":\"" + objTSPost.service_type + "\"}";
                var data = JsonConvert.DeserializeObject<TSGetRootObject>(str);
                data.status = ((HttpWebResponse)httpResponse).StatusDescription;
                return data;
            }

        }
        catch (WebException ex)
        {
            objTSPost.status = ex.Message;
            return objTSPost;
        }

    }
6
  • Can you post some details about the error you are getting? The message, the type of the exception? Commented Oct 3, 2016 at 6:58
  • 2
    Use more generic Exception instead of WebException if catch is NOT catching. Commented Oct 3, 2016 at 7:04
  • You are catching exceptions of type webexception only. It might be throwing some other type of exception, thats why its not being caught. I suggest, you should add another catch block of generic type "Exception". Something like this catch (Exception ex) { objTSPost.status = ex.Message; return objTSPost; } Commented Oct 3, 2016 at 7:04
  • Following error i am getting "An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The remote server returned an error: (400) Bad Request." Commented Oct 3, 2016 at 7:05
  • How to handle these type of errors Commented Oct 3, 2016 at 7:13

1 Answer 1

2

Sachin is correct, you should handle exceptions from the most specific to the least specific exception.

Also it is not a good practice propagating the exception message to the users since it might reveal security vulnerabilities, instead I suggest you log the actual message and propagate a standard userfriendly message instead. Maybe you are doing that after the method returns it's value but since the rest of the code is not available I'd just like to give you a heads up on it.

    try
    {

        //My maybe not toally reliable code

    }
    catch (WebException ex)
    {
        LogMessage(ex.Message);
        objTSPost.status = "My custom userfriendly specific web exception message";
        return objTSPost;
    }
    catch(Exception ex)
    {
        LogMessage(ex.Message);
        objTSPost.status = "My custom userfriendly unhandled exception message";
        return objTSPost;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @JFM for your valuable suggesstion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.