473,795 Members | 2,826 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
15 13022
Wessel Troost <no*****@like.t he.sun> wrote:
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.


But an HTTP/1.1 server might *not* close the connection after sending the
response.

The OP should read the HTTP/1.1 RFC if he wants to implement an HTTP client
at this time.
Nov 17 '05 #11
> But an HTTP/1.1 server might *not* close the connection after sending the
response.
You can specify the HTTP version in your request, which would eliminate
this speculative problem?
The OP should read the HTTP/1.1 RFC if he wants to implement an HTTP
client
at this time.


To write a generic client, the OP might have to read the specification.
On the other hand, he might just want to contact a specific server, with a
specific IIS or Apache version, which behaves consistently. In which case
reading the RFC would be like shooting a mouse with a nuclear bomb.

Not that anyone could read an RFC and come up with correct code. It takes
groups of developers years to do that.

Greetings,
Wessel
Nov 17 '05 #12
"Wessel Troost" <no*****@like.t he.sun> wrote in
news:op.st9mdvq rf3yrl7@asbel:
You can specify the HTTP version in your request, which would
eliminate this speculative problem?
Only that one. It wont help you with URL encoding, Chunked transfers, or many of the other things
in HTTP 1.0.
To write a generic client, the OP might have to read the
specification. On the other hand, he might just want to contact a
specific server, with a specific IIS or Apache version, which behaves
consistently. In which case reading the RFC would be like shooting a
mouse with a nuclear bomb.
No - you've seriously understimated the task and do not understand HTTP nor the RFC's. The RFC
is the consistency. IIS and Apache both conform to it. Various user configurations, proxies and
document types will alter your "observed consistency of a single test URL".

Just because you run one test - does not meant it will always respond in that manner.
Not that anyone could read an RFC and come up with correct code. It
takes groups of developers years to do that.


Which is why you should use an ready made HTTP client and not write one unless you intend to do
it correctly. I can teach my wife how to write a Hello World - but saying "Well its consistent" and
she doesnt need the rest does not mean I can take a vacation and give her my job.

HTTP looks deceptively simple - and small test programs will work in the SHORT TERM.
However there are many more complexities to HTTP than first appear and such hacks will not
work long term.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 17 '05 #13
"Wessel Troost" <no*****@like.t he.sun> wrote in
news:op.st9mdvq rf3yrl7@asbel:
You can specify the HTTP version in your request, which would
eliminate this speculative problem?
Only that one. It wont help you with URL encoding, Chunked transfers, or many of the other things
in HTTP 1.0.
To write a generic client, the OP might have to read the
specification. On the other hand, he might just want to contact a
specific server, with a specific IIS or Apache version, which behaves
consistently. In which case reading the RFC would be like shooting a
mouse with a nuclear bomb.
No - you've seriously understimated the task and do not understand HTTP nor the RFC's. The RFC
is the consistency. IIS and Apache both conform to it. Various user configurations, proxies and
document types will alter your "observed consistency of a single test URL".

Just because you run one test - does not meant it will always respond in that manner.
Not that anyone could read an RFC and come up with correct code. It
takes groups of developers years to do that.


Which is why you should use an ready made HTTP client and not write one unless you intend to do
it correctly. I can teach my wife how to write a Hello World - but saying "Well its consistent" and
she doesnt need the rest does not mean I can take a vacation and give her my job.

HTTP looks deceptively simple - and small test programs will work in the SHORT TERM.
However there are many more complexities to HTTP than first appear and such hacks will not
work long term.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 17 '05 #14
Wessel Troost <no*****@like.t he.sun> wrote:
But an HTTP/1.1 server might *not* close the connection after sending the
response.

You can specify the HTTP version in your request, which would eliminate
this speculative problem?


Of course. My mistake.
Nov 17 '05 #15
Wessel Troost <no*****@like.t he.sun> wrote:
But an HTTP/1.1 server might *not* close the connection after sending the
response.

You can specify the HTTP version in your request, which would eliminate
this speculative problem?


Of course. My mistake.
Nov 17 '05 #16

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
33885
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
25140
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
4477
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
28656
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
4197
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
2424
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
9672
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
9519
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,...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2920
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.