Home > Back-end >  The boost c library word processing problems, big hands!
The boost c library word processing problems, big hands!

Time:09-22

[size=40 px] here is net friend boost simulation post code, the effect is achieved, but access to the string is in brackets (" 3 f \ r \ n {\ "errno " : \ \ "0", \ "errmsg " : \ "success ", \ "lastpoint " : \ "of" the 2017-02-07-18 to 36 \} \ r \ n0 \ r \ n \ r \ n ") in the other top fixed post requests are found to be () function returns the length of the 16 response, there are 3 f, if the response is so down several return true not a few long ago, my idea is to capture the first \ r \ n to the second \ r \ n between the data, but have the flaw, will only c #, my goal is: let the value of the sc is corresponding content ({\ "errno " : \ \ "0", \ "errmsg " : \ "success ", \ "lastpoint " : \ "the 2017-02-07-18 to 36 \}"), you can do it:
1, it is best to intercept the returned string, a boost to the library function of intercepting string, with STD don't give too high scores,
2, modify the method body, let sc without intercept is response content ~ (as is the same thing), or simply help me write a pure boost post or get request code, grateful,
Post code: [/size]



 # include 
# include
# include
# include "boost/an asio. HPP"
# include
# include



//into the namespace
Using the namespace boost;
using namespace std;
IP using boost: : an asio: : : : TCP;

//into the namespace

///GET request
String PostRequest (string host, string path, string form)
{
Long length=form. The length ();

//declare an Asio basis: io_service (task scheduling machine)
Boost: : an asio: : io_service io_service;

//access to the server terminal list
TCP: : resolver resolver (io_service);
TCP: : resolver: : query query (host, "HTTP");
TCP: : resolver: : iterator iter=resolver. Resolve (query);

//try to connect each terminal until the success to establish a socket connection
TCP: : socket socket (io_service);
Boost: : an asio: : connect (socket, iter);

//build network request header
//specified "Connection: close" disconnected after get response, ensure that all data files,
Boost: : an asio: : streambuf request;
Ostream request_stream (& amp; Request);
Request_stream & lt; & lt; "POST" & lt; & lt; The path & lt; & lt; "HTTP/1.1 \ r \ n";
Request_stream & lt; & lt; "Host:" & lt; & lt; The host & lt; & lt; "\ r \ n";
Request_stream & lt; & lt; "Accept: */* \ r \ n";
Request_stream & lt; & lt; "The content-type: application/x - WWW - form - urlencoded \ r \ n";
Request_stream & lt; & lt; "The Content - Length:" & lt; & lt; Length & lt; & lt; "\ r \ n";
Request_stream & lt; & lt; "Connection: close \ r \ n \ r \ n";//note that there are two empty lines
Request_stream & lt; & lt; The form;//POST to send the data itself does not contain more than empty lines

//send the request
Boost: : an asio: : write (socket, request);

//read response. The response stream buffer streambuf automatically grow to full line
//the growth can be in in the buffer flow structure by setting the maximum limit
Boost: : an asio: : streambuf response;
Boost: : an asio: : read_until (socket, the response, "\ r \ n");

//check whether the reply is OK.
Cost response_stream (& amp; The response);//reply flow
String http_version;
Response_stream & gt;> Http_version;
Unsigned int status_code;
Response_stream & gt;> Status_code;
String status_message;
Getline (response_stream status_message);
if (! Response_stream | | http_version. Substr (0, 5).
="HTTP/"){
Printf (" invalid response \ n ");
}
If (status_code!
=200){
Printf (" response to return to the status code % d \ n ", status_code);
}

//read response header, a blank line after stop
Boost: : an asio: : read_until (socket, the response, "\ r \ n \ r \ n");

//show the response header
The string header;
Int len=1;
While (getline (response_stream, header) & amp; & The header! "=" \ r & amp; & The header!="\ r \ n")
{
/* if (header. The find (" the Content - Length: ")==0) {
Stringstream stream;
The stream & lt; & lt; The header. The substr (16);
The stream & gt;> Len.
} */
}

Long size=response. The size ();

If (size & gt; 0 {
//... Do nothing
}

//loop reads the data flow, until the end of the file
Boost: : system: : error_code error;
While (boost: : an asio: : read (socket, the response, boost: : an asio: : transfer_at_least (1), the error))
{
//get response length
Size=response. The size ();
If (len!=0) {
Cout & lt; & lt; The size & lt; & lt; "Byte" & lt; & lt; (the size * 100)/len & lt; & lt; "% \ n";
}

}
If (error!=boost: : an asio: : error: : eof)
{
Throw the boost: : system: : system_error (error);
}
//convert streambuf type to type string return
Cost is (& amp; The response);
Is. Unsetf (ios_base: : skipws);
String sz.
Sz. Append (istream_iterator & lt; Char> (is), istream_iterator & lt; Char> ());

//return the converted string
Return sz.
}

Int main ()
{
Cout & lt; & lt; PostRequest (" task. Browser. 360. Cn ", "/online/setpoint", "stamp"=1485856383 & amp; Qt=% Q % 3 du 3 d % 25 m1%25 s3%25 oq % 25 n1008%26 n % 3 d % 25%25 s3%25 m1 oq % 25 n1008%26 le % 3 dqvh0zumemkvhl2z % 3 d % 26 m % 3 dzgt4wgwowgwowgwowgwowgwozqn3%26 qid % 3 d49871531%26 im % 3 5 ft01281fc12930368b13 d1 % % 26 SRC % 3 d360se % 26 t % 3 d1 % % 0 at % 3 ds 0 d % 3 d0a9351aa131bc7b8dd23726e3ebdafad % 26 t % 3 d1483137678%26 lm % 3 d % 26 lf % 3 d1%26 sk % 3 d6f360194503d7fff701e6bfab6645364%26 mt % 3 d1483137678%26 rc % 3 d1%26 v % 3 d2%2 e0%26 a % 3 d1 & amp; Verify=86 f47277614701445350a48e4182a076 ");
getchar();
return 0;
}

  • Related