2015-08-19 17 views
0

我制作了一个简单的网络浏览器,并且我可以在页面www.google.com/?gws_rd=ssl上连接到Google,并且获取了所有http像Cache-Control:信息,Content-Type:等,但这些东西后,最后一部分出来作为垃圾,这里是它给了我:在winsock中用C++编写了一个套接字,并且连接到google,并且在响应结束时获得一些垃圾

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▬ÿ≈╨▓ [email protected]╠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(); 
} 
+0

内容采用gzip压缩,内容编码为:gzip标头。 –

+1

当你解决这个问题时,你不会使用'recv()'返回的计数。它应该是'printf(“从服务器:%。* s \ n”,iResult,recvbuf)'。 – EJP

回答

3

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

在这种情况下,内容已被gzip压缩,所以您需要在打印之前对其进行解压缩。

最简单的解决方法是删除Accept-Encoding标头。

+0

取出gzip编码牺牲性能或其他类型的功能? –

+0

这只是意味着更多的互联网流量。 –

+0

非常感谢! :) –

相关问题