Solved

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

Posted on 2013-12-23
3
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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