Like this page is the first to say I am a Chinese this sentence syntactic analysis, and then the HTTP post request to implement in c + +, there are many places have similar code, can be said to be exactly the same code, but did not run,
As follows:
Resources:
http://apps.hi.baidu.com/share/detail/39003388
http://blog.csdn.net/yc0188/article/details/4741871
http://bbs.chinaunix.net/thread-2094334-1-1.html
//the header file
#include
#include
#include
using namespace std;
//function declaration
Int request (char * hostname, char * API, char * parameters).
//method calls
Int _tmain (int arg c, TCHAR * argv [], TCHAR * envp [])
{
Int nRetCode=0;
//initialize the MFC and the print and the error on failure
if (! AfxWinInit (: : GetModuleHandle (NULL), NULL, : : GetCommandLine (), 0))
{
//TODO: change the error code to suit your needs
_tprintf (_T (" Fatal Error: MFC initialization failed \ n "));
return 1;
}
Request (" reg.163.com ", "http://reg.163.com/CheckUser.jsp", "test");
Return nRetCode;
}
//function to achieve
Int request (char * hostname, char * API, char * parameters)
{
WSADATA WSADATA;
WSAStartup (0 x0101, & amp; WsaData);
//initialize the socket
Struct hostent * host_addr=gethostbyname (hostname);
If (host_addr==NULL)
{
Cout<& lt;" Unable to locate the host "& lt;
}
Sockaddr_in sin;
Sin. Sin_family=AF_INET;
Sin. The sin_port=htons (80) (unsigned short);
Sin. The sin_addr. S_addr=* ((int *) * host_addr - & gt; H_addr_list);
Int the sock=socket (AF_INET SOCK_STREAM, 0).
If (the sock==1)
{
The return - 100;
}
//connect
If (connect (the sock, (const struct sockaddr *) & amp; Sin, sizeof (sockaddr_in))==1)
{
Cout<& lt;" Connect failed "& lt;
}
//initialization sending information
Char send_str [2048]={0};
//header
Strcat (send_str, "POST");
Strcat (send_str, API);
Strcat (send_str, "HTTP/1.1 \ r \ n");
Strcat (send_str, "Host:");
Strcat (send_str, hostname);
Strcat (send_str, "\ r \ n");
Strcat (send_str, "Connection: keep - the alive \ r \ n");
Char content_header [100].
Sprintf (content_header, "the Content - Length: % d \ r \ n", strlen (parameters));
Strcat (send_str, content_header);
Strcat (send_str, "cache-control: Max - age=0 \ r \ n");
Strcat (send_str, "Origin: http://www.hao123.com\r\n");
Strcat (send_str, "the user-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/15.0.849.0 Safari/535.1 \ r \ n");
Strcat (send_str, "content-type: application/x - WWW - form - urlencoded \ r \ n");
Strcat (send_str, "Accept: text/HTML and application/XHTML + XML, application/XML. Q=0.9 */*; Q=0.8 \ r \ n ");
Strcat (send_str, "Referer: http://www.hao123.com/\r\n");
Strcat (send_str, "Accept - Encoding: gzip, deflate, SDCH \ r \ n");
Strcat (send_str, "Accept - Language: useful - CN, useful; Q=0.8 \ r \ n ");
//content information
Strcat (send_str, "\ r \ n");
Strcat (send_str, parameters);
If (send (the sock, send_str, strlen (send_str), 0)==1)
{
Cout<& lt;" Send failed "& lt;
}
//return information
Char recv_str [4096]={0};
If (recv (sock, recv_str, sizeof (recv_str), 0)==1)
{
Cout<& lt;" Recv failed "& lt;
}
Cout
WSACleanup ();
return 0;
}
This code is very strange, why there is a www.hao123.com???
Ask god to analyze, thank you!!
CodePudding user response:
HTTP header inside some things is not required, just like what Referer removed, there will not be affectedCodePudding user response:
This is the result of I use win32 run out:HTTP/1.1 200 OK
Server: nginx
Date: Sat, 05 Jul 2014 07:43:13 GMT
The content-type: text/HTML. Charset=utf-8
Transfer - Encoding: chunked
Connection: keep alive -
Than: Accept - Encoding
Cache-control: no - Cache
The Set - cookies: SID=dbc45a17 b72c2a6f65a b7-5-6882-4450-88. Domain=reg.163.com; Path=/
The Set - cookies: JSESSIONID=adbSGcF80xKlj5_EHEfCu; Path=/
Expires, Sat, 05 Jul 2014 07:43:12 GMT
Than: the user-agent
Than: Accept
The Content - Encoding: gzip
235
And the correct operation of the API page it says the result is:
$curl -i "http://api.ltp-cloud.com/analysis/? Api_key=YourApiKey& Text=I am Chinese, & amp; The pattern=dp& Format=plain "
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 03 Jan 2014 04:24:32 GMT
The content-type: text/plain
Transfer - Encoding: chunked
Connection: keep alive -
Than: Accept - Encoding
I'm _0 _1 (SBV
HED is _1-1
China _2 _3 ATT
Man is _3 _1 VOB
Pray god to analyse the I in the running for the code! Grateful to
CodePudding user response:
Check your argument is correct, such as api_keynullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull