WebRequest doesn't always return all of the data 
Author Message
 WebRequest doesn't always return all of the data

Hi!

The HttpWebReqest doesn't always return the whole page I'm requesting.
Sometimes it returns just a few hundered characters of a page that's really
24000 chars long. Then I try again, and it returns perhaps 16000 characters,
or maybe just 2000 characters.

My code can be found in a different post I did the other day, with the
subject "WebRequests keep timing out".

Anyone knows why this is happening?

/john



Sat, 30 Jul 2005 10:32:30 GMT  
 WebRequest doesn't always return all of the data
Got code?


Quote:
> Hi!

> The HttpWebReqest doesn't always return the whole page I'm requesting.
> Sometimes it returns just a few hundered characters of a page that's
really
> 24000 chars long. Then I try again, and it returns perhaps 16000
characters,
> or maybe just 2000 characters.

> My code can be found in a different post I did the other day, with the
> subject "WebRequests keep timing out".

> Anyone knows why this is happening?

> /john



Sat, 30 Jul 2005 10:52:23 GMT  
 WebRequest doesn't always return all of the data
Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Form1

Inherits System.Windows.Forms.Form
Private Const MAX_LEVEL = 5
Dim result As HttpWebResponse
Dim req As HttpWebRequest
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
Private Sub GetPage()
req = WebRequest.Create("http://www.somedomain.com")
req.Timeout = 2000
Try
result = req.GetResponse()
Catch ex As Exception
End Try
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("UTF-7")
sr = New StreamReader(ReceiveStream, encode)
Dim read(32767) As Char
sr.Read(read, 0, 32767)
Dim s As String = New String(read, 0, 32767)
TextBox.Text = s
Label.Text = Len(TextBox.Text)
result.Close()
ReceiveStream.Close()
End Sub

End Class



Quote:
> Got code?



> > Hi!

> > The HttpWebReqest doesn't always return the whole page I'm requesting.
> > Sometimes it returns just a few hundered characters of a page that's
> really
> > 24000 chars long. Then I try again, and it returns perhaps 16000
> characters,
> > or maybe just 2000 characters.

> > My code can be found in a different post I did the other day, with the
> > subject "WebRequests keep timing out".

> > Anyone knows why this is happening?

> > /john



Sun, 31 Jul 2005 04:27:53 GMT  
 WebRequest doesn't always return all of the data
Hi!

It would be nice if you could also post the actual datafile that is being
corrupted.

I noticed from your code below, that you are just doing one read for 32767
characters. The problem with this is that you are assuming that all the data
fits in 32767 characters. That might not be true in your scenario.

A better way to do this would be: (C#)

MemoryStream ms = new MemoryStream();

Stream rs = resp.GetResponseStream();
byte [] buffer = new byte[1024];
int read = rs.Read(buffer,0,buffer.Length);
while(read > 0) {
    ms.Write(buffer,0,read);
    read = rs.Read(buffer,0,buffer.Length);

Quote:
}

rs.Close();

ms.Seek(0,SeekOrigin.Begin);
Encoding encoding = Encoding.UTF7;
StreamReader sr = new StreamReader(ms, encoding);

string s = sr.ReadToEnd();
--
Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


Quote:
> Imports System
> Imports System.Net
> Imports System.IO
> Imports System.Text
> Imports System.Text.RegularExpressions

> Public Class Form1

> Inherits System.Windows.Forms.Form
> Private Const MAX_LEVEL = 5
> Dim result As HttpWebResponse
> Dim req As HttpWebRequest
> Dim ReceiveStream As Stream
> Dim encode As Encoding
> Dim sr As StreamReader
> Private Sub GetPage()
> req = WebRequest.Create("http://www.somedomain.com")
> req.Timeout = 2000
> Try
> result = req.GetResponse()
> Catch ex As Exception
> End Try
> ReceiveStream = result.GetResponseStream()
> encode = System.Text.Encoding.GetEncoding("UTF-7")
> sr = New StreamReader(ReceiveStream, encode)
> Dim read(32767) As Char
> sr.Read(read, 0, 32767)
> Dim s As String = New String(read, 0, 32767)
> TextBox.Text = s
> Label.Text = Len(TextBox.Text)
> result.Close()
> ReceiveStream.Close()
> End Sub

> End Class



> > Got code?



> > > Hi!

> > > The HttpWebReqest doesn't always return the whole page I'm requesting.
> > > Sometimes it returns just a few hundered characters of a page that's
> > really
> > > 24000 chars long. Then I try again, and it returns perhaps 16000
> > characters,
> > > or maybe just 2000 characters.

> > > My code can be found in a different post I did the other day, with the
> > > subject "WebRequests keep timing out".

> > > Anyone knows why this is happening?

> > > /john



Sun, 31 Jul 2005 07:09:06 GMT  
 WebRequest doesn't always return all of the data
At First, I couldn't get the code to work at all. So, I decided to do some
snooping. I found some C# Code which looked promising and converted it to
VB.Net.
Worked like a champ. I put your code inbetween the lines. Its commented out.
Should give you something to compare to.

        Dim lcUrl As String = "http://www.Yahoo.com"

        'req = WebRequest.Create("http://www.somedomain.com")
        Dim lohttp As HttpWebRequest = req.Create(lcUrl)

        lohttp.Timeout = 10000
        'req.Timeout = 2000

        lohttp.UserAgent = "Code Sample Web Client"
        Dim loWebResponse As HttpWebResponse = lohttp.GetResponse()

        'Try
        'result = req.GetResponse()
        ReceiveStream = result.GetResponseStream()
        'Catch ex As Exception
        'End Try

        Dim enc As Encoding = Encoding.GetEncoding(1252)
        'encode = System.Text.Encoding.GetEncoding("UTF-7")
        Dim loResponseStream As StreamReader = New
StreamReader(loWebResponse.GetResponseStream(), enc)
        'sr = New StreamReader(ReceiveStream, encode)

        'Dim read(32767) As Char
        Dim lcHtml As String = loResponseStream.ReadToEnd()
        'sr.Read(read, 0, 32767)
        'Dim s As String = New String(read, 0, 32767)
        'TextBox.Text = s
        'Label.Text = Len(TextBox.Text)

        MsgBox(lcHtml.ToString())

        loWebResponse.Close()
        loResponseStream.Close()

hth


Quote:
> Hi!

> It would be nice if you could also post the actual datafile that is being
> corrupted.

> I noticed from your code below, that you are just doing one read for 32767
> characters. The problem with this is that you are assuming that all the
data
> fits in 32767 characters. That might not be true in your scenario.

> A better way to do this would be: (C#)

> MemoryStream ms = new MemoryStream();

> Stream rs = resp.GetResponseStream();
> byte [] buffer = new byte[1024];
> int read = rs.Read(buffer,0,buffer.Length);
> while(read > 0) {
>     ms.Write(buffer,0,read);
>     read = rs.Read(buffer,0,buffer.Length);
> }

> rs.Close();

> ms.Seek(0,SeekOrigin.Begin);
> Encoding encoding = Encoding.UTF7;
> StreamReader sr = new StreamReader(ms, encoding);

> string s = sr.ReadToEnd();
> --
> Remove "user" from the email address to reply to the author.

> This posting is provided "AS IS" with no warranties, and confers no rights

> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cpyright.htm



> > Imports System
> > Imports System.Net
> > Imports System.IO
> > Imports System.Text
> > Imports System.Text.RegularExpressions

> > Public Class Form1

> > Inherits System.Windows.Forms.Form
> > Private Const MAX_LEVEL = 5
> > Dim result As HttpWebResponse
> > Dim req As HttpWebRequest
> > Dim ReceiveStream As Stream
> > Dim encode As Encoding
> > Dim sr As StreamReader
> > Private Sub GetPage()
> > req = WebRequest.Create("http://www.somedomain.com")
> > req.Timeout = 2000
> > Try
> > result = req.GetResponse()
> > Catch ex As Exception
> > End Try
> > ReceiveStream = result.GetResponseStream()
> > encode = System.Text.Encoding.GetEncoding("UTF-7")
> > sr = New StreamReader(ReceiveStream, encode)
> > Dim read(32767) As Char
> > sr.Read(read, 0, 32767)
> > Dim s As String = New String(read, 0, 32767)
> > TextBox.Text = s
> > Label.Text = Len(TextBox.Text)
> > result.Close()
> > ReceiveStream.Close()
> > End Sub

> > End Class



> > > Got code?



> > > > Hi!

> > > > The HttpWebReqest doesn't always return the whole page I'm
requesting.
> > > > Sometimes it returns just a few hundered characters of a page that's
> > > really
> > > > 24000 chars long. Then I try again, and it returns perhaps 16000
> > > characters,
> > > > or maybe just 2000 characters.

> > > > My code can be found in a different post I did the other day, with
the
> > > > subject "WebRequests keep timing out".

> > > > Anyone knows why this is happening?

> > > > /john



Sun, 31 Jul 2005 10:34:45 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Adding CookieCollection to WebRequest doesn't seem to work

2. workspaces(0).databases(0) doesn't always work

3. acCmdPrint doesn't always work

4. Why doesn't Access always add properly?

5. dbEngine (0)(0) doesn't always work in 7.0

6. Item_Sent event doesn't always fire??

7. VBScript Item_Open event doesn't always fire?

8. Edit links command doesn't always work

9. select query doesn't always work

10. App doesn't always start in debugger

11. Why doesn't refresh always refresh?

12. VB6 Mask Doesn't Always Mask

 

 
Powered by phpBB® Forum Software