带套接字的GET请求返回损坏的数据

时间:2015-03-07 10:08:48

标签: c# sockets

套接字有问题。我使用以下代码发出GET请求:

using (s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
    s.Connect(host, 80);
    string requestS =
    "GET http://" + adress + " HTTP/1.1\r\n" +
    "Host: " + host + "\r\n" +
    "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0\r\n" +
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
    "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\r\n" +
    "Accept-Encoding: gzip, deflate\r\n" +
    "Connection: close\r\n\r\n";
    Byte[] bytesSent = Encoding.UTF8.GetBytes(requestS);
    Byte[] bytesReceived = new Byte[1000];
    s.Send(bytesSent, bytesSent.Length, 0);
    int bytes = 0;

    do
    {
        bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
        page2 += Encoding.UTF8.GetString(bytesReceived, 0, bytes);
        m.Write(bytesReceived, 0, bytes);
    }
    while (bytes > 0);

}

以下是我从远程服务器获得的信息:

HTTP/1.1 301 Moved Permanently
Date: Sat, 07 Mar 2015 10:02:48 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
X-Pingback: http://www.example.com/blog/xmlrpc.php
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Location: http://www.example.comhttp/www.example.com/somescript.php
Content-Length: 3
Connection: close
Content-Type: text/html; charset=UTF-8

如您所见,位置数据已损坏,我不知道如何解决此问题。

1 个答案:

答案 0 :(得分:0)

我在这里看不到会引起这种情况的任何事情。破坏的UTF-8处理(如评论中所述)不会发挥此错误,因为这里的所有字符都是ASCII。

问题出在其他地方。使用Fiddler确保Location标头未被无效发送。或者,发布可执行的repro代码。现在,这就是可以诊断的所有内容。