前段时间,我做了一个非常简单的方法,可以发出http请求并返回响应。这有点像我的预期,但现在我想再次使用它,但我突然遇到了一个我似乎无法弄清楚的问题。 每次发出请求时,都会返回错误503:服务不可用。
有时它有效,有时则不然。
我注意到如果我在连接关闭后放置一个断点并等待一点并继续,没有问题,一切正常。所以我的猜测是这与服务器的计时/延迟有关。
有没有人有这方面的经验?
提前致谢!
string HttpHelper::HttpGet(string host, string path)
{
WSADATA wsaData;
int result;
// Initialize Winsock
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0)
throw SocketError(L"WSAStartUp", result);
// Resolve the server address and port
addrinfo * pAddrInfo;
result = getaddrinfo(host.c_str(), "80", 0, &pAddrInfo);
if (result != 0)
throw SocketError(L"addrinfo", result);
//Create the socket
SOCKET sock = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, pAddrInfo->ai_protocol);
if (sock == INVALID_SOCKET)
throw SocketError(L"Socket", WSAGetLastError());
// Connect to server.
result = connect(sock, pAddrInfo->ai_addr, pAddrInfo->ai_addrlen);
if (result != 0)
throw SocketError(L"Connect", WSAGetLastError());
const string request = "GET " + path + " HTTP/1.1\nHost: " + host + "\n\n";
// Send an initial buffer
result = send(sock, request.c_str(), request.size(), 0);
if (result == SOCKET_ERROR)
throw SocketError(L"Send", WSAGetLastError());
// shutdown the connection since no more data will be sent
result = shutdown(sock, SD_SEND);
if (result == SOCKET_ERROR)
throw SocketError(L"Close send connection", WSAGetLastError());
// Receive until the peer closes the connection
string response;
char buffer[50];
int bytesRecv = 0;
for (;;)
{
result = recv(sock, buffer, sizeof(buffer), 0);
if (result == SOCKET_ERROR)
throw SocketError(L"Recv", WSAGetLastError());
if (result == 0)
break;
response += string(buffer, result);
wstringstream stream;
stream << L"HttpGet() > Bytes received: " << bytesRecv;
DebugLog::Log(stream.str(), LogType::INFO);
bytesRecv += result;
}
wstringstream stream;
stream << L"HttpGet() > Bytes received: " << bytesRecv;
DebugLog::Log(stream.str(), LogType::INFO);
// cleanup
result = closesocket(sock);
if (result == SOCKET_ERROR)
throw SocketError(L"Closesocket", WSAGetLastError());
result = WSACleanup();
if (result == SOCKET_ERROR)
throw SocketError(L"WSACleanup", WSAGetLastError());
freeaddrinfo(pAddrInfo);
DebugLog::Log(L"HttpGet() > Cleanup Successful ", LogType::INFO);
return response;
}
wstring SocketError::ErrorMessage(const wstring & context, const int errorCode) const
{
wchar_t buf[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, buf, sizeof(buf), 0);
wchar_t * newLine = wcsrchr(buf, '\r');
if (newLine) *newLine = '\0';
wstringstream stream;
stream << L"Socket error in " << context << L" (" << errorCode << L"): " << buf;
return stream.str();
}
答案 0 :(得分:3)
很难说......
这是一个简单而通用的winsock代码,您可以尝试使用此代码来查看问题是否也存在。
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
string website_HTML;
locale local;
void get_Website(string url );
char buffer[10000];
int i = 0 ;
//****************************************************
int main( void ){
get_Website("www.stackoverflow.com" );
cout<<website_HTML;
cout<<"\n\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}
//****************************************************
void get_Website(string url ){
WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
int lineCount=0;
int rowCount=0;
struct hostent *host;
string get_http;
get_http = "GET / HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
cout << "WSAStartup failed.\n";
system("pause");
//return 1;
}
Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
host = gethostbyname(url.c_str());
SockAddr.sin_port=htons(80);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
//return 1;
}
send(Socket,get_http.c_str(), strlen(get_http.c_str()),0 );
int nDataLength;
while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){
website_HTML+=buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
}
答案 1 :(得分:0)
我将假设如果您将相同的主机名放入网络浏览器,您将获得有效的内容,而不是503。
三个随机猜测:
1)您需要一个User-Agent标头。将您的请求字符串构建为:
const string request = "GET " + path + " HTTP/1.1\n"
request += "Host: " + host + "\n";
request += "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36\n";
request += "\n";
2)正如我在评论中提到的,请确保send
正在发送所有数据:
size_t sent = 0;
size_t remaining = request.size();
while (remaining > 0)
{
const char* data = request.c_str() + sent;
result = send(sock, data, remaining, 0);
if (result == SOCKET_ERROR)
throw SocketError(L"Send", WSAGetLastError());
sent += result;
remaining -= result;
}
3)唯一的另一件事可能是shutdown
电话。
只需删除以下行。从理论上讲,它不应该有所作为。但值得一试。
result = shutdown(sock, SD_SEND);
答案 2 :(得分:0)
对于Windows tcp套接字,您必须以这种格式发送它:
GET /romeo.txt HTTP/1.1\r\nHost: data.pr4e.org\r\n\r\n
其中 /romeo.txt 是主机URL的路径,而 data.pr4e.org 是主机
贷记给某人:)