CppServer
CppServer copied to clipboard
HTTP Client Hanging
Hi, I'm having an issue with the HTTP client. I've built the HTTP server and HTTP client examples, and while the server seems to work perfectly, using curl to test it, the client seems to hang whenever a Send*Request() method is called. For example:
auto response = client->SendGetRequest("/").get(). No matter what the host is, the client always hangs. If .get() is omitted, the program continues to the next line, but either way the client never actually sends a request or throws an error. Is there a workaround to this?
Here is my full file that I'm using:
#include "server/http/http_client.h"
#include "string/string_utils.h"
#include <iostream>
int main(int argc, char** argv)
{
// HTTP server address
std::string address = "127.0.0.1:8080";
std::cout << "HTTP server address: " << address << std::endl;
// Create a new Asio service
auto service = std::make_shared<CppServer::Asio::Service>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new HTTP client
auto client = std::make_shared<CppServer::HTTP::HTTPClientEx>(service, address, "http");
//send get request
std::cout << "sending message" << std::endl;
auto response = client->SendGetRequest("/").get();
std::cout << response << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
Thanks
Most of HTTP servers require and check properly filled "Host" header. Please try in your example this sample:
void Request(const std::string& url)
{
auto& req = request();
req.Clear();
req.SetBegin("GET", url);
req.SetHeader("Host", address + ":" + std::to_string(port));
req.SetBody();
auto response = SendRequest(req).get();
if (response.status() == 200)
++total_messages;
else
++total_errors;
}
Hi, Thanks for replying to my question so quickly! Unfortunately, I'm still having the same problem. The program just stops forever at the SendRequest line. Is there anything else that I could try? This is how I modified the code snippet that you sent:
auto& req = client->request();
req.Clear();
req.SetBegin("GET", "/");
req.SetHeader("Host", "127.0.0.1:8080");
req.SetBody("foo");
std::cout << "sending request..." << std::endl;
auto response = client->SendRequest(req).get();
if (response.status() == 200)
++total_messages;
else
++total_errors;
Thanks, Conrad
Hi, I'm experiencing similar client issues (be it HTTP Client or HTTPS Client). Somehow, I can reach servers running on my machine, but not ones on the web (eg. http://httpbin.org). Trying to send a basic get request to http://httpbin.org/get using the following code snippet
auto service = std::make_shared<CppServer::Asio::Service>();
CppServer::HTTP::HTTPResponse testResponse;
CppServer::HTTP::HTTPRequest testRequest;
testRequest.Clear();
testRequest.SetBegin("GET", "/get");
testRequest.SetHeader("Host", "http://httpbin.org");
testRequest.SetBody();
auto testClient = std::make_shared<CppServer::HTTP::HTTPClientEx>(service, "http://httpbin.org", "http");
testResponse.Clear();
testResponse = testClient->SendRequest(testRequest, CppCommon::Timespan::seconds(10)).get();
will, as Conrad pointed out, hang at the last line forever. Using the ipv4 address and port instead of the URL does not help either. I also wonder why the timeout does not hit. Sadly, like this, the library is not really useful.