非常奇怪的HTTP行为 [英] Very strange HTTP behaviour

查看:111
本文介绍了非常奇怪的HTTP行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在测试以下代码:

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;




我连续几次运行该方法并使用数据包嗅探器检查结果。我第一次发送请求时,它工作得很好:发送请求并且服务器回答。这是包嗅探器的输出:

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

如您所见,我发送请求,然后服务器确认并发送数据。我运行该方法的第二个时间,发送完全相同的请求,但服务器只是重置我的连接并且不发送任何数据:

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

然后,当我再次运行它时,它会抛出一个异常,说连接已经关闭:

" ;无法将数据写入传输连接:已建立的连接被主机中的软件中止。"
我对此感到很疯狂,我认为没有理由关闭服务器关于我的连接O_o此示例与google.com有关,但它适用于所有Web服务器。任何帮助将不胜感激!



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!

推荐答案

您是否在请求之间重用相同的TCP连接?如果是这样,这种行为是设计使然,因为您要求服务器关闭连接(通过在请求中添加"connection:close"标头)。因此,在响应第一个请求后,服务器将关闭连接。如果您下次坚持使用已关闭的连接,服务器将发送RST数据包。

希望有所帮助。
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.


这篇关于非常奇怪的HTTP行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆