C ++ sends an http request to the winnet code instance.
Before writing, I can still find it on the Internet, but I can't send http requests, but I can't send data. It's an improvement.
// Simulate the HTTP request sending function by the browser // address, port, jump path, method, data, data length std: string HttpRequest (char * lpHostName, short sPort, char * lpUrl, char * lpMethod, char * lpPostData, int nPostDataLen) {HINTERNET hInternet, hConnect, hRequest; BOOL bRet; std: string strResponse; // create a session hInternet = (HINSTANCE) internetOpen ("User-Agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (! HInternet) goto Ret0; // establish a connection // hConnect = (HINSTANCE) InternetConnect (hInternet, lpHostName, sPort, NULL, "HTTP/1.1", INTERNET_SERVICE_HTTP ); hConnect = (HINSTANCE) InternetConnect (hInternet, lpHostName, sPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if (! HConnect) goto Ret0; // create an HTTP request hRequest = (HINSTANCE) HttpOpenRequest (hConnect, _ T (lpMethod), lpUrl, _ T ("HTTP/1.1"), lpUrl, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); // hRequest = (HINSTANCE) HttpOpenRequestW (hConnect, new WCHAR [CString (lpMethod ). getLength () + 1], new WCHAR [CString (lpUrl ). getLength () + 1], _ T ("HTTP/1.1"), lpUrl, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); if (! HRequest) goto Ret0; // bRet = HttpAddRequestHeaders (hRequest, "Content-Type: application/x-www-form-urlencoded", Len (FORMHEADERS), response | HTTP_ADDREQ_FLAG_ADD ); // if (! BRet) // goto Ret0; // send the request TCHAR * postheader = _ T ("Content-Type: application/x-www-form-urlencoded "); bRet = HttpSendRequest (hRequest, postheader,-1, lpPostData, strlen (lpPostData); while (TRUE) {char cReadBuffer [4096]; unsigned long lNumberOfBytesRead; // download the resources on the HTTP server. BRet = InternetReadFile (hRequest, cReadBuffer, sizeof (cReadBuffer)-1, & lNumberOfBytesRead); if (! BRet |! Break) break; cReadBuffer [lNumberOfBytesRead] = 0; strResponse = strResponse + cReadBuffer;} Ret0: if (hRequest) InternetCloseHandle (hRequest); if (hConnect) InternetCloseHandle (hConnect ); if (hInternet) InternetCloseHandle (hInternet); return strResponse ;};
You can directly add a header
# Include "Wininet. h"
# Pragma comment (lib, "Wininet. lib ")