当我连接到某些网站时,它给了我:
Content-Type: text/html; charset=ISO-8859-1
Connection: close
Transfer-Encoding: chunked
Date: Tue, 01 Jan 2013 18:49:53 GMT
fff8
在文件的末尾,它看起来像:
</script><!-- vBadvanced 1-3-9-4-8-0 -->
</body>
</html
1
>
0
但是,当我执行 stackoverflow.com 时,它的打印效果非常好.. 源代码中可能有额外的空白行,但没关系.. 为什么其他站点会添加数字?
我该如何解决?另外,如何将该标头与 html 本身分开?
我的代码如下:
#define _WIN32_WINNT 0x501
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
void Get(string WebPage)
{
WSADATA wsaData;
string Address;
struct addrinfo *result;
struct sockaddr_in *sockaddr_ipv4;
char Buffer[99000];
string Header = "GET / HTTP/1.1\r\n";
Header += "Host: " + WebPage + "\r\n";
Header += "Connection: close\r\n";
Header += "\r\n";
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) return;
SOCKET Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
getaddrinfo(WebPage.c_str(), NULL, NULL, &result);
if (result->ai_family == AF_INET)
{
sockaddr_ipv4 = (struct sockaddr_in *) result->ai_addr;
Address = inet_ntoa(sockaddr_ipv4->sin_addr);
}
freeaddrinfo(result);
SOCKADDR_IN SockAddr;
memset(&SockAddr, 0, sizeof(SockAddr));
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = inet_addr(Address.c_str());
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) == SOCKET_ERROR) return;
if (send(Socket, Header.c_str(), Header.size(), 0) == SOCKET_ERROR) return;
shutdown(Socket, SD_SEND);
std::string Response;
int bytes = 1;
while (bytes > 0)
{
bytes = recv(Socket, Buffer, sizeof(Buffer), 0);
Buffer[bytes] = '\0';
Response.append(Buffer, bytes);
};
closesocket(Socket);
WSACleanup();
}
int main()
{
Get("google.com");
}