Solved

ASP.NET/VB:  HttpWebRequest and Full Error Message from Remote Server

Posted on 2013-12-18
4
372 Views
Last Modified: 2014-01-07
This code returns an error:
"The remote server returned an error: (405) Method Not Allowed."

This is because the remote server sent an error code header.

The remote server ALSO returned a friendly error message and I want to see what the remote server returned.  The error message contains information I need.  It is not good enough to know that the remote server returned an error header.  I want my code to tell me in full what the remote server returned.
<%@ 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
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 39728473
The message is there... the method POST(strange) is not allowed. Can you check on the remote server? is POST method allowed?
0
 
LVL 16

Author Comment

by:hankknight
ID: 39729206
I want the error to be passed on EXACTLY as it is received.
0
 
LVL 27

Assisted Solution

by:Chinmay Patel
Chinmay Patel earned 250 total points
ID: 39735496
You understand that it is a perfectly fine error message:  (405) Method Not Allowed right?

You will get this message in case the POST method is blocked on the server.
It might be blocked on remote server for many reasons, security being the foremost.
Do you have any control over the remote server?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 39755007
Enclose this code in try ... catch block and see if you can extract the error information from exception that is thrown.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now