在 winsock 中用 C++ 创建一个套接字并连接到 google 并在响应结束时得到一些垃圾

Made a socket in C++ in winsock and connected to google and am getting some garbage at the end of the response

我制作了一个简单的网络浏览器,我能够连接到页面 www.google.com/?gws_rd=ssl 上的 Google,并且我获得了所有 http 信息,例如 Cache-Control:Content-Type: 等但最后一部分是垃圾,这就是它给我的:

HTTP/1.1 200 OK Date: Wed, 19 Aug 2015 03:38:22 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bi
n/answer.py?hl=en&answer=151657 for more info."
Content-Encoding: gzip
Server: gws
Content-Length: 14534
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: PREF=ID=1111111111111111:FF=0:TM=1439955502:LM=1439955502:V=1:S=juTG
4tnW5tH5E6BF; expires=Fri, 18-Aug-2017 03:38:22 GMT; path=/; domain=.google.com
Set-Cookie: NID=70=glka-fZspN0pbH8M2m0Oay-dXvaHNwyMqKB-O4dn7ZgObHTbnn7dyc4cfn_uX
XKOobdvfpksNRCV5uM1QWPGXV_MjfxdvW2aQQ4zrEtfdxnZ4IxuPQFM61B_ZI0wDTsJ; expires=Thu
, 18-Feb-2016 03:38:22 GMT; path=/; domain=.google.com; HttpOnly

▼ï
Bytes received: 3316
From server: ⁿh╖6Ü╗ú»╠╟²ú}╪φ▼²h▌<¢Füg┴½ó┬k■Θµ¢←┤┼
ƒz~ßD╓$╕lw[²▬⌡G¢╕*r|!╩╓⌂╫ñäk┬ÿ.─E▬ÿ≈╨▓ W@D╠y√┐☺àY{J╙Z"╞ëf±4▄aeP↨⌐"╔S¡²C╟╜,?⌠╢|║┤
ÿ√╢φûΓå-╔╖n¢<╞3|(₧&-╖ë>aJVz╛ÖF┌§Θº`á╙⌠≤mlk·ñu
╞?╧m▄û¢ú┘♣âúΘéö>◄╣Θ╝░që└â₧ƒ┼▓┐l·Äu╛▀û½^Ä│⌠kΩò²╔╗ú▼m`Ä ⁿh▼=nπ±@╣╝²²?Γå▒æ~╒ⁿ╟Å]ΦF‼
╚╒R╒Öêß^╧╪O7ΣB?ºß∩φl█.âq)≈«(R→τå┌&«DZ¡(╘╬α◄╗ív:Fñk!}8Ü ┐╬♂E.♥k╘Q$ƒ§%

这是我的代码:

#include <windows.h>
#include <winsock2.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define SCK_VERSION2 0x0202
#define DEFAULT_BUFLEN 12000
#define DEFAULT_PORT 27015

namespace Globals{
    extern string input = "";
}
using namespace Globals;

int sck() {
    //----------------------
    // Declare and initialize variables.
    WSADATA wsaData;
    int iResult;

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;

    char name[500] = "";
    char ipADDRESS[500] = "";
    char sPORT[500] = "";

    sockaddr_in sName;
    int sNameSize =  sizeof(sName);

    char const* sendbuf = "GET /?gws_rd=ssl  HTTP/1.1\r\n"
    "Host: www.google.com:80\r\n"
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\r\n"
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    "Accept-Language: en-us,en;q=0.5\r\n"
    "Accept-Encoding: gzip,deflate\r\n"
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
    "Keep-Alive: 300\r\n"
    "Connection: keep-alive\r\n"
    "Pragma: no-cache\r\n"
    "DNT: 1"
    "Cache-Control: no-cache\r\n\r\n";
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;                                    //23.214.132.132 GoDaddy.com
    int WSAERROR = WSAGetLastError();                                   
    //system("color 04");                                               
    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
      printf("WSAStartup failed: %d\n", iResult);
      return 1;
    }

    //----------------------
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Error at socket(): %i\n", WSAGetLastError() );
        WSACleanup();
        return 1;
    }

    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.

    printf("IP ADDRESS: 74.125.196.191 is Google \n");
    cin >> ipADDRESS;
    printf("PORT: \n");
    cin >> sPORT;
    u_short PORT = strtoul(sPORT, NULL, 0);
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr(ipADDRESS);                            
    clientService.sin_port = htons(PORT); 
    //----------------------
    // Connect to server.
    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    if ( iResult == SOCKET_ERROR) {
        closesocket (ConnectSocket);
        printf("Unable to connect to server: %i\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //----------------------
    //Get local host name
    iResult = gethostname(name, sizeof(name));
    if (iResult == NO_ERROR) {
        printf("Host Name: %s\n", name);
    }
    else if (iResult == SOCKET_ERROR) {
        printf("Could not resolve host name: %i", WSAGetLastError());
    }

    //------------------------
    //Get peer name
    iResult = getpeername(ConnectSocket, (struct sockaddr*)&sName, &sNameSize);
    if (iResult == NO_ERROR)
        printf("Peer Name: %s\n", inet_ntoa(sName.sin_addr));
    else if (iResult == SOCKET_ERROR)
        printf("Could not get peer name: %i\n", WSAGetLastError());

    //-------------------------
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    else
        printf("Bytes Sent: %i\n", iResult);

    //-----------------------------
    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 ) {
            printf("Bytes received: %d\n", iResult); //printf("Bytes received: %d\n", iResult);
            printf("From server: %s\n", recvbuf);
        }
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else if (WSAERROR == WSAETIMEDOUT)
            printf("recv failed: WSAETIMEDOUT\n");
    } while( iResult > 0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();
    system("PAUSE");
    return 0;
}

int main() {
        sck();
}

你发送的 Accept-Encoding header 告诉服务器你可以接受 gzip 和 deflate 编码,这意味着你必须寻找 Content-Encoding header并相应地解码内容。

在这种情况下,内容已被 gzip 压缩,因此您需要在打印之前将其解压缩。

最简单的解决方案是删除 Accept-Encoding header。