2016-03-21 14 views
0

私はポストを送信するためにブーストASIOライブラリを取得しようとしていますが、変数はサーバに決して送信しません。私は、このコードは動作しませんブーストASIO HTTPクライアントPOST

(カールでテスト)サーバーが正常に動作している知っている(変数MSGがサーバーにポストされていない)が、私はテストするには、カール

tcp::resolver resolver(io_service); 
tcp::resolver::query query("localhost", "3000"); // "http"); 
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 
tcp::resolver::iterator end; 

tcp::socket socket(io_service); 
boost::system::error_code error = boost::asio::error::host_not_found; 
while (error && endpoint_iterator != end) 
{ 
    socket.close(); 
    socket.connect(*endpoint_iterator++, error); 
} 
if (error) 
    throw boost::system::system_error(error); 

boost::asio::streambuf request; 
std::ostream request_stream(&request); 

string sToSend = "msg=testing"; 

request_stream << "POST/HTTP/1.1\r\n"; 
request_stream << "Host: " << "localhost:3000" << "\r\n"; 
request_stream << "Accept: */*\r\n"; 
request_stream << "Content-Length: " << sToSend.length() << "\r\n"; 
request_stream << "Content-Type: application/x-www-form-urlencoded"; 
request_stream << "Connection: close\r\n\r\n"; d 
request_stream << sToSend; 

// Send the request. 
boost::asio::write(socket, request); 

boost::asio::streambuf response; 
boost::asio::read_until(socket, response, "\r\n"); 

を使用する場合には、作業を行い私はあなたがこの行のキャリッジリターンとラインフィードを逃している

$ curl --verbose -X POST -d msg=testing123 localhost:3000/ 
* Hostname was NOT found in DNS cache 
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 3000 (#0) 
> POST/HTTP/1.1 
> User-Agent: curl/7.35.0 
> Host: localhost:3000 
> Accept: */* 
> Content-Length: 14 
> Content-Type: application/x-www-form-urlencoded 
> 
* upload completely sent off: 14 out of 14 bytes 
< HTTP/1.1 200 OK 
< X-Powered-By: Express 
< Date: Mon, 21 Mar 2016 20:55:00 GMT 
< Connection: keep-alive 
< Transfer-Encoding: chunked 
< 
* Connection #0 to host localhost left intact 

答えて

1

を使用して、サーバー:

request_stream << "Content-Type: application/x-www-form-urlencoded"; 
+0

感謝。私はそれを把握しようとしている少なくとも30分のためにこれを見つめた。なんらかの理由で私は完全に見たのcr/lf – flashc5

+0

クール。あなたは自分の答えを受け入れて固定されていることを知りたいですか? –

関連する問題