C ++ sends an http request to the winnet code instance.

Source: Internet
Author: User

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 ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.