Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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