作为一个呼应,写一个c++版本的同步http post客户端功能,如果你需要纯C版本,移步这里

linux下纯C简单的HTTP POST请求 客户端模型

讲解一下基本的的http post协议

  • 通过\r\n,实现tcp的消息边界
  • 每个请求的第一段 POST /a.b HTTP/1.1
    • POST http的方法,还有最常用的GET,当然还有其他的几种,略过
    • /a.b 请求的网页路径,比如如果是首页,最经常的就是/
    • HTTP/1.1 http协议的版本号,传说中已经出了2了,还有神奇的谷歌出的用来替代http协议的SPDY
  • 通过这条信息表明这是一个表单
    • Content-Type: application/x-www-form-urlencoded
  • 通过这条信息来表示这次httppost 的包体长度,非必需项
    • Content-Length:12
  • 然后就是一个空行,代表接下来都是包体

刚才是请求,谈一下响应就更简单了

  • 响应内容 HTTP/1.1 200 OK

    • 200就是传送中的状态,404没找喔等等
    • HTTP/1.1 表示http的版本号
    • 当然,如果包头也是可以存在,此处不介绍
    • 然后就是一个空行,分割是包体

好了,直接上代码吧

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp> 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直到成功
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator); // 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.0\r\n";
request_stream << "Host: " << host << ":" << port << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Content-Length: " << data.length() << "\r\n";
request_stream << "Content-Type: application/x-www-form-urlencoded\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(, ) != "HTTP/")
{
reponse_data = "Invalid response";
return -;
}
// 如果服务器返回非200都认为有错,不支持301/302等跳转
if (status_code != )
{
reponse_data = "Response returned with status code != 200 " ;
return status_code;
} // 传说中的包头可以读下来了
std::string header;
std::vector<string> headers;
while (std::getline(response_stream, header) && header != "\r")
headers.push_back(header); // 读取所有剩下的数据作为包体
boost::system::error_code error;
while (boost::asio::read(socket, response,
boost::asio::transfer_at_least(), error))
{
} //响应有数据
if (response.size())
{
std::istream response_stream(&response);
std::istreambuf_iterator<char> eos;
reponse_data = string(std::istreambuf_iterator<char>(response_stream), eos);
} if (error != boost::asio::error::eof)
{
reponse_data = error.message();
return -;
}
}
catch(std::exception& e)
{
reponse_data = e.what();
return -;
}
return ;
} int main(int argc, char* argv[])
{
string host = "127.0.0.1";
string port = "";
string page = "/auth/login";
string data = "user_name=linbc&password=a";
string reponse_data; int ret = post(host, port, page, data, reponse_data);
if (ret != )
std::cout << "error_code:" << ret << std::endl; std::cout << reponse_data << std::endl; return ;
}

编译一下吧。。

g++ post.cc -std=c++11 -pthread -lboost_system

跨平台c++/boost/asio 简单的HTTP POST请求 客户端模型的更多相关文章

  1. 使用 boost.asio 简单实现 异步Socket 通信

     客户端: class IPCClient { public: IPCClient(); ~IPCClient(); bool run(); private: bool connect(); bool ...

  2. boost asio异步读写网络聊天程序客户端 实例详解

    boost官方文档中聊天程序实例讲解 数据包格式chat_message.hpp <pre name="code" class="cpp">< ...

  3. 使用boost.asio实现网络通讯

    #include <boost/asio.hpp> #define USING_SSL //是否加密 #ifdef USING_SSL #include <boost/asio/ss ...

  4. boost::asio译文

        Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...

  5. Boost.Asio技术文档

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

  6. #include <boost/asio.hpp>

    TCP服务端和客户端 TCP服务端 #include <iostream> #include <stdlib.h> #include <boost/asio.hpp> ...

  7. (原创)如何使用boost.asio写一个简单的通信程序(一)

    boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ...

  8. (原创)如何使用boost.asio写一个简单的通信程序(二)

    先说下上一篇文章中提到的保持io_service::run不退出的简单办法.因为只要异步事件队列中有事件,io_service::run就会一直阻塞不退出,所以只要保证异步事件队列中一直有事件就行了, ...

  9. boost::asio之(一)简单客户端服务器回显功能

    客户端: // BoostDev.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #inc ...

随机推荐

  1. Centos7修改主机名称、DNS、网卡信息

    1 hostnamectl set-hostname wangshuyi 2 vi /etc/hostname 3 vi /etc/resolv.conf 4 vi /etc/sysconfig/ne ...

  2. Android OpenGL教程-第二课【转】

    第二课 你的第一个多边形: 在第一个教程的基础上,我们添加了一个三角形和一个四边形.也许你认为这很简单,但你已经迈出了一大步,要知道任何在OpenGL中绘制的模型都会被分解为这两种简单的图形. 读完了 ...

  3. [转]Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)

    本文转自:http://www.cnblogs.com/aehyok/p/3545824.html 前言 很久没更新博客了,加上刚过年,现在准备重新开战,继续自己的学习之路.本文已同步到Web API ...

  4. Backbone之温故而知新1-MVC

    在忙碌了一段时间之后,又有了空余时间来学习新的东西,自从上次研究了backbone之后,一直不得入门,今天有时间有温故了一次,有了些许进步在此记录下, 在开始之前,不得不提一下我的朋友给了我“豆瓣音乐 ...

  5. redis(4)事务

    一.事务 一般来说,事务必须满足4个条件,也就是我们常说的ACID: 1)Atomicity 原子性:一个事务中的所有操作要么全部完成,要么全部不完成,不会结束在中间的某个环节.事务在执行过程中发生错 ...

  6. 二、spring-boot-devtools热部署

    springboot提供了热部署,需要添加依赖: <dependency> <groupId> org.springframework.boot</groupId> ...

  7. Java 基础(2)——编译运行 & 规范

    上节学到 Java 的编译与运行,我们已经学会了怎么去执行一个写好(假装我们已经会写了)的代码,这篇当然要试试手啦 O(∩_∩)O 哈哈~ 小试一下 新建一个 HelloAhanWhite.java ...

  8. 中南oj 1215: 稳定排序

    1215: 稳定排序 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 111  Solved: 43 [Submit][Status][Web Boar ...

  9. 为My97DatePicker日期插件设置默认日期

    datepicker.zip 为My97DatePicker日期插件设置默认日期,开始日期为系统日期的前一个月,结束日期为系统日期: 开始日期不能大于结束日期,且都不能大于今天: 开始日期-maxDa ...

  10. Java泛型拾遗

    先上百度百科的解释 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方 ...