473,785 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TCPClient in HTTp Request

jin
hi, i'm trying using the tcpClient to get a html file from net, instead of
using WebClient or WebRequest,

the main part of the source code is like this:

private void tcpconnect()
{
tcp=new TcpClient("www. yahoo.com",80);
tcp.NoDelay=fal se;
tcp.ReceiveTime out=60000;
tcp.ReceiveBuff erSize=25000;
stream = tcp.GetStream() ;
byte[] send = Encoding.ASCII. GetBytes("GET /index.html HTTP/1.0\r\n\r\n");
stream.Write(se nd,0,send.Lengt h);

byte[] receive = new byte[tcp.ReceiveBuff erSize];

int lastreceive=str eam.Read(receiv e,0,tcp.Receive BufferSize);
string str = Encoding.ASCII. GetString(recei ve,0,tcp.Receiv eBufferSize);

textBox1.Text=s tr;
tcp.Close();
stream.Close();
}

but the problem is, while i try to run this function, it didn't read all the
html source code for me, but just a part, i try to run twice of the read
method in the function, and it does continue reading for me. I think there
might be some other function which allow us to check whether the html file is
finished loading or not, but i'm dunno which is it and i can't find it
through the msdn library. Is that anyone could help?

Thank for all
Nov 17 '05 #1
15 13019
The TcpClient is only a communications protocol. It knows nothing of
HTML or even when something has been entirely read. It's up to your app
to figure that out.

To determine whether you have read in all of the HTML, either the
remote server will close the TCP session, in which case you can check
some flag for that (can't remember which one) or if the connection is
not closed, it is implied that you have read all of the data when you
encounter an "</HTML> tag.

Best Regards
Johann Blake

Nov 17 '05 #2
Have you tried reading form a smaller webpage on a different site?
Also try increasing the buffer. Using a tool that reads you packets is
also helpful, such as MS Fiddler. There are others available as well.

Brett

Nov 17 '05 #3
Hi,

and why are you doing this?

you will need to implement the HTTP protocol.

In your case I think that the problem is in the receiving end, what if you
change it like:

StreamReader reader = new StreamReader( stream );

string line;
while ( (line=reader.Re adLine())!=null )
Console.Write( line );

reader.Close();

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"jin" <ji*@discussion s.microsoft.com > wrote in message
news:AB******** *************** ***********@mic rosoft.com...
hi, i'm trying using the tcpClient to get a html file from net, instead of
using WebClient or WebRequest,

the main part of the source code is like this:

private void tcpconnect()
{
tcp=new TcpClient("www. yahoo.com",80);
tcp.NoDelay=fal se;
tcp.ReceiveTime out=60000;
tcp.ReceiveBuff erSize=25000;
stream = tcp.GetStream() ;
byte[] send = Encoding.ASCII. GetBytes("GET /index.html HTTP/1.0\r\n\r\n");
stream.Write(se nd,0,send.Lengt h);

byte[] receive = new byte[tcp.ReceiveBuff erSize];

int lastreceive=str eam.Read(receiv e,0,tcp.Receive BufferSize);
string str = Encoding.ASCII. GetString(recei ve,0,tcp.Receiv eBufferSize);

textBox1.Text=s tr;
tcp.Close();
stream.Close();
}

but the problem is, while i try to run this function, it didn't read all
the
html source code for me, but just a part, i try to run twice of the read
method in the function, and it does continue reading for me. I think there
might be some other function which allow us to check whether the html file
is
finished loading or not, but i'm dunno which is it and i can't find it
through the msdn library. Is that anyone could help?

Thank for all

Nov 17 '05 #4
jin wrote:
hi, i'm trying using the tcpClient to get a html file from net,
instead of using WebClient or WebRequest,

the main part of the source code is like this:

private void tcpconnect()
{
tcp=new TcpClient("www. yahoo.com",80);
tcp.NoDelay=fal se;
tcp.ReceiveTime out=60000;
tcp.ReceiveBuff erSize=25000;
stream = tcp.GetStream() ;
byte[] send = Encoding.ASCII. GetBytes("GET /index.html
HTTP/1.0\r\n\r\n"); stream.Write(se nd,0,send.Lengt h);

byte[] receive = new byte[tcp.ReceiveBuff erSize];

int lastreceive=str eam.Read(receiv e,0,tcp.Receive BufferSize);
string str =
Encoding.ASCII. GetString(recei ve,0,tcp.Receiv eBufferSize);

textBox1.Text=s tr;
tcp.Close();
stream.Close();
}

but the problem is, while i try to run this function, it didn't read
all the html source code for me, but just a part, i try to run twice
of the read method in the function, and it does continue reading for
me. I think there might be some other function which allow us to
check whether the html file is finished loading or not, but i'm dunno
which is it and i can't find it through the msdn library. Is that
anyone could help?


Classical NetworkStream programming error: You just cannot expect to
read all data with a single call to Read(), regardless of the receive
buffer's size. You have to loop until Read() returns 0.
Cheers,
--
http://www.joergjooss.de
mailto:ne****** **@joergjooss.d e
Nov 17 '05 #5
=?Utf-8?B?amlu?= <ji*@discussion s.microsoft.com > wrote in
news:AB******** *************** ***********@mic rosoft.com:
hi, i'm trying using the tcpClient to get a html file from net,
instead of using WebClient or WebRequest,
Why? I would STRONGLY recommend against this. HTTP is NOT a simple protocol. Sure you might
get a simple GET to work - but then wait till the extras of the protocl kick in, chunked transfers, etc.

byte[] receive = new byte[tcp.ReceiveBuff erSize];
int
lastreceive=str eam.Read(receiv e,0,tcp.Receive BufferSize)
; string str =
Encoding.ASCII. GetString(recei ve,0,tcp.Receiv eBufferSize
);


TCP is split into packets - what you are trying will not work. Even when you solve this - you have
solved only the tip of the iceberg.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 17 '05 #6
"Johann Blake" <jo*********@ya hoo.com> wrote in news:1121852438 .817575.71720
@g44g2000cwa.go oglegroups.com:
To determine whether you have read in all of the HTML, either the
remote server will close the TCP session, in which case you can check
some flag for that (can't remember which one) or if the connection is
not closed, it is implied that you have read all of the data when you
encounter an "</HTML> tag.


No - thats totally wrong. Use the HTTP protocol - not the content. HTTP can transport binary files,
XML, and other. And in HTML whitespace and other things could follow the </HTML> tag. Stopping
the transfer at that is a horrible hack that will lead to very poor results.

HTTP 1.1 for example keeps the connection open.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 17 '05 #7
There is nothing wrong with my suggestion. I've done this many times
without any problems. He wants to read HTML using a TcpClient. There's
nothing wrong with that approach. It has its advantages and
disadvantages. The thing he wasn't aware of is that HTML has nothing to
do with the actual TcpClient protocol itself. Anything can be sent with
a TcpClient.

Johann

Nov 17 '05 #8
"Johann Blake" <jo*********@ya hoo.com> wrote in news:1121926001 .566551.261750
@g44g2000cwa.go oglegroups.com:
There is nothing wrong with my suggestion. I've done this many times
without any problems. He wants to read HTML using a TcpClient. There's
nothing wrong with that approach. It has its advantages and
Yes there is - he will have troubles later. HTTP is NOT the simple protocol it seems.

Its like using a search and replace to transform an XML document. It will work only in the short
term and break very quickly. Implementing 10% of the HTTP protocol is a hack - and very poor style
guaranteed to break quickly.

The proper solution is to use an HTTP component.
disadvantages. The thing he wasn't aware of is that HTML has nothing to
do with the actual TcpClient protocol itself. Anything can be sent with
a TcpClient.


http://www.indyproject.org/

Believe me, I know how TCP works.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Nov 17 '05 #9
> int lastreceive=str eam.Read(receiv e,0,tcp.Receive BufferSize);
string str =
Encoding.ASCII. GetString(recei ve,0,tcp.Receiv eBufferSize);

Call these functions recursively until stream.Read() returns 0 to indicate
end-of-stream.

Regards,
Wessel
Nov 17 '05 #10

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

Similar topics

1
2014
by: Johann Blake | last post by:
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...
3
2639
by: Aaron | last post by:
tcpclient socket webrequest what are each used for? I read some reference books and did some research on the internet, but I'm still confused. could someone clarify this for me? Thanks, Aaron
5
33880
by: Greg Martz | last post by:
I'd like to do the following in C# and prefer using tcpclient rather than raw sockets... Connect to a unix box Login run date +%H%M%S retrieve the response. That's it, nothing more. This shouldn't be too complicated, I thought... I have yet to find any examples of being able to do this.
10
25139
by: Abubakar | last post by:
hi, I work on a computer that is part of a network and uses proxy to connect to net. I cant connect to servers outside my proxy with simple ConnectTo code. I need to know how to make my requests go through proxy. eg, _socket = new TcpClient("http://msdn.microsoft.com", port); does not work. Thanx. Ab.
4
4475
by: WATYF1 | last post by:
Hello. I'm writing a VB.NET app to check email message counts for both POP3 and IMAP4. I'm using TCPClient to connect, and a NetworkStream to send simple commands. It's a very simple bit of code, actually... the problem is, if the user is behind a proxy, then the Connect method fails (times out). How do I get around this? I thought this would be a common issue and that there would be plenty of code out there to demonstrate how to...
3
28655
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work slowly, about 4 seconds between send and receive. In our production environment with hundreds of transactions it was truly costly. a while ago i changed de TcpClient object. Now i am using a Socket (System.Net.Sockets.Socket) object and it...
1
4194
by: Yair Nissan | last post by:
Hi all, I have an application which uses HttpWebRequest to get data from other sites. I'm trying to convert the application to use TcpClient (or Socket) since I need to bind the connection to a local endpoint for each request. Is there a component which performs all the tasks that the HttpWebRequest performs but enables me to bind each request to a local endpoint or a link to a code sample that would explain how to do so.
3
2422
by: Erjan | last post by:
Hi, I am using TcpClient to connect to a device which does not talk NetBios. The TcpClient tries first to do something with Netbios. This probably failes on a timeout and then TcpClient sets up the TCP connection. Due to this it takes about 5 secons to set up a TCP connection on a LAN. Is it possible to disable the NetBios request ? I have rewritten the program with sockets but also sockets try to
0
1370
by: sternr | last post by:
Hey, I'm using a TcpClient to create HTTP requests to my web-server (I know of HttpWebRequest, it is mandatory for me to use TcpClient.). Here's my code: TcpClient tcp = new TcpClient(SERVER_IP, SERVER_PORT); tcp.NoDelay = true; string data = "GET http://" + SERVER_IP + SERVER_PORT + "/getData?id=1
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.