Getting HTTP data from server

N

Nuno Magalhaes

Hello,

I've got a small application that tries to read HTML data without the
HTTPWebResponse and Request methods and I guess the server is closing
the connection after a while.

Here is my sample code:
----------------------------------------------------
IPHostEntry iphe=Dns.GetHostByName("www.google.com");
string ipString=iphe.AddressList[0].ToString();
TcpClient tcpClient=new TcpClient(ipString,80);
NetworkStream ns=tcpClient.GetStream();
StreamReader sr=new StreamReader(ns);
StreamWriter sw=new StreamWriter(ns);
sw.WriteLine("GET /\r\n\r\n");
string serverHtml=sr.ReadLine();
----------------------------------------------------
The problem is that sr.ReadLine() returns null after a while... since
I'm trying to access an "object null reference". Is there any problems
in the code above?
I tried also:
sw.WriteLine("GET /\n\n");
sw.WriteLine("GET / HTTP/1.1\r\nConnection: Keep-Alive\n\n");
sw.WriteLine("GET / HTTP/1.1\nConnection: Keep-Alive\n");

No results. The server never responds. Can anyone tell me what I'm
doing wrong?

Thanks in advance,
Nuno Magalhaes.
 
J

Joerg Jooss

Nuno said:
Hello,

I've got a small application that tries to read HTML data without the
HTTPWebResponse and Request methods and I guess the server is closing
the connection after a while.

Here is my sample code:
----------------------------------------------------
IPHostEntry iphe=Dns.GetHostByName("www.google.com");
string ipString=iphe.AddressList[0].ToString();
TcpClient tcpClient=new TcpClient(ipString,80);
NetworkStream ns=tcpClient.GetStream();
StreamReader sr=new StreamReader(ns);
StreamWriter sw=new StreamWriter(ns);
sw.WriteLine("GET /\r\n\r\n");
string serverHtml=sr.ReadLine();
----------------------------------------------------
The problem is that sr.ReadLine() returns null after a while... since
I'm trying to access an "object null reference". Is there any problems
in the code above?
I tried also:
sw.WriteLine("GET /\n\n");
sw.WriteLine("GET / HTTP/1.1\r\nConnection: Keep-Alive\n\n");
sw.WriteLine("GET / HTTP/1.1\nConnection: Keep-Alive\n");

No results. The server never responds. Can anyone tell me what I'm
doing wrong?

None of your HTTP requests is valid. Why don't you use
System.Net.WebClient for such mundane tasks?

Cheers,
 
N

Nuno Magalhaes

Because WebClient doesn't allow me to time certain parameters of QoS
such as: Time to resolve DNS, Time to Connect, Time to Receive Data,
between others...

Any hints on how can I know if a web page is completly read? Since the
stream or the socket.Available sometimes is 0. May be busy with another
pages and I have to wait for data with a message box between send and
receive. I don't know when a page is completly read since I don't get
the Content-Length parameter in the HTTP response header. Maybe I'm not
sending all the parameters to the server also.

Here's my sample code:
****************************************************
-socket.Connect(endpoint);
-byte[] msg=Encoding.UTF8.GetBytes("GET / HTTP/1.1\n\n");
byte[] bytes=new byte[65536];
int i=socket.Send(msg,0,msg.Length,SocketFlags.None);
MessageBox.Show("Sent "+i.ToString()+" bytes. Available:
"+socket.Available.ToString()+" bytes.");
socket.Receive(bytes,0,socket.Available,SocketFlags.None);
TrafficLogTextBox.Text+=Encoding.UTF8.GetString(bytes);
TrafficLogTextBox.Text+="\r\n";
MessageBox.Show(Encoding.UTF8.GetString(bytes));
 
B

Benny Raymond

Wouldn't hurt to flush after writing data to the socket - 9 times out of
10 this is what keeps me going nutz until i remember to flush...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top