Thanks: Thanks to csdn's zjh824.
The following code is modified on the basis of your article "some notes when using the httpsendrequest function post data in wince.
Http://blog.csdn.net/zjh824/archive/2007/10/26/1845580.aspx
Http://hi.baidu.com/%B3%AC%C8%BBa/blog/item/0a2a0d1f311ad7e2e1fe0b74.html
/*************************************** **************************************** **************************************** *****/
Bool senddatatoserver (cstring strweb, // server page address. Example: www.safewe.com/job1.htm
Cstring strsendbuffer, // send data
Lpbyte lprecvbuffer, // return the data storage address
DWORD dwrecvdatalen, // return Data Length
Cstring & strretheadinfo, // return the data Header
Cstring stropenrequesttype = text ("get"), // data request types: Get and post
Cstring strheaders = text ("accept: */* \ r \ ncontent-type: Application/X-WWW-form-urlencoded "),
// Data Header
DWORD dwport = 80, // Port
DWORD dwservicetype = afx_inet_service_http // service type
);
Bool senddatatoserver (cstring strweb, // server page address. Example: www.safewe.com/job1.htm
Cstring strsendbuffer, // send data
Lpbyte lprecvbuffer, // return the data storage address
DWORD dwrecvdatalen, // return Data Length
Cstring & strretheadinfo, // return the data Header
Cstring stropenrequesttype, // data request type: divided into get and post
Cstring strheaders, // data Header
DWORD dwport, // Port
DWORD dwservicetype // service type
)
{
Cstring strservername = strweb;
Cstring strobject = text ("");
Hinternet hopen;
Hinternet hconnect;
Hinternet hrequest;
Bool Bret = false;
DWORD dwsize;
Tchar * lpheaders = NULL;
Tchar szhttpstatus [4] = {0 };
Int dwhttpstatus = 0;
Char * pszdata = NULL;
Int nerrorcode;
Int n = strweb. Find (text ("/"));
If (n! =-1)
{
Strservername = strweb. Left (N );
Strobject = strweb. mid (n + 1 );
}
Hopen = internetopen (text ("safewe"), internet_open_type_direct, null, 0, 0 );
If (! Hopen)
Return false;
If (! (Hconnect = internetconnect (hopen, strservername, dwport, null, null, dwservicetype, 0, 0 )))
{
Internetclosehandle (hopen );
Return false;
}
Lptstr accepttypes [2] = {text ("*/*"), null };
DWORD dwflags = internet_flag_reload | internet_flag_no_cache_write;
If (! (Hrequest = httpopenrequest (hconnect, stropenrequesttype, strobject, http_version, null,
(Lpctstr *) accepttypes, dwflags, 0 )))
{
Internetclosehandle (hconnect );
Internetclosehandle (hopen );
Return false;
}
DWORD dwhttptimeout = 5000;
If (! Internetsetoption (hrequest, internet_option_connect_timeout, & dwhttptimeout, sizeof (DWORD )))
Goto end;
If (! Internetsetoption (hrequest, internet_option_send_timeout, & dwhttptimeout, sizeof (DWORD )))
Goto end;
If (! Internetsetoption (hrequest, internet_option_receive_timeout, & dwhttptimeout, sizeof (DWORD )))
Goto end;
// Note that the third parameter of httpsendrequest must be in Multi-byte encoding format. Otherwise, an error occurs on the server.
Pszdata = new char [strsendbuffer. getlength () * 2 + 2];
Memset (pszdata, 0, strsendbuffer. getlength () * 2 + 2 );
Widechartomultibyte (cp_acp, 0, strsendbuffer. getbuffer (0 ),
-1, pszdata, strsendbuffer. getlength () * 2, null, null );
// Httpaddrequestheaders (hrequest, szheaders, szheaders. getlength (), http_addreq_flag_coalesce_with_comma); // you can add data headers in this way.
If (httpsendrequest (hrequest, strheaders, strheaders. getlength (), pszdata, strlen (pszdata) = false)
{
Nerrorcode = getlasterror ();
Goto end;
} // If the data header strheaders is empty, the error error_http_header_not_found will occur.
Httpqueryinfo (hrequest, http_query_raw_headers_crlf, null, & dwsize, null );
Lpheaders = new tchar [dwsize + 1];
Zeromemory (lpheaders, sizeof (lpheaders)/sizeof (tchar ));
If (! Httpqueryinfo (hrequest, http_query_raw_headers_crlf, (lpvoid) lpheaders, & dwsize, null ))
Goto end2;
Strretheadinfo = lpheaders;
Memcpy (szhttpstatus, lpheaders + wcslen (_ T ("HTTP/1.1"), 3 * sizeof (tchar ));
Dwhttpstatus = _ wtoi (szhttpstatus );
If (dwhttpstatus <200) | (dwhttpstatus> 300 ))
Goto end2;
If (! Internetreadfile (hrequest, (lpvoid) (lprecvbuffer), dwrecvdatalen, & dwsize) // you can change it to receive all data cyclically.
Goto end2;
If (dwsize = 0)
Goto end2;
Bret = true;
End2:
If (lpheaders)
{
Delete lpheaders;
Lpheaders = NULL;
}
End:
If (pszdata)
{
Delete pszdata;
Pszdata = NULL;
}
Internetclosehandle (hrequest );
Internetclosehandle (hconnect );
Internetclosehandle (hopen );
Return Bret;
}
/*************************************** **************************************** **************************************** *****/
// Function example:
Byte byrecvbuff [1, 1024] = {0 };
Cstring strrethead;
Senddatatoserver (text ("update.safewe.com/report.php "),
Text ("string = 0123456789012345 Test &"), byrecvbuff, 1024, strrethead, text ("Post "));
/*************************************** **************************************** **************************************** *****/
// Corresponding PHP page code:
<HTML>
<Body>
Thanks for your report! <Br/>
<? PHP
$ MD5 = $ _ post ["string"];
$ File = fopen ("data.txt", "A +") or $ file = fopen ("data.txt", "W ");
Fwrite ($ file, $ string, strlen ($ string ));
Fclose ($ file );
?>
</Body>
</Html>
/*************************************** **************************************** **************************************** *****/
References:
Notes when using the httpsendrequest function to post data in WinCE
Http://blog.csdn.net/zjh824/archive/2007/10/26/1845580.aspx