472,139 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Getting HTTP data from server

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.

Nov 25 '05 #1
3 1779
Nuno Magalhaes wrote:
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,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 25 '05 #2
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,SocketFlag s.None);
TrafficLogTextBox.Text+=Encoding.UTF8.GetString(by tes);
TrafficLogTextBox.Text+="\r\n";
MessageBox.Show(Encoding.UTF8.GetString(bytes));

Nov 27 '05 #3
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...

Nuno Magalhaes wrote:
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.

Nov 27 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nathan Sokalski | last post: by
reply views Thread by =?Utf-8?B?cGI2NDgxNzQ=?= | last post: by
9 posts views Thread by =?Utf-8?B?UmF5?= | last post: by
33 posts views Thread by JamesB | last post: by
reply views Thread by leo001 | last post: by

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.