473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1868
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
2
by: David Hearn | last post by:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Description: An unhandled exception occurred during the execution of the current...
1
by: Nathan Sokalski | last post by:
I have retrieved data from a database using a SELECT statement that includes an INNER JOIN. The data seems to be retrieved to the DataSet OK, but I am having trouble getting the data from the...
1
by: eisenb | last post by:
microsoft.public.dotnet.languages.vb.upgrade isn't getting much traffic, so I'll try here. Ours is a VB6 shop exploring .NET. First I wrote a new application with VB .NET 2003 to create a pdf...
0
by: =?Utf-8?B?cGI2NDgxNzQ=?= | last post by:
We have been having this problem for months and always assumed it was because of our somewhat complicated setup. I have just spent almost all of today making this into a simple example that can be...
9
by: =?Utf-8?B?UmF5?= | last post by:
Have SQL server on one machine. Have IIS on another machine in same large intranet. Have website in IIS with Basic Authentication turned on and other options deselected. Have webpage (.aspx) with...
3
by: JohnGos | last post by:
Since around 10th May (a couple of days after the recent IE autoupdate), a web application which has worked without problem for several years has developed intermittent problems with data posted from...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.