我对 C++ 套接字 (Linux) 创建的 HTTP 请求有一个问题。我需要从 API 获取一些信息。
#include <iostream>
#include <ctype.h>
#include <cstring>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int sock;
struct sockaddr_in client;
int PORT = 80;
int main(int argc, char const *argv[])
{
struct hostent * host = gethostbyname("api.themoviedb.org");
if ( (host == NULL) || (host->h_addr == NULL) ) {
cout << "Error retrieving DNS information." << endl;
exit(1);
}
bzero(&client, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons( PORT );
memcpy(&client.sin_addr, host->h_addr, host->h_length);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "Error creating socket." << endl;
exit(1);
}
if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {
close(sock);
cout << "Could not connect" << endl;
exit(1);
}
stringstream ss;
ss << "GET /3/movie/" << 550 << "?api_key=xxx HTTP/1.1\r\n"
<< "Host: api.themoviedb.org\r\n"
<< "Accept: application/json\r\n"
<< "\r\n\r\n";
string request = ss.str();
if (send(sock, request.c_str(), request.length(), 0) != (int)request.length()) {
cout << "Error sending request." << endl;
exit(1);
}
char cur;
while ( read(sock, &cur, 1) > 0 ) {
cout << cur;
}
return 0;
}
但问题是时间太长了。它开始向控制台写入响应,但在 9/10 结束,大约需要 30 秒才能结束。当我尝试从以下位置更改循环时:
cout << cur;
至:
cout << cur << endl;
然后它会写一个完整的结果,但之后程序会滞后一段时间。这段代码有什么问题?当我尝试从终端获得经典 curl 的响应时,一切正常。谢谢你的帮助