GetResponseStream

  • Thread starter Thread starter Sehboo
  • Start date Start date
S

Sehboo

Hello,

I have two questions:

1. How can I download a webpage only if the content type is "text"
without actually downloading and checking it?

2. When I download the page (using the code that I have posted here),
it changes the content of the page. Like if the page has abcd%40efg
it makes it abcd@efg, or this is atleast what I get in my msPage.
What do I need to do so that I get abcd%40efg, and not abcd@efg?

Thanks

mwPageRequest = CType(WebRequest.Create(msURL),
HttpWebRequest)
mwPageRequest.Timeout = miTimeout
mwPageRequest.AllowAutoRedirect = True
mwPageResponse = mwPageRequest.GetResponse()
Dim r As New
StreamReader(mwPageResponse.GetResponseStream())
msPage = r.ReadToEnd()
mwPageResponse.Close()
r.Close()
 
1. How can I download a webpage only if the content type is "text"
without actually downloading and checking it?

Use the HEAD method and check the ContentType. Then if it matches the
content type you want download the entire page.

Dim wr As HttpWebRequest
Dim resp As HttpWebResponse

wr = DirectCast(HttpWebRequest.Create("http://www.yahoo.com"), _
HttpWebRequest)
wr.AllowAutoRedirect = True
wr.Timeout = 5000
wr.Method = "HEAD"

resp = DirectCast(wr.GetResponse(), HttpWebResponse)

If resp.ContentType = "text/html" Then

wr = DirectCast(HttpWebRequest.Create("http://www.yahoo.com"), _
HttpWebRequest)
wr.Timeout = 5000
wr.AllowAutoRedirect = True

resp = DirectCast(wr.GetResponse(), HttpWebResponse)

Dim reader As New IO.StreamReader(resp.GetResponseStream())
MsgBox(reader.ReadToEnd)

End If
 
Back
Top