drogon/examples/client_example/main.cc

35 lines
1.2 KiB
C++
Raw Normal View History

2018-07-24 17:24:23 +08:00
#include <drogon/HttpAppFramework.h>
#include <trantor/net/InetAddress.h>
#include <netdb.h>
#include <iostream>
#include <future>
int main()
{
trantor::Logger::setLogLevel(trantor::Logger::TRACE);
struct hostent* he = gethostbyname("www.baidu.com");
struct in_addr addr;
if (he && he->h_addrtype == AF_INET) {
addr = *reinterpret_cast<struct in_addr*>(he->h_addr);
struct sockaddr_in ad;
bzero(&ad, sizeof ad);
ad.sin_family = AF_INET;
ad.sin_addr=addr;
trantor::InetAddress netaddr(ad);
auto client=HttpAppFramework::instance().newHttpClient(netaddr.toIp(),80);
2018-07-25 17:07:21 +08:00
auto req=HttpRequest::newHttpRequest();
2018-07-24 17:24:23 +08:00
req->setMethod(drogon::HttpRequest::kGet);
2018-07-24 18:50:25 +08:00
int count=0;
2018-07-24 17:24:23 +08:00
for(int i=0;i<10;i++)
2018-07-24 18:50:25 +08:00
client->sendRequest(req,[&](ReqResult result,const HttpResponse &response){
2018-07-24 17:24:23 +08:00
std::cout<<"receive response!"<<std::endl;
//auto headers=response.
2018-07-24 18:50:25 +08:00
count++;
2018-07-24 17:24:23 +08:00
std::cout<<response.getBody()<<std::endl;
2018-07-24 18:50:25 +08:00
std::cout<<"count="<<count<<std::endl;
2018-07-24 17:24:23 +08:00
});
HttpAppFramework::instance().run();
} else {
std::cout<<"can't find the host"<<std::endl;
}
}