Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I read the response from a web request when the Status is not 200?
    primarykey
    data
    text
    <p>I am having difficulty getting the response text from a HTTP web request in vb.net when I get a web exception.</p> <p>This is the code I am doing it with. </p> <pre><code>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 &amp; "Status Description = " &amp; HttpWebResponse.StatusDescription End Using Catch ex As Exception Logger.LogError(Me, ex) End Try End Using </code></pre> <p>Annoyingly, the API I am contacting uses a 404 as a valid response. If I put the request in a browser some message text will be displayed. I want to be able to use that text in my program. I can not simply use the error code to determine actions as I don't think I can differentiate between a valid 404 response and an actual error.</p> <p>In the code this line</p> <pre><code>myWebResponse = CType(request.GetResponse(), HttpWebResponse) </code></pre> <p>throws an exception.</p> <p>In the exception I can get the 404 code and the description but not the response stream. It is always null. </p> <p>If I get a 200 response I get the text in the Response stream no problem. </p> <p>In the web exception response object (in Visual Studios debugger) I have checked the headers and the object values and can't find the response text anywhere. If I stick the request URL in a browser I get response text back even though it is a 404. </p> <p>The raw response in fiddler:</p> <pre class="lang-none prettyprint-override"><code>HTTP/1.1 404 Not Found Connection: close Content-Type: text/plain; charset=UTF-8 Content-Length: 35 "The response Message" </code></pre> <p>Any ideas on how I can get "The response Message" in my program? I have to use .Net on the server. </p> <p>Thanks for any help anybody can give.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload