Solved

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

Posted on 2013-12-18
4
377 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

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!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
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 …

735 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