Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用套接字从页面加载数据_C++_Sockets_Visual Studio 2013 - Fatal编程技术网

C++ 使用套接字从页面加载数据

C++ 使用套接字从页面加载数据,c++,sockets,visual-studio-2013,C++,Sockets,Visual Studio 2013,我选择不使用libcurl,因为我已经花了太多的精力让它工作。 所以我对C++是新的,并不能完全理解所有的东西。 我无法从example.com/test.php获取内容,但使用test.example.com确实有效(但必须为其创建子域,这很烦人) 除此之外,有没有办法只获取内容而不获取响应头 我的代码: string result; WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { cout &

我选择不使用libcurl,因为我已经花了太多的精力让它工作。

所以我对C++是新的,并不能完全理解所有的东西。 我无法从example.com/test.php获取内容,但使用test.example.com确实有效(但必须为其创建子域,这很烦人)

除此之外,有没有办法只获取内容而不获取响应头

我的代码:

string result;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
    cout << "WSAStartup failed.\n";
    system("pause");

}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.example.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
    cout << "Could not connect";
    system("pause");
}
cout << "Connected.\n";
send(Socket, "GET / HTTP/1.1\r\nHost: test.example.nl\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: test.example.nl\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
    int i = 0;
    while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
        result += buffer[i];
        i += 1;
    }
}
cout << result;
closesocket(Socket);
WSACleanup();
但是如果标题不一样呢?例如,它使用不同的服务器或其他东西。或更改了php版本

标题作为响应(以防这对某人来说很有趣)


每次发出请求时,标题都会更改。你不能仅仅假设它是一个固定的字节数。您必须解析标题。您需要的是:

如果你查看第39页,你会看到HTTP头以一个空行结束,所以寻找空白行,然后内容低于此。当然,如果这不仅仅是一个学术练习,你可能会想找一个为你做这件事的图书馆。HTTP相当复杂

6答复

在接收并解释请求消息后,服务器会做出响应 使用HTTP响应消息

这里是规范的摘录。您想要“消息正文”


我接受了你的答案,虽然我不太确定我的方法是否100%正确,但它还是起了作用。由于我的响应标题以一个空行结尾,这意味着后面有两个CRLF。所以我做了一个决定,如果是这样的话,它开始保存信息,只给我内容。
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
       if(i > 160) {
            result += buffer[i];
            i += 1;
        }
    }
HTTP/1.1 200 OK
Date: Fri, 13 Feb 2015 20:39:56 GMT
Server: Apache
X-Powered-By: PHP/5.3.29
Content-Length: 4
Connection: close
Content-Type: text/html
   Response      = Status-Line               ; Section 6.1
                   *(( general-header        ; Section 4.5
                    | response-header        ; Section 6.2
                    | entity-header ) CRLF)  ; Section 7.1
                   CRLF
                   [ message-body ]          ; Section 7.2