Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2004

Tip: Looking for answers? Try searching our database.

Why can IE return a web page but TcpClient cannot?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johann Blake - 27 Oct 2004 10:38 GMT
Hi,

I have come across a rather bizarre problem using the TcpClient to
retrieve a web page. I use the TcpClient in conjunction with a
StreamWriter to write a HTTP request to the web site. The web server
returns either a 301, 302 or 404 code indicating that the page could
not be found or has been redirected. If I issue the same URL using
Internet Explorer (IE), it does retrieve the page.

I then used a HTTP sniffer program to see what was really going out on
the line. The sniffer shows exactly the same HTTP headers being sent
out using either my program or IE. They are exact in both
case-sensitivity, size and content. I can't figure this out. I have
thought that maybe the HTTP request being sent out to the web server
is too slow. I used the Flush method, set the linguiring state and
tried a number of other things, but the web server always comes back
with 404. Sometimes it even comes back with a 301 or 302. Yet IE is
consistently able to download the file.

I've deleted cookies and the cache but it doesn't help.

What gives??

Here is a partial amount of my code:

----------------------------------------------

TcpClient tcp = new TcpClient();
tcp.Connect(cnn.law.printthis.clickability.com, 80);

-- Tried the following to no avail.
// tcp.NoDelay = true;
// LingerOption  tcpClientLingerOption = new LingerOption(false,0);
// tcp.LingerState = tcpClientLingerOption;

NetworkStream networkStream = tcp.GetStream();
StreamWriter streamWriter = new StreamWriter(networkStream);

-- Tried the following to no avail.
// streamWriter.AutoFlush = true;

// Note: there are no line breaks if you are viewing this as posted in
a newsgroup (Google, etc).
string line =  @"GET
/pt/cpt?action=cpt&title=CNN.com+-+Malvo+gets+life+sentence+in%A0sniper+killing+-+Oct+26%2C+2004&expire=-1&urlID=12081313&fb=Y&url=http%3A%2F%2Fwww.cnn.com%2F2004%2FLAW%2F10%2F26%2Fmalvo.plea%2Findex.html&partnerID=2013
HTTP/1.1" + "\r\n" +
       @"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-powerpoint,
application/vnd.ms-excel, application/msword, */*"  + "\r\n" +
       @"Accept-Language: en-us" + "\r\n" +
       @"Accept-Encoding: gzip, deflate" + "\r\n" +
       @"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.0; .NET CLR 1.1.4322)" + "\r\n" +
       @"Host: cnn.law.printthis.clickability.com" + "\r\n" +
       @"Connection: Keep-Alive" + "\r\n\r\n";

streamWriter.Write(line);
streamWriter.Flush();
// Even tried using a StreamReader instead of a NetworkStream, but to
no avail.
int bytesRead = networkStream.Read(buffer, 0, bytesToRead);
string packet = Encoding.GetEncoding(1252).GetString(buffer, 0,
bytesRead);
streamWriter.Close();

Thanks,
Johann Blake
Robert Jordan - 27 Oct 2004 10:46 GMT
Hi Johann,

> Hi,
>
[quoted text clipped - 4 lines]
> not be found or has been redirected. If I issue the same URL using
> Internet Explorer (IE), it does retrieve the page.

301 and 302 are redirects. You have to follew them to
get the right page.

But why on earth don't you use the WebClient oder HttpWebRequest??

bye
Rob

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.