作為一個呼應,寫一個c++版本的同步http post客戶端功能,如果你需要純C版本,移步這裡
講解一下基本的的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(0, 5) != "HTTP/") { reponse_data = "Invalid response"; return -2; } // 如果伺服器返回非200都認為有錯,不支援301/302等跳轉 if (status_code != 200) { 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(1), 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 -3; } } catch(std::exception& e) { reponse_data = e.what(); return -4; } return 0; } 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; }
編譯一下吧。。
g++ post.cc -std=c++11 -pthread -lboost_system