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.
Ruairi O'Brien
source share