Solved

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

Posted on 2013-12-23
3
539 Views
Last Modified: 2014-01-07
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
Comment
Question by:hankknight
3 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 39736183
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
 
LVL 16

Author Comment

by:hankknight
ID: 39736327
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
 
LVL 27

Accepted Solution

by:
Chinmay Patel earned 250 total points
ID: 39737380
Great. Can you turn on debug mode as suggested in error to see where this error is happening?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question