Boost上 Http的一个Get示例分析
来源:互联网 发布:vrp问题蚁群算法代码 编辑:程序博客网 时间:2024/06/06 05:24
有的时候需要实现C++编写http客户端更新网页服务器上的内容,Boost 编程有一个Get的示例如下。是取得WEB server的license。
以下是Boost 官方实例:
//// sync_client.cpp// ~~~~~~~~~~~~~~~//// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)//// Distributed under the Boost Software License, Version 1.0. (See accompanying// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)//#include <iostream>#include <istream>#include <ostream>#include <string>#include <boost/asio.hpp>using boost::asio::ip::tcp;int main(int argc, char* argv[]){ try { argc =3 ; argv[1] = "127.0.0.1"; argv[2] = "/License"; if (argc != 3) { std::cout << "Usage: sync_client <server> <path>\n"; std::cout << "Example:\n"; std::cout << " sync_client www.boost.org /LICENSE_1_0.txt\n"; return 1; } boost::asio::io_service io_service; // Get a list of endpoints corresponding to the server name. tcp::resolver resolver(io_service); tcp::resolver::query query(argv[1], "http"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); // Try each endpoint until we successfully establish a connection. 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.
// 以上 和服务器建立了一个TCP的连接。
boost::asio::streambuf request; std::ostream request_stream(&request); request_stream << "GET " << argv[2] << " HTTP/1.0\r\n"; request_stream << "Host: " << argv[1] << "\r\n"; request_stream << "Accept: */*\r\n"; request_stream << "Connection: close\r\n\r\n";
//这里确定了URL的命令,命令的解释: GET获取HTTP上的 argv[2]中内容 也就是license.txt 获取完后接受 Accept ,最后关闭 Connection Close // 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/") { std::cout << "Invalid response\n"; return 1; } if (status_code != 200) { std::cout << "Response returned with status code " << status_code << "\n"; return 1; } // Read the response headers, which are terminated by a blank line. boost::asio::read_until(socket, response, "\r\n\r\n"); // Process the response headers. std::string header; while (std::getline(response_stream, header) && header != "\r") std::cout << header << "\n"; std::cout << "\n"; // Write whatever content we already have to output. if (response.size() > 0) std::cout << &response; // Read until EOF, writing data to output as we go. boost::system::error_code error; while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) std::cout << &response; if (error != boost::asio::error::eof) throw boost::system::system_error(error); } catch (std::exception& e) { std::cout << "Exception: " << e.what() << "\n"; } return 0;}
- Boost上 Http的一个Get示例分析
- boost执行http的GET请求
- 一个更好用的http库 -- libcurl,送上简单示例代码
- 一个c++的 http get 代码
- Http post get的一个简单列子
- 为Boost ASIO的http server4例子添加Cookie和Post/Get参数提取支持
- iOS上的http请求:get、post以及同步、异步
- iOS上的http请求:get、post以及同步、异步
- 性能测试需求分析的一个示例
- 一个Stack的Java示例图解分析
- 性能测试需求分析的一个示例
- node的http.get一个诡异的问题
- HTTP GET请求报文格式示例
- [Golang]Http get和post请求示例
- http相应get和post请求示例
- http协议get,post请求示例
- boost::asio扩展一个服务的代码分析
- http 的Get和 Post请求深入分析
- query 取得选中的radio
- 解决Eclipse报errors running builder ‘javascript validator’ on project
- vimrc
- 收藏一些JavaScript代码
- 一道编程题
- Boost上 Http的一个Get示例分析
- http://acm.hdu.edu.cn/showproblem.php?pid=1548
- Java SE/EE/ME区别
- shell 时间计算
- Android中电池信息(Battery information)的取得
- Enterprise Manager配置失败
- POCO C++库在Android 平台上集成(1)
- java 获取某时刻之前若干小时后的时刻
- 不容易系列之(4)——考新郎 hdu2049 递推,错排,组合数