Use WinHTTP to write a client that accesses the web page. If a post form is required, such as the user name and password for login, subsequent page access will be related to the login session. Clients such as IE browser use cookies obtained from the server to save relevant information for session consistency. For details, refer to WinHTTP development and check that there is not much relevant information on the Internet, the following functions use get to obtain the cookie ID number from a website and save it to cstring. Later get operations use winhttpaddrequestheaders to add the cookie to the header, then you can perform the request operation:
Cstringw getcookie ()
{
Cstringw strcookie = l "";
DWORD dwsize = 0;
Lpvoid lpoutbuffer = NULL;
Bool bresults = false;
Hinternet hsession = NULL,
Hconnect = NULL,
Hrequest = NULL;
// Use winhttpopen to obtain a session handle.
Hsession = winhttpopen (sz_agent,
Winhttp_access_type_default_proxy,
Winhttp_no_proxy_name,
Winhttp_no_proxy_bypass, 0 );
// Specify an HTTP server.
If (hsession)
Hconnect = winhttpconnect (hsession, l "wapmail.webdunia.com ",
Internet_default_http_port, 0 );
// Create an HTTP request handle.
If (hconnect)
Hrequest = winhttpopenrequest (hconnect, l "get", null,
Null, winhttp_no_referer,
Winhttp_default_accept_types,
0 );
// Send a request.
If (hrequest)
Bresults = winhttpsendrequest (hrequest,
Winhttp_no_additional_headers, 0,
Winhttp_no_request_data, 0,
0, 0 );
// End the request.
If (bresults)
Bresults = winhttpreceiveresponse (hrequest, null );
// First, use winhttpqueryheaders to obtain the size of the buffer.
If (bresults)
{
Winhttpqueryheaders (hrequest, winhttp_query_raw_headers_crlf,
Winhttp_header_name_by_index, null,
& Dwsize, winhttp_no_header_index );
// Allocate memory for the buffer.
If (getlasterror () = error_insufficient_buffer)
{
Lpoutbuffer = new wchar [dwsize/sizeof (wchar)];
// Now, use winhttpqueryheaders to retrieve the header.
Bresults = winhttpqueryheaders (hrequest,
Winhttp_query_raw_headers_crlf,
Winhttp_header_name_by_index,
Lpoutbuffer, & dwsize,
Winhttp_no_header_index );
}
}
// Print the header contents.
Char * tmpch = (char *) malloc (sizeof (lpoutbuffer ));
If (bresults)
Sprintf (tmpch, "% s", lpoutbuffer );
Strcookie = tmpch;
Int set_cookie = strcookie. Find (L "cookie", 0 );
Int semicolon = strcookie. Find (L ";", set_cookie );
If (set_cookie! =-1 & semicolon! =-1)
Strcookie = strcookie. mid (set_cookie, semicolon-set_cookie) + L "/R/N ";
// Free the allocated memory.
Delete [] lpoutbuffer;
// Report any errors.
If (! Bresults)
Printf ("error % d has occurred./N", getlasterror ());
// Close any open handles.
If (hrequest) winhttpclosehandle (hrequest );
If (hconnect) winhttpclosehandle (hconnect );
If (hsession) winhttpclosehandle (hsession );
Return strcookie;
}