#include <boost/asio.hpp>

#define USING_SSL   //是否加密

#ifdef USING_SSL
#include <boost/asio/ssl.hpp>
#endif using boost::asio::ip::tcp;
using std::string; int post(const string& host, const string& port, const string& page, const string& data, string& reponse_data)
{
try
{
boost::asio::io_service io_service;
//如果io_service存在复用的情况
// if(io_service.stopped())
// io_service.reset(); // 从dns取得域名下的所有ip
tcp::resolver resolver(io_service);
tcp::resolver::query query(host, port);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); // 尝试连接到其中的某个ip直到成功
#ifdef USING_SSL
boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
context.set_default_verify_paths();
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(io_service, context);
socket.set_verify_mode(boost::asio::ssl::context::verify_none);
boost::asio::connect(socket.lowest_layer(), endpoint_iterator);
socket.handshake(boost::asio::ssl::stream_base::client);
#else
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
#endif // Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request); request_stream << "POST " << page << " HTTP/1.1\r\n";
request_stream << "Host: " << host << "\r\n";
request_stream << "Content-Length: " << data.length() << "\r\n";
// Content-Type: 视实际情况修改,or“application/octet-stream”
request_stream << "Content-Type: application/json; charset=UTF-8\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
request_stream << data; // Send the request.
boost::asio::write(socket, request); // Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf constructor.
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n"); // Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
reponse_data = "Invalid response";
return -2;
}
// 如果服务器返回非200都认为有错,不支持301/302等跳转
if (status_code != 200)
{
std::stringstream oss;
oss << "Response returned with status code" << " : " << status_code;
reponse_data = oss.str();
return -3;
} // 传说中的包头可以读下来了
std::string header;
std::vector<string> headers;
// string content_length_header("Content-Length: ");
// size_t content_length = 0;
while (std::getline(response_stream, header) && header != "\r")
{
/* if(string::npos != header.find(content_length_header))
{
string tmp(header.substr(content_length_header.size()));
stringstream ss;
ss << tmp;
ss >> content_length;
}*/
headers.push_back(header);
//std::cout << header << std::endl;
} // 读取所有剩下的数据作为包体
boost::system::error_code error;
while (boost::asio::read(socket, response,
boost::asio::transfer_at_least(1), error))
{
} // 返回值为eof,认为是正确的
if (error != boost::asio::error::eof && error.value() != 0x140000db)
{
std::stringstream oss;
oss << error.value() << " : " << error.message();
reponse_data = oss.str();
return -4;
} //响应有数据
if (response.size())
{
std::istream response_stream(&response);
std::istreambuf_iterator<char> eos;
reponse_data = string(std::istreambuf_iterator<char>(response_stream), eos);
}
}
catch(std::exception& e)
{
reponse_data = e.what();
return -1;
}
return 0;
}

 

参考资料:

跨平台c++/boost/asio 简单的HTTP POST请求 客户端模型
http://www.cnblogs.com/linbc/p/5034286.html

C++ Post/Get请求(Boost.Asio库)
https://blog.csdn.net/csnd_ayo/article/details/64437935

boost::asio ssl

https://blog.csdn.net/aalbertini/article/details/38300757

基于boost asio实现的支持ssl的通用socket框架

https://www.xuebuyuan.com/2178001.html

 

使用boost.asio实现网络通讯的更多相关文章

  1. Boost.Asio c++ 网络编程翻译(14)

    保持活动 假如,你须要做以下的操作: io_service service; ip::tcp::socket sock(service); char buff[512]; ... read(sock, ...

  2. Boost.Asio c++ 网络编程翻译(26)

    Boost.Asio-其他特性 这章我们讲了解一些Boost.Asio不那么为人所知的特性.标准的stream和streambuf对象有时候会更难用一些,但正如你所见.它们也有它们的益处.最后,你会看 ...

  3. asio的网络通讯代码练手

    asio的网络基本模板(单例模式 消息队列 ) // MyAsio.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include < ...

  4. Boost.Asio c++ 网络编程翻译(11)

    *_at方法 这些方法在一个流上面做随机存取操作.你来指定read和write操作从什么地方開始(offset): async_read_at(stream, offset, buffer [, co ...

  5. Boost.Asio c++ 网络编程翻译(20)

    异步服务端 这个图表是相当复杂的:从Boost.Asio出来你能够看到4个箭头指向on_accept.on_read,on_write和on_check_ping. 着也就意味着你永远不知道哪个异步调 ...

  6. Boost.Asio c++ 网络编程翻译(21)

    同步VS异步 Boost.Asio的作者做了一个非常惊艳的工作:它能够让你在同步和异步中自由选择,从而更好的适应你的应用. 在之前的章节中,我们学习了每种类型应用的框架,比方同步client,同步服务 ...

  7. Boost.Asio c++ 网络编程翻译(16)

    TCP异步服务端 核心功能和同步服务端的功能类似,例如以下: class talk_to_client : public boost::enable_shared_from_this<talk_ ...

  8. Boost.Asio c++ 网络编程翻译(10)

    read/write方法 这些方法对一个流进行读写操作(能够是套接字,或者其它表现的像流的类): async_read(stream, buffer [, completion],handler):这 ...

  9. Boost.Asio c++ 网络编程翻译(18)

    同步服务端 同步服务端也相当简单.它须要两个线程,一个负责接收新的client.另外一个负责处理已经存在的client. 它不能使用单线程:等带一个新的client是一个堵塞操作,所以我们须要另外一个 ...

随机推荐

  1. Lovable eccentric

    It took him four years to stage this elaborate joke simply to prove that critics do not always know ...

  2. ZT 3.1 依赖倒置原则的定义

    设计模式精解-GoF 23 种设计模式解析附 C++实现源码http://www.mscenter.edu.cn/blog/k_eckelP58 Template 模式获得一种反向控制结构效果,这也是 ...

  3. JavaScript函数的声明与调用方式

    入职第一天小记 对于初入前端的程序猿来说,对于函数的理解与使用可谓是相当浅薄的,回顾这自己近几年的工作以及学习经历,准备对JavaScript来个系统的总结. 如果要我们对H5中的表单做个简单的校验, ...

  4. Expression拼接

    public static class PBuilder { /// <summary> /// 机关函数应用True时:单个AND有效,多个AND有效:单个OR无效,多个OR无效:混应时 ...

  5. ios 线程同步

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/ThreadSafe ...

  6. BZOJ3238:[AHOI2013]差异(SAM)

    Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...

  7. [POI2015]KIN

    题目 感觉这种题好套路啊,怎么又是这个做法 癌不过怎么没有人和我一样些写套路做法,那干脆来写个题解吧 我们考虑枚举区间的右端点,这样我们只需要考虑从\(1\)到\(i\)的最大区间就好了 由于我们选择 ...

  8. JSP的域对象的作用范围

    <%-- Created by IntelliJ IDEA. User: tT丶 Date: 2017-12-12 Time: 14:53 To change this template use ...

  9. C#和C++的Socket通信

    最近在用C#做一个项目的时候,Socket发送消息的时候遇到了服务端需要接收C++结构体的二进制数据流,这个时候就需要用C#仿照C++的结构体做出一个结构来,然后将其转换成二进制流进行发送,之后将响应 ...

  10. 配置文件和mybatis文件存放位置导致系统启动不了

    1.web.xml <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation< ...