How to get value from object?

B

Brett

I'm having difficults getting at values of an object. Here is my code,
which only gets a webpage. The two values I can't reach have comments next
to them. I've also listed the watches tree and with values I'd like to get
within the wResp object.

Dim wReq As WebRequest = WebRequest.Create(url)
If TypeOf wReq Is HttpWebRequest Then
CType(wReq, HttpWebRequest).UserAgent = "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.1; Feedreader; .NET CLR 1.1.4322; .NET
CLR 2.0.40607)"
End If
Dim wResp As WebResponse = wReq.GetResponse()
myContentType = wResp.ContentType
myStatusCode= wResp.StatusCode 'this is not valid
myStatusDescription = wResp.StatusDescription 'this is not
valid

In the Watches window, the tree for wResp looks like this:

Name Value
Type
- wResp
{System.Net.HttpWebResponse} System.Net.HttpWebResponse
- {System.Net.HttpWebResponse} {System.Net.HttpWebResponse}
System.Net.HttpWebResponse
ContentType "image/gif"
String
StatusCode OK
System.Net.HttpStatusCode
StatusDescription "OK"
String

I don't see a wResp.StatusCode or wResp.StatusDescription listed in the drop
down pop hint. What am I missing to get at those values?

Also, how do I get the numerical values of response codes? Such as "OK" =
200 or "Bad request" = 400. I don't see anything like that listed in the
watch window.

Thanks,
Brett
 
J

Jay B. Harlow [MVP - Outlook]

Brett,
Dim wResp As WebResponse = wReq.GetResponse()

You have a WebResponse variable.
myStatusCode= wResp.StatusCode 'this is not valid

However you are attempting to return HttpWebResponse values.

Seeing as the original request was a HttpWebRequest, wResp will contain an
HttpWebResponse, you need to cast the variable to HttpWebResponse to use the
values.

Dim wResp As HttpWebResponse = DirectCast(wReq.GetResponse(),
HttpWebResponse)


NOTE: CType is the convertion operator, while DirectCast is a cast operator.
CType will attempt to convert the value if a conversion is possible (a
string to an integer), while DirectCast requires the value to be the
specified type (GetResponse *is* returning a HttpWebResponse object).

Hope this helps
Jay
 

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