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!