Get server errordescrition

Get server errordescrition

am 19.12.2007 17:23:11 von 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

RE: Get server errordescrition

am 19.12.2007 18:13:02 von brucebarker

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)


"Dirk.Reimers" wrote:

> 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
>

Re: Get server errordescrition

am 19.12.2007 20:40:11 von dirk.reimers

On 19 Dez., 18:13, bruce barker
wrote:
> 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)
>
> "Dirk.Reimers" wrote:
> > 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

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

Re: Get server errordescrition

am 19.12.2007 22:18:01 von brucebarker

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)


"Dirk.Reimers" wrote:

> On 19 Dez., 18:13, bruce barker
> wrote:
> > 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)
> >
> > "Dirk.Reimers" wrote:
> > > 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
>
> 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
>
>

Re: Get server errordescrition

am 20.12.2007 08:17:52 von 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

Re: Get server errordescrition

am 20.12.2007 21:10:17 von 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