Question about VB program results...

D

David

I have the following two button click procedures to retrieve html code from
web sites.

The code for Button1 seems to work fine. Button2 works fine for
http://yahoo.com but not for http://groups.google.com. When trying the
Google site, iContentLength is -1 after it is set to
myResponse.ContentLength. Any ideas why? If I uncomment the lines in
Button2's event handler then that part of it will work with Google's site,
using the same technique that Button1 uses.

I'm trying to learn VB while working on this so that's why I'm interested in
why Button2 works for some sites but not for all...

It seems like iContentLength = myResponse.ContentLength doesn't work
properly. I am not sure how to look at the myResponse stream object using
the debugger so I don't know how to make heads or tails of this.

Thanks for any help,

David
 
D

David

David said:
I have the following two button click procedures to retrieve html code from
web sites.

The code for Button1 seems to work fine. Button2 works fine for
http://yahoo.com but not for http://groups.google.com. When trying the
Google site, iContentLength is -1 after it is set to
myResponse.ContentLength. Any ideas why? If I uncomment the lines in
Button2's event handler then that part of it will work with Google's site,
using the same technique that Button1 uses.

I'm trying to learn VB while working on this so that's why I'm interested
in why Button2 works for some sites but not for all...

It seems like iContentLength = myResponse.ContentLength doesn't work
properly. I am not sure how to look at the myResponse stream object using
the debugger so I don't know how to make heads or tails of this.

Thanks for any help,

David

The code might help a bit...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim myChar As Char
Dim myRequest As HttpWebRequest =
HttpWebRequest.Create(Me.textURL.Text)
Dim webResponse As HttpWebResponse = myRequest.GetResponse
Dim responseStream As IO.Stream = webResponse.GetResponseStream
Dim responseReader As StreamReader = New
StreamReader(responseStream)
Dim writer As StreamWriter = New StreamWriter("c:\textStream.txt",
Me.ckAppend.Checked)
writer.WriteLine("Dumping HTML stream...")
Do Until responseReader.EndOfStream = True
myChar = ChrW(responseReader.Read)
If Asc(myChar) = 10 Then writer.Write(Chr(13))
writer.Write(myChar)
Loop
writer.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim myRequest As HttpWebRequest =
HttpWebRequest.Create(Me.textURL.Text)
Dim myResponse As HttpWebResponse = myRequest.GetResponse
Dim m_sPageResponded As String = myResponse.ResponseUri.ToString
Dim iContentLength As Integer
Dim sTotalBuffer() As Byte
Dim br As New BinaryReader(myResponse.GetResponseStream)
Dim sBuffer() As Byte
Dim m_iBytesRead As Integer
Dim iTotalBytes As Integer
Dim myChar As Char
Dim i As Integer
iContentLength = myResponse.ContentLength
' Dim responseStream As IO.Stream = myResponse.GetResponseStream
' Dim responseReader As StreamReader = New
StreamReader(responseStream)
' Dim writer2 As StreamWriter = New
StreamWriter("c:\textStream2.txt", Me.ckAppend.Checked)
' writer2.Write(responseReader.ReadToEnd)
' writer2.Close()

ReDim sTotalBuffer(iContentLength - 1)
m_iBytesRead = 1
iTotalBytes = 0
Do Until m_iBytesRead = 0
ReDim sBuffer(iContentLength - 1)
m_iBytesRead = br.Read(sBuffer, 0, iContentLength)
ReDim Preserve sBuffer(m_iBytesRead - 1)
If m_iBytesRead > 0 Then Array.Copy(sBuffer, 0, sTotalBuffer,
iTotalBytes, sBuffer.Length)
iTotalBytes += m_iBytesRead
Loop
Dim writer As StreamWriter = New StreamWriter("c:\textStream.txt",
Me.ckAppend.Checked)
writer.WriteLine("Dumping HTML stream...")
For i = LBound(sTotalBuffer) To UBound(sTotalBuffer)
myChar = ChrW(sTotalBuffer(i))
If sTotalBuffer(i) = 10 Then writer.Write(Chr(13))
writer.Write(myChar)
Next
writer.Close()
End Sub
 

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