How can I read the response from a web request if the status is not 200?

I find it difficult to get the response text from an HTTP web request in vb.net when I get a web exception.

This is the code I'm doing this with.

Try myWebResponse = CType(request.GetResponse(), HttpWebResponse) myStreamReader = New StreamReader(myWebResponse.GetResponseStream()) ResponseText = myStreamReader.ReadToEnd If myWebResponse.StatusCode = HttpStatusCode.Accepted Or myWebResponse.StatusCode = 200 Then SendResult = True 'Sent SendStatus = 1 'message sent successfully Try Integer.TryParse(myWebResponse.Headers("Number-Of-MT-PDU"), num_MT_PDU) Catch ex As Exception End Try Else SendStatus = 2 'message processed but not sent successfully End If Catch e As WebException If (e.Status = WebExceptionStatus.ProtocolError) Then Dim response As WebResponse = e.Response Using (response) Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse) statusCode = httpResponse.StatusCode Try myStreamReader = New StreamReader(response.GetResponseStream()) Using (myStreamReader) ResponseText = myStreamReader.ReadToEnd & "Status Description = " & HttpWebResponse.StatusDescription End Using Catch ex As Exception Logger.LogError(Me, ex) End Try End Using 

Annoyingly, the API I'm linking to uses 404 as a valid answer. If I placed the request in the browser, the message text will be displayed. I want to be able to use this text in my program. I canโ€™t just use the error code to determine the actions, because I donโ€™t think I can distinguish between a valid 404 answer and an actual error.

In the code of this line

 myWebResponse = CType(request.GetResponse(), HttpWebResponse) 

throws an exception.

In the exception, I can get the 404 code and description, but not the response flow. It is always zero.

If I get a response of 200, I get the text in the response stream without any problems.

In the exception response web object (in the Visual Studios debugger), I checked the headers and values โ€‹โ€‹of the objects and could not find the response text anywhere. If I bind the request URL in the browser, I get the response text, even if it is 404.

Raw response in the violinist:

 HTTP/1.1 404 Not Found Connection: close Content-Type: text/plain; charset=UTF-8 Content-Length: 35 "The response Message" 

Any ideas on how I can get a โ€œReply Messageโ€ in my program? I have to use .Net on the server.

Thanks for any help anyone can give.

+7
source share
2 answers

This LINQPad query works fine by unloading the HTML provided by the Not Found web server webpage:

 Dim rq = System.Net.WebRequest.Create(New Uri("http://localhost/test")) Try Dim rs = rq.GetResponse rs.Dump Catch Ex As System.Net.WebException Dim rs = Ex.Response Call (New StreamReader(rs.GetResponseStream)).ReadToEnd.Dump End Try 

Fyi. Your code works for me, except for the alleged typo re HttpWebResponse.StatusDescription (and commenting on "unrelated things"), again as a LINQPad request (in .NET 4.0):

 Dim request = WebRequest.Create("http://localhost/test") Dim myStreamReader As StreamReader Dim SendStatus As Integer = -1 Dim statusCode As HttpStatusCode Dim ResponseText As String Try Dim myWebResponse = CType(request.GetResponse(), HttpWebResponse) myStreamReader = New StreamReader(myWebResponse.GetResponseStream()) ResponseText = myStreamReader.ReadToEnd If myWebResponse.StatusCode = HttpStatusCode.Accepted Or myWebResponse.StatusCode = 200 Then 'SendResult = True 'Sent SendStatus = 1 'message sent successfully 'Try ' Integer.TryParse(myWebResponse.Headers("Number-Of-MT-PDU"), num_MT_PDU) 'Catch ex As Exception 'End Try Else SendStatus = 2 'message processed but not sent successfully End If Catch e As WebException If (e.Status = WebExceptionStatus.ProtocolError) Then Dim response As WebResponse = e.Response Using (response) Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse) statusCode = httpResponse.StatusCode Try myStreamReader = New StreamReader(response.GetResponseStream()) Using (myStreamReader) ResponseText = myStreamReader.ReadToEnd & "Status Description = " & httpResponse.StatusDescription ' HttpWebResponse.StatusDescription End Using Catch ex As Exception 'Logger.LogError(Me, ex) ex.Dump("Exception") End Try End Using End If End Try ResponseText.Dump("ResponseText") 

I also confirmed the above code (with added As clauses and converting .Dump calls to Console.WriteLine ) works in .NET 2.0 with VB8.

+4
source

Note that the key is that although the GetResponseStream () action throws a .NET WebException, the HttpWebResponse is actually passed to the WebException object, so when in Catch you create a new GetResponseStream () in the WebException. The object of the response.

Below, very similar code for when in the Catch source GetResponseStream ()

  Try OriginalResponseStream = GetResponseStream(OriginalHTTPWebResponse) Catch wex as WebException Dim response As WebResponse = wex.Response Dim statusCode As HttpStatusCode Dim ResponseText As String Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse) statusCode = httpResponse.StatusCode Try Dim myStreamReader As New StreamReader(response.GetResponseStream()) Using (myStreamReader) ResponseText = myStreamReader.ReadToEnd Process(ResponseText) '<===as in whatever you need to do with the response End Using Catch ex As Exception HandleIt(ex.Message) '<===as in whatever you want to do if Exception during the above End Try End Try 
0
source

All Articles