.NET Framework Bookmark and Share   
 index > Network Class Library (System.Net) > Very strange HTTP behaviour
 

Very strange HTTP behaviour

I'm currently testing the following code:
public int GetData(byte[] buffer)
        {

            //vars
            byte[] bufferWrite = new byte[2048];
            byte[] bufferRead;
            ASCIIEncoding encoder = new ASCIIEncoding();
            int bytesRead = 0;
            int totalBytes = 0;

            //write request
            bufferWrite = encoder.GetBytes("GET / HTTP/1.1\r\n"
                + "User-Agent: Meh request!\r\n"
                + "Host: www.meh.pt\r\n"
                + "Connection: Close\r\n"
                + "\r\n");

            clientStream.Write(bufferWrite, 0, bufferWrite.GetLength(0));

            //Get response
            do
            {
                bufferRead = new byte[1024];
                bytesRead = clientStream.Read(bufferRead, 0, 1024);
                totalBytes += bytesRead;
                bufferRead.CopyTo(buffer, totalBytes);

            } while (bytesRead > 0);

            return totalBytes;



I'm running the method several times in a row and checking the results with a packet sniffer. The first time I send the request, it works just fine: the request is sent and the server answers. Here's the output from the package sniffer:

80.208.124.97 74.125.45.100 HTTP GET / HTTP/1.1
74.125.45.100 80.208.124.97 TCP http > iwec [ACK] Seq=1 Ack=82 Win=5720 Len=0

As you can see, I send the request, then the server acknowledges and sends the data. The sencond time I run the method, the exact same request is send, but the server just resets the connection on me and sends no data:

80.208.124.97 74.125.45.100 HTTP GET / HTTP/1.1
74.125.45.100 80.208.124.97 TCP http > iwec [RST] Seq=5431 Win=0 Len=0

Then, as I run it again, it throws an exception saying that the connection has been closed:

"Unable to write data to the transport connection: An established connection was aborted by the software in your host machine."

I'm going crazy with this, I see no reason why the server would close the connection on me O_o This example was with google.com, but it happens with all web servers. Any help would be greatly appreciated!
jr_ferreira
Are you reusing the same TCP connection between requests? if so, this behavior is by design, because you are asking the server to close the connection (by adding the "connection: close" header in your request). So, after it responds to the 1st request, the server will close the connection. And if you insist on using the closed connection next time, the server is going to send a RST packet. hope that helps.
feroze
--
My blog
  • Proposed As Answer byFeroze Daud Tuesday, September 22, 2009 5:45 PM
  •  
Feroze Daud

You can use google to search for other answers

Custom Search

More Threads

• Link error LNK2028 in TCP/IP routines
• Connect to Gateway server directly?
• SMTP Host Credentials from web.config
• System.Net.WebPermission Error
• How can I encryption in 128 bit file Upload and Download from Server...?
• SSL wiht TcpClient
• Listening to a port and storing the stream in the database in vb.net or C#
• How can i do a POST request faster (httpWebRequest, VB.NET)
• Asynchronous System.Net.Sockets.UdpClient problem
• Sending signed and encrypted S/MIME (PKCS 7) mail with .net