• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

ASP.NET/VB/: Return full response if error code received using HttpWebRequest

How can I get the full output returned with this web request?  The remote server returns an error code status header but it ALSO returns content after the headers have been sent.  I want to capture the response.
<%@ Page Language="VB"%>
<%@Import namespace="System"%>
<%@Import namespace="System.Web"%>
<%@Import namespace="System.Net"%>
<%@Import namespace="System.IO"%>
<%@Import namespace="System.Text"%>
<%

Dim data As String = "{""hello"": ""world"", ""number"": 555}"
Dim webRequest As HttpWebRequest = TryCast(WebRequest.Create("https://zuhqtr5.couchappy.com/"), HttpWebRequest)
Dim auth As String = "admin" & ":" & "password123"
auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth))
webRequest.Headers("Authorization") = "Basic " & auth
webRequest.Method = "POST"
webRequest.ContentType = "application/json"
webRequest.ContentLength = data.Length
webRequest.Accept = "application/json"
webRequest.ReadWriteTimeout = 500000
Dim requestWriter As New StreamWriter(webRequest.GetRequestStream())
requestWriter.Write(data)
requestWriter.Close()
Dim webResp As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
Dim webResult As Stream = webResp.GetResponseStream()
Dim result As New StreamReader(webResult)
HttpContext.Current.Response.Write(result.ReadToEnd())

%>

Open in new window

0
hankknight
Asked:
hankknight
2 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
I'm not quite sure what you are after. The lines:
Dim webResult As Stream = webResp.GetResponseStream()
Dim result As New StreamReader(webResult)
HttpContext.Current.Response.Write(result.ReadToEnd())

Open in new window

Should already be retrieving anything sent back by the remote server.
0
 
hankknightAuthor Commented:
My code only returns the content sent back if a "200 OK" status header is sent.  If "405 Method Not Allowed" header is sent I see an ASP.NET error and NOT the error from the remote server.  See the attached screenshot.
iisError.png
0
 
Chinmay PatelEnterprise ArchitectCommented:
Great. Can you turn on debug mode as suggested in error to see where this error is happening?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now