C++HTTP GET请求问题
C++ HTTP GET request problems?
我编写了一个程序,它将tcp请求发送到命令行中指定的web地址,并打印响应。当我把这个请求发送到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: %dn", 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: %dn", 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(): %ldn", 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.1rn";
char recvbuf[DEFAULT_BUFLEN];
//Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if(iResult == SOCKET_ERROR){
printf("send failed: %dn", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ldn", 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: %dn", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if(iResult > 0){
printf("Bytes received: %dn", iResult);
printf(recvbuf);
printf("nn");
} else if(iResult == 0){
printf("Connection closedn");
} else {
printf("recv failed: %dn", WSAGetLastError());
}
} while(iResult > 0);
//cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
提前谢谢。
一个可能的原因是服务器在发送响应之前需要更多的数据。
char *sendbuf = "GET /index.html HTTP/1.1rn";
这应该是
char *sendbuf = "GET /index.html HTTP/1.1rnrn";
以便告诉服务器不要期望更多的HTTP标头(每个标头后面跟着一个rn
,然后添加一个额外的rn
来结束请求)。
基本上,您的HTTP请求是不完整的。必须至少再提供一个标头(Host
)。请注意,无论如何,服务器都可能(错误地)接受不完整的请求。如果您想简单启动,请复制浏览器发送的请求(例如,打开web调试器并查看传出的网络请求)。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 发送一个带有libcurl C++问题的帖子请求:s
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 如何解决来自 arduino 的 post 请求的问题,在失败之前只收到 2-3 次
- 在Visual Studio中使用Libcurl发出HTTPS请求时遇到问题
- 如何理解C++常见问题中的"程序忽略我的输入请求"?
- C++HTTP GET请求问题
- QT 5.1.1 - 在 post 请求中设置 urlQuery 时出现问题