【问题标题】:C++ HTTP GET request problems?C++ HTTP GET 请求问题?
【发布时间】:2012-07-10 04:14:43
【问题描述】:

我编写了一个程序,它向命令行中指定的网址发送 tcp 请求并打印响应。当我向 www.google.co.uk(或任何网站)发送此获取请求时,我什么也得不到:(

有人能告诉我我做错了什么,以及对谷歌的正确 GET 请求应该是什么样子。这是代码...

#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")

int main(int argc, char *argv[]){

WSADATA wsaData;
int iResult;

//Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult != 0){
    printf("WSAStartup failed: %d\n", iResult);
    return 1;
}

struct addrinfo *result = NULL,
                *ptr = NULL,
                hints;

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

#define DEFAULT_PORT "80"

//Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if(iResult != 0){
    printf("getaddrinfo failed: %d\n", iResult);
    WSACleanup();
    return 1;
}

SOCKET ConnectSocket = INVALID_SOCKET;

//Attempt to connect to the first address returned by
//the call to getaddrinfo
ptr = result;

//Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
    ptr->ai_protocol);

if(ConnectSocket == INVALID_SOCKET){
    printf("Error at socket(): %ld\n", WSAGetLastError());
    freeaddrinfo(result);
    WSACleanup();
    return 1;
}

//Connect to server
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if(iResult == SOCKET_ERROR){
    closesocket(ConnectSocket);
    ConnectSocket = INVALID_SOCKET;
}

//Should really try the next address returned by getaddrinfo
//if the connect call failed
//But for this simple example we just free the resources
//returned by getaddrinfo and print an error message

freeaddrinfo(result);

if(ConnectSocket == INVALID_SOCKET){
    printf("Unable to connect to server!\n");
    WSACleanup();
    return 1;
}

#define DEFAULT_BUFLEN 512

int recvbuflen = DEFAULT_BUFLEN;

char *sendbuf = "GET /index.html HTTP/1.1\r\n";
char recvbuf[DEFAULT_BUFLEN];

//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;
}

printf("Bytes Sent: %ld\n", iResult);

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

do {
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if(iResult > 0){
        printf("Bytes received: %d\n", iResult);
        printf(recvbuf);
        printf("\n\n");
    } else if(iResult == 0){
        printf("Connection closed\n");
    } else {
        printf("recv failed: %d\n", WSAGetLastError());
    }
} while(iResult > 0);

//cleanup
closesocket(ConnectSocket);
WSACleanup();

return 0;
 }

提前致谢。

【问题讨论】:

  • 您是否尝试过在没有请求的情况下获取页面?我只是想知道,因为您怎么知道索引页是index.html 还是index.html 甚至home.html 等等?所以我会尝试像GET HTTP/1.1 这样的请求。以及为什么在收到答案之前关闭连接?
  • “我一无所获”是什么意思?您收到 0 个字节,您的程序冻结或突然结束而没有打印任何内容!?
  • @ViteFalcon:没关系。如果 URL 无效,服务器无论如何都会发送正确的 HTTP 响应(重定向、找不到页面等)

标签: c++ http tcp winsock get-request


【解决方案1】:

一个可能的原因是服务器在发送响应之前期望更多数据。

char *sendbuf = "GET /index.html HTTP/1.1\r\n";

应该是这样的

char *sendbuf = "GET /index.html HTTP/1.1\r\n\r\n";

这样您就可以告诉服务器不要期望更多的 HTTP 标头(每个标头后跟一个 \r\n,然后添加一个额外的 \r\n 以结束请求)。

基本上,您的 HTTP 请求是不完整的。至少还有一个必须提供的标头 (Host)。请注意,服务器可能(错误地)接受和不完整的请求。如果您想从简单的开始,请复制浏览器发送的请求(例如,打开 Web 调试器并查看传出的网络请求)。

【讨论】:

  • 它会期待更多。 HTTP 1.1 请求还需要包含 Host 标头,例如:char *sendbug = "GET /index.html HTTP/1.1\r\nHost: TheServerHostNameHere\r\n\r\n";
猜你喜欢
  • 2011-06-12
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
相关资源
最近更新 更多