473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

HttpWebRequest - data is truncated?

I am looking for some help about the method below which partly works.
The problem is that the data returned is truncated in that the
returned data has missing bytes near the beginning of the data. I am
using VS 2005 and C#.

Has anyone got any ideas?

public string getWebPage(string url)
{
// add error handling
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de-
DE;"+
"rv:1.7.5) Gecko/20041108 Firefox/1.0";
request.Timeout=20000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader myTextReader = new StreamReader(responseStream);
char[] strBuffer = new char[25];
myTextReader.ReadBlock(strBuffer,0,25);
string stringBuffer = new string(strBuffer);
if (stringBuffer.IndexOf("GIF8")>-1 ||
stringBuffer.IndexOf("JFIF")>-1)
{
// image found
return "image";
}
else
{
// this is a text page
}
string tempString = null;
int count = 0;
do
{
count = responseStream.Read(buf,0,buf.Length);
if (count!=0)
{
tempString=Encoding.ASCII.GetString(buf,0,count);
sb.Append(tempString);
}
}while (count>0);
}
catch (WebException e)
{
return "ZenoBot getWebPage web failed to fetch "+e.ToString();;
}
catch (Exception e)
{
return "ZenoBot getWebPage web failed to fetch "+e.ToString();;
}

return sb.ToString();
}
}
Jun 27 '08 #1
2 3565
Logician <sa***@logicians.comwrote:
I am looking for some help about the method below which partly works.
The problem is that the data returned is truncated in that the
returned data has missing bytes near the beginning of the data. I am
using VS 2005 and C#.
The StreamReader is probably buffering data - reading more than 25
bytes and then saving it in its buffer.

Instead of using a StreamReader, just use the Stream directly to start
with. You're interested in the bytes which represent "GIF8", not the
actual *text* "GIF8", right?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #2
On Apr 24, 10:15*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Logician <sa...@logicians.comwrote:
I am looking for some help about the method below which partly works.
The problem is that the data returned is truncated in that the
returned data has missing bytes near the beginning of the data. I am
using VS 2005 and C#.

The StreamReader is probably buffering data - reading more than 25
bytes and then saving it in its buffer.

Instead of using a StreamReader, just use the Stream directly to start
with. You're interested in the bytes which represent "GIF8", not the
actual *text* "GIF8", right?

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet* Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk
Yes the 25 bytes was the issue. I saw code at
http://weblogs.asp.net/guys/archive/...21/414118.aspx which
helped a lot.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
9
by: Mike Cronin via DotNetMonster.com | last post by:
Hi there, Can anyone tell me what level of encryption is used when making an HTTPS POST request through an instance of the System.Net.HttpWebRequest object? Thanks much in advance! Mike...
5
by: uthuras | last post by:
Machine : AIX 5.2 Product : UDB DB2 Release 8.1 FP4a I have problem loading data into destination table. The data file is huge with more than 6 Million records. This what i have done 1....
0
by: boxboy | last post by:
Hi, I'm writing a console application and am having a problem with HttpWebRequest when posting data to a webserver. A "System.Net.WebException: The server committed a protocol violation" is always...
6
by: James MA | last post by:
I'm now writing a small program to communicate a web server to simulate a web client. I use te httpwebrequest to talk with the server, and it works find for "POST" method, however, when i test...
0
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may...
0
by: joshblair | last post by:
Hello, I am trying to post XML documents to a third party using the HttpWebRequest. This URL uses HTTPS (SSL) but I don't have a client certificate to deal with. Apparently they are using...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.