question

DarrenRose-2607 avatar image
0 Votes"
DarrenRose-2607 asked DarrenRose-2607 commented

Getting StatusCode, StatusDescription and ResponseUri from System.Net.Http.HttpRequestException

Hi

My app is throwing a System.Net.Http.HttpRequestException when an API I use (Postmark) can't connect due to in my case proxy issues.

ex.message only gives me "An error occurred while sending the request." which is not much use in diagnosing cause

Looking in ex.message.innerexception I can see "The remote server returned an error: (403) Forbidden." which is more useful but still doesn't solve my problem

If I run the app in VS I can see that in InnerException there is Response which contains more detailed information which contains what I need to know to solve the problem e.g. StatusCode, StatusDescription and ResponseUri

83676-image.png

My question is how can I access these values from my code when not running in debugger as the obvious ex.InnerException.Response.StatusCode doesn't work as "Response is not a member of Exception" despite it appearing as so in debugger


dotnet-visual-basic
image.png (254.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
2 Votes"
Viorel-1 answered DarrenRose-2607 commented

If you know the possible type of the exception (WebException for example, which can be determined during debugging), then try something like this:

 Try
     '...
 Catch exc As Exception
     Dim web_exc As WebException = TryCast(exc.InnerException, WebException)
     If web_exc IsNot Nothing Then
         Dim response As HttpWebResponse = TryCast(web_exc.Response, HttpWebResponse)
         If response IsNot Nothing Then
             Dim status = response.StatusCode
             '...
         End If
     End If
 End Try

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you Viorel-1 that was very helpful

In my case the exception is a System.Net.Http.HttpRequestException

Your code as is worked fine and gives me information I need :)

But not sure when/if I should be changing where you have WebException to HttpRequestException and how I know - as I tried changing it but then get "Response is not a member of HttpRequestException", so not sure if I would always use WebException even for HttpRequestException errors, or if need to change something else as well?

Thanks

83751-image.png


0 Votes 0 ·
image.png (24.5 KiB)
Viorel-1 avatar image Viorel-1 DarrenRose-2607 ·

How did you determine that ex.InnerException is a HttpRequestException.

0 Votes 0 ·

I determined ex was a HttpRequestException just from output of ex.ToString

83689-image.png


0 Votes 0 ·
image.png (138.6 KiB)

So am I right in thinking System.Net.Http.HttpRequestException is the Exception and System.Net.WebException is the InnerException? if so I answered my own question I think - thank you :)

0 Votes 0 ·