Get server errordescrition

D

Dirk.Reimers

Hi all,

sorry for my dumb question:

I use the following code:

Dim request As WebRequest = HttpWebRequest.Create("http://" &
ip & _
/TVBrowserTimerEvent?command=add")
request.Credentials = New NetworkCredential("root", "toor")
Try
Dim response As HttpWebResponse =
CType(request.GetResponse(), HttpWebResponse)
Catch ex As Exception
MsgBox(ex.Message)
End Try

which is working fine if the server responds with Code 200 OK

But I'm not able to get any additional info if the request goes wrong.

The servers answer is (for example):

-----
HTTP/1.1 400 Function failed.
Connection: close
Content-Length: 46
Content-Type: text/html; charset=utf-8

service name don't match.
----

But I'm only able to retrieve the 400 error code by accessing the ex
element. I do not know how to access the additional "service name
don't match." information.

Any hints?

Thanx

Dirk
 
B

bruce barker

there is no standard for returning errors other than the status description.
you may need to read the response content (response.GetResponseStream) to get
detail.

-- bruce (sqlwork.com)
 
D

Dirk.Reimers

there is no standard for returning errors other than the status description.
you may need to read the response content (response.GetResponseStream) to get
detail.

-- bruce (sqlwork.com)

As far as I read i can use

Dim response As HttpWebResponse = request.GetResponse()
Dim responseStream As Stream = response.GetResponseStream
Dim reader As StreamReader = New
StreamReader(responseStream)

but vb.net throw an error just after the first line (Dim response As
HttpWebResponse = request.GetResponse()) as the server send bacxk an
400 status code. The next line of code doen't get executed anymore.


Dirk
and then
 
B

bruce barker

dim response before the try, then you can access it in the catch. be sure to
check that it is not null which it will be if there is a connection error
rather than a bad response code.

-- bruce (sqlwork.com)
 
D

Dirk.Reimers

Hi,

here are some server communication sniffs:

everythink ok:

---
GET /TVBrowserTimerEvent?command=add
HTTP/1.1
Authorization: Basic cm9vdDpkcmVhbWJveA==


HTTP/1.1 200 OK
Connection: close
Content-Length: 37
Content-Type: text/html; charset=utf-8

event was created successfully.
----

and now the error state:

---
GET /TVBrowserTimerEvent?command=add
HTTP/1.1
Authorization: Basic cm9vdDpkcmVhbWJveA==


HTTP/1.1 400 Function failed.
Connection: close
Content-Length: 97
Content-Type: text/html; charset=utf-8

event could not be added
---
in the error case 'request' is 'Nothing' indeed and the catch can
only access the ex error.The error description mentions server code
400. So I think the communiction is quite ok (I can see a valid HTTP
connection in the ehternet sniff) and the error is not a connetcion
error code. But I am not able to get any further descrition to be
parsed as there are some different errors with the same HTTP error
code.

Thanks for you support.

Dirk
 
D

Dirk.Reimers

Thank you Bruce,

I finally found the correct code:

---
Dim request As WebRequest = HttpWebRequest.Create("http://" &
ip & "/TVBrowserTimerEvent?command=add")
request.Credentials = New NetworkCredential("root", "toor")

Try
Dim response As HttpWebResponse = request.GetResponse()
Catch ex As WebException
Dim error_response As HttpWebResponse = CType(ex.Response,
HttpWebResponse)
Dim error_stream As Stream =
error_response.GetResponseStream
Dim stream_content As StreamReader = New
StreamReader(error_stream)
MsgBox(stream_content.ReadToEnd, MsgBoxStyle.Exclamation,
"Event could not be added")
End Try

---

The clue was to ctype the exception to an httpwebresponse, than access
the webresponses stream and read it to end.

Thanx a lot

Dirk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top