Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebResponse is always null

I have method which makes HTTP POST

I call this method with try catch like this

try
{
   returnString = MakePost(gatewayEnpoint, data);
   return returnString;
}
catch (System.Net.WebException ex)
{
   var response = (HttpWebResponse)ex.Response;
   if (response.StatusCode != HttpStatusCode.RequestTimeout || response.StatusCode != HttpStatusCode.InternalServerError)
   {
       return ex.Message.ToString();
   }
}

but response is always null.

How i can handle HTTP exceptions? i need status codes

thanks

like image 219
Irakli Lekishvili Avatar asked Nov 06 '12 10:11

Irakli Lekishvili


1 Answers

There won't be a response if the client hasn't even managed to contact the server.

The cases in which there will be a response are for things like 404 status codes.

If you're never getting a response, then you're probably making some other mistake (e.g. trying to contact a server which doesn't exist).

I would strongly recommend that you don't return success and error cases in the same way as you're doing now, by the way.

like image 86
Jon Skeet Avatar answered Sep 23 '22 11:09

Jon Skeet