Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-18
4
Medium Priority
?
380 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 1000 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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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