Solved

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

Posted on 2013-12-18
4
379 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
[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
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

623 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