两个函数的区别:

提交表单数据和提交文本数据

表单数据:

request_stream << "Content-Type: application/x-www-form-urlencoded\r\n";

文本数据:

request_stream << "Content-Type: text/html\r\n";
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp> using boost::asio::ip::tcp;
using std::string; int post_form(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 post_txt(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: text/html\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 = "80";
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 != 0)
std::cout << "error_code:" << ret << std::endl; std::cout << reponse_data << std::endl; return 0;
}*/

基于Boost库的HTTP Post函数的更多相关文章

  1. (十)boost库之多线程

    (十)boost库之多线程 1.创建线程 使用boost库可以方便的创建一个线程,并提供最多支持9个参数的线程函数,相对于void*来说,方便了很多,创建线程主要提供了一下3种方式: 线程库头文件:# ...

  2. 漫步Facebook开源C++库Folly之string类设计(散列、字符串、向量、内存分配、位处理等,小部分是对现有标准库和Boost库功能上的补充,大部分都是基于性能的需求而“重新制造轮子”)

    就在近日,Facebook宣布开源了内部使用的C++底层库,总称folly,包括散列.字符串.向量.内存分配.位处理等,以满足大规模高性能的需求. 这里是folly的github地址:https:// ...

  3. boost库在工作(15)绑定器与函数对象之三

    前面已经可以优美地解决两个参数的函数给算法for_each调用了,但是又会遇到这样的一种情况,当需要三个参数或者三个以上的参数给算法for_each调用呢?从STL里的绑定器bind1st,显然是不行 ...

  4. boost库:函数对象

    函数对象是指那些可以被传入到其它函数或是从其它函数返回的一类函数. 1. boost::bind bind提供了一个机制,是函数与几乎不限数量的参数一起使用,就可以得到指定签名的函数.bind会复制传 ...

  5. [C/C++] C/C++延伸学习系列之STL及Boost库概述

    想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...

  6. Boost库实现线程池学习及线程实现的异步调用

    A.Boost线程池实现 参考自: Boost库实现线程池实例 原理:使用boost的thread_group存储多个线程,使用bind方法将要处理的函数转换成线程可调用的函数进行执行:使用队列存储待 ...

  7. boost库区间range基本原理及使用实例

    由 www.169it.com 搜集整理 区间的概念类似于STL中的容器概念.一个区间提供了可以访问半开放区间[first,one_past_last)中元素的迭代器,还提供了区间中的元素数量的信息. ...

  8. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

  9. C++ Boost库分类总结

    c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...

随机推荐

  1. C#/Net定时导出Excel并定时发送到邮箱

    一.定时导出Excel并定时发送到邮箱   首先我们先分析一下该功能有多少个小的任务点:1.Windows计划服务 2.定时导出Excel定指定路径 3.定时发送邮件包含附件   接下来我们一个个解决 ...

  2. 【Python】keras卷积神经网络识别mnist

    卷积神经网络的结构我随意设了一个. 结构大概是下面这个样子: 代码如下: import numpy as np from keras.preprocessing import image from k ...

  3. 从零自学Java-6.使用循环重复执行操作

    1.使用for循环: 2.使用while循环: 3.使用do-while循环: 4.提早退出循环(break,continue): 5.为循环命名. 程序Nines:显示1-200的整数与9的乘积 p ...

  4. LeetCode题解之 Letter Case Permutation

    1.题目描述 2.问题分析 可以使用递归的方法解决,参考了别人的答案才写出来的. 3.代码 vector<string> letterCasePermutation(string S) { ...

  5. Web Services 根据wsdl生成代理类

    生成代理类步骤: 一:找到Visual Studio 的工具文件夹 二:用管理员方式打开本机工具命令提示 三:输入要执行的脚本 wsdl /language:C# /n:xxxx.HermesMobi ...

  6. length() 用法

    length:(字符串 | 列):求出字符串的长度: 求出bqh4表中zym列的长度. select * from bqh4select a.*,length(zym) from bqh4 a

  7. 在Ubuntu17.04中遇到无法清空回收站解决方法

    在Ubuntu17.04下,遇到清空回收站文件时报错,提示”Failed to delete the item from the trash”,无法清空回收站. 回收站其实就是一个文件夹,存放着被删掉 ...

  8. How to add hyperlink in JLabel

    You can do this using a JLabel, but an alternative would be to style a JButton. That way, you don't ...

  9. django中url 和 path 的区别

    django中 url 和 path 都是配置路径,有什么不同? django.urls path django.conf.urls  url path 与 url 是两个不同的模块,效果都是响应返回 ...

  10. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...