### C++ Example: Sending a GET Request Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-send This example demonstrates how to open an HTTP connection using GET, send the request without a body, and retrieve the response text. It requires COM initialization and specific WinHTTP libraries. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // variable for return value HRESULT hr; // initialize COM hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varFalse; VARIANT varEmpty; CLSID clsid; VariantInit(&varFalse); V_VT(&varFalse) = VT_BOOL; V_BOOL(&varFalse) = VARIANT_FALSE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Get Response text. hr = pIWinHttpRequest->get_ResponseText(&bstrResponse); } // Print response to console. wprintf(L"%.256s",bstrResponse); // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (bstrResponse) SysFreeString(bstrResponse); CoUninitialize(); return 0; } ``` -------------------------------- ### Select Client Certificate Example Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-setclientcertificate This JScript example demonstrates how to select a client certificate for a WinHttpRequest object. It specifies the certificate store and subject for the request. ```javascript // Instantiate a WinHttpRequest object. var HttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); // Open an HTTP connection. HttpReq.Open("GET", "https://www.fabrikam.com/", false); // Select a client certificate. HttpReq.SetClientCertificate( "LOCAL_MACHINE\Personal\My Middle-Tier Certificate"); // Send the HTTP Request. HttpReq.Send(); ``` -------------------------------- ### WinHttpGetProxyForUrl Example Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttp-autoproxy-api This example demonstrates how to use WinHttpGetProxyForUrl to obtain proxy configuration for a given URL. It covers auto-detection of the PAC URL and setting the obtained proxy information on a WinHTTP request handle. ```APIDOC ## WinHttpGetProxyForUrl ### Description Retrieves the proxy configuration for a given URL, supporting auto-detection of the Proxy Auto-Configuration (PAC) URL and execution of the PAC script to determine the appropriate proxy server. ### Method WinHttpGetProxyForUrl ### Parameters - **hHttpSession** (HINTERNET) - Handle to the WinHTTP session. - **lpcwszUrl** (LPCWSTR) - The URL for which to retrieve the proxy configuration. - **pAutoProxyOptions** (WINHTTP_AUTOPROXY_OPTIONS*) - Pointer to a WINHTTP_AUTOPROXY_OPTIONS structure that specifies how to obtain the proxy configuration. - **pProxyInfo** (WINHTTP_PROXY_INFO*) - Pointer to a WINHTTP_PROXY_INFO structure that receives the proxy configuration. ### Request Example ```cpp HINTERNET hHttpSession = NULL; HINTERNET hConnect = NULL; HINTERNET hRequest = NULL; WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions; WINHTTP_PROXY_INFO ProxyInfo; DWORD cbProxyInfoSize = sizeof(ProxyInfo); ZeroMemory( &AutoProxyOptions, sizeof(AutoProxyOptions) ); ZeroMemory( &ProxyInfo, sizeof(ProxyInfo) ); // Create the WinHTTP session. hHttpSession = WinHttpOpen( L"WinHTTP AutoProxy Sample/1.0", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0 ); // Exit if WinHttpOpen failed. if( !hHttpSession ) goto Exit; // Create the WinHTTP connect handle. hConnect = WinHttpConnect( hHttpSession, L"www.microsoft.com", INTERNET_DEFAULT_HTTP_PORT, 0 ); // Exit if WinHttpConnect failed. if( !hConnect ) goto Exit; // Create the HTTP request handle. hRequest = WinHttpOpenRequest( hConnect, L"GET", L"ms.htm", L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0 ); // Exit if WinHttpOpenRequest failed. if( !hRequest ) goto Exit; // Set up the autoproxy call. // Use auto-detection because the Proxy // Auto-Config URL is not known. AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; // Use DHCP and DNS-based auto-detection. AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A; // If obtaining the PAC script requires NTLM/Negotiate // authentication, then automatically supply the client // domain credentials. AutoProxyOptions.fAutoLogonIfChallenged = TRUE; // Call WinHttpGetProxyForUrl with our target URL. If // auto-proxy succeeds, then set the proxy info on the // request handle. If auto-proxy fails, ignore the error // and attempt to send the HTTP request directly to the // target server (using the default WINHTTP_ACCESS_TYPE_NO_PROXY // configuration, which the requesthandle will inherit // from the session). // if( WinHttpGetProxyForUrl( hHttpSession, L"https://www.microsoft.com/ms.htm", &AutoProxyOptions, &ProxyInfo)) { // A proxy configuration was found, set it on the // request handle. if( !WinHttpSetOption( hRequest, WINHTTP_OPTION_PROXY, &ProxyInfo, cbProxyInfoSize ) ) { // Exit if setting the proxy info failed. goto Exit; } } // Send the request. // if( !WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, NULL ) ) { // Exit if WinHttpSendRequest failed. goto Exit; } // Wait for the response. // if( !WinHttpReceiveResponse( hRequest, NULL ) ) goto Exit; // A response has been received, then process it. // (omitted) // Exit: // Cleanup resources... ``` ### Response #### Success Response - **WINHTTP_PROXY_INFO** (structure) - Contains the proxy configuration details if successful. #### Response Example (Details of the WINHTTP_PROXY_INFO structure would be provided here if available in the source.) ``` -------------------------------- ### Passport Configuration Example Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/passport-authentication-in-winhttp Example of configuration information downloaded from the passport nexus server. WinHTTP uses DARealm, DALogin, and ConfigVersion from this information. ```text PassportURLs: DARealm=Passport.net, DALogin=login.passport.com/login2.asp, DAReg=https://register.passport.com/defaultwiz.asp, Properties=https://memberservices.passport.com/ppsecure/MSRV_EditProfile.asp, Privacy=https://www.passport.com/consumer/privacypolicy.asp, GeneralRedir=https://nexusrdr.passport.com/redir.asp, Help=https://memberservices.passport.com/UI/MSRV_UI_Help.asp, ConfigVersion=2 ``` -------------------------------- ### C++ Example: Open, Send, WaitForResponse, and Read Response Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-waitforresponse This C++ example demonstrates how to open an asynchronous HTTP connection, send an HTTP request, wait for the response, and read the response text using the IWinHttpRequest interface. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // Variable for return value HRESULT hr; // Initialize COM. hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varTrue; VARIANT varEmpty; CLSID clsid; VariantInit(&varTrue); V_VT(&varTrue) = VT_BOOL; V_BOOL(&varTrue) = VARIANT_TRUE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varTrue); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Wait for response. VARIANT_BOOL varResult; hr = pIWinHttpRequest->WaitForResponse(varEmpty, &varResult); } if (SUCCEEDED(hr)) { // Get Response text. hr = pIWinHttpRequest->get_ResponseText(&bstrResponse); } if (SUCCEEDED(hr)) { // Print the response to a console. wprintf(L"%.256s",bstrResponse); } // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (bstrResponse) SysFreeString(bstrResponse); CoUninitialize(); return 0; } ``` -------------------------------- ### Using WinHttpGetProxyForUrl for Auto-Proxy Configuration Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttp-autoproxy-api This C++ example demonstrates how to set up an HTTP GET request using WinHTTP's autoproxy capabilities. It initializes WinHTTP handles, configures autoproxy options for auto-detection (DHCP and DNS), and then calls WinHttpGetProxyForUrl to obtain proxy information. If successful, it sets the proxy configuration on the request handle before sending the request. If autoproxy fails, it attempts to send the request directly. ```cpp HINTERNET hHttpSession = NULL; HINTERNET hConnect = NULL; HINTERNET hRequest = NULL; WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions; WINHTTP_PROXY_INFO ProxyInfo; DWORD cbProxyInfoSize = sizeof(ProxyInfo); ZeroMemory( &AutoProxyOptions, sizeof(AutoProxyOptions) ); ZeroMemory( &ProxyInfo, sizeof(ProxyInfo) ); // // Create the WinHTTP session. // hHttpSession = WinHttpOpen( L"WinHTTP AutoProxy Sample/1.0", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0 ); // Exit if WinHttpOpen failed. if( !hHttpSession ) goto Exit; // // Create the WinHTTP connect handle. // hConnect = WinHttpConnect( hHttpSession, L"www.microsoft.com", INTERNET_DEFAULT_HTTP_PORT, 0 ); // Exit if WinHttpConnect failed. if( !hConnect ) goto Exit; // // Create the HTTP request handle. // hRequest = WinHttpOpenRequest( hConnect, L"GET", L"ms.htm", L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0 ); // Exit if WinHttpOpenRequest failed. if( !hRequest ) goto Exit; // // Set up the autoproxy call. // // Use auto-detection because the Proxy // Auto-Config URL is not known. AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; // Use DHCP and DNS-based auto-detection. AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A; // If obtaining the PAC script requires NTLM/Negotiate // authentication, then automatically supply the client // domain credentials. AutoProxyOptions.fAutoLogonIfChallenged = TRUE; // // Call WinHttpGetProxyForUrl with our target URL. If // auto-proxy succeeds, then set the proxy info on the // request handle. If auto-proxy fails, ignore the error // and attempt to send the HTTP request directly to the // target server (using the default WINHTTP_ACCESS_TYPE_NO_PROXY // configuration, which the requesthandle will inherit // from the session). // if( WinHttpGetProxyForUrl( hHttpSession, L"https://www.microsoft.com/ms.htm", &AutoProxyOptions, &ProxyInfo)) { // A proxy configuration was found, set it on the // request handle. if( !WinHttpSetOption( hRequest, WINHTTP_OPTION_PROXY, &ProxyInfo, cbProxyInfoSize ) ) { // Exit if setting the proxy info failed. goto Exit; } } // // Send the request. // if( !WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, NULL ) ) { // Exit if WinHttpSendRequest failed. goto Exit; } // // Wait for the response. // if( !WinHttpReceiveResponse( hRequest, NULL ) ) goto Exit; // // A response has been received, then process it. // (omitted) // Exit: // ``` -------------------------------- ### Get Response Text (C++) Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-responsetext This C++ example demonstrates how to open an HTTP connection, send an HTTP request, and read the response text using the IWinHttpRequest interface. It requires COM initialization and specific libraries. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // Variable for return value HRESULT hr; // Initialize COM. hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varFalse; VARIANT varEmpty; CLSID clsid; VariantInit(&varFalse); V_VT(&varFalse) = VT_BOOL; V_BOOL(&varFalse) = VARIANT_FALSE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Get Response text. hr = pIWinHttpRequest->get_ResponseText(&bstrResponse); } // Print the response to a console. wprintf(L"%.256s",bstrResponse); // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (bstrResponse) SysFreeString(bstrResponse); CoUninitialize(); return 0; } ``` -------------------------------- ### Update Interface Index Table Example Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttpconnectionupdateifindextable This example demonstrates how to update the interface index table with mappings for both a Wi-Fi and a cellular connection. Ensure the hSession is a valid WinHTTP session handle. ```C++ WINHTTP_CONNECTION_IFINDEX_ENTRY entries[2] = {}; entries[0].pwszConnectionName = L"Wi-Fi"; entries[0].dwIfIndex = 4; entries[1].pwszConnectionName = L"Cellular"; entries[1].dwIfIndex = 7; WINHTTP_CONNECTION_IFINDEX_LIST ifList = {}; ifList.pConnectionIfIndexEntries = entries; ifList.nEntries = 2; WinHttpConnectionUpdateIfIndexTable(hSession, &ifList); ``` -------------------------------- ### C++ Example: Get Response Header Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-getresponseheader This C++ example demonstrates how to use IWinHttpRequest::GetResponseHeader to retrieve the 'Date' header from an HTTP response. It requires COM initialization and proper object management. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // Variable for return value HRESULT hr; // Initialize COM hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varFalse; VARIANT varEmpty; CLSID clsid; VariantInit(&varFalse); V_VT(&varFalse) = VT_BOOL; V_BOOL(&varFalse) = VARIANT_FALSE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Get Response text. BSTR bstrName = SysAllocString(L"Date"); hr = pIWinHttpRequest->GetResponseHeader(bstrName, &bstrResponse); } if (SUCCEEDED(hr)) { // Print response to console. wprintf(L"%.256s",bstrResponse); } // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (bstrResponse) SysFreeString(bstrResponse); CoUninitialize(); return 0; } ``` -------------------------------- ### JScript Example: Open, Send, WaitForResponse, and Read Response Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-waitforresponse This JScript example demonstrates how to open an asynchronous HTTP connection, send an HTTP request, wait for a response, and read the response text using the WinHttpRequest object. ```javascript // Instantiate a WinHttpRequest object. var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); // Initialize an HTTP request. WinHttpReq.Open("GET", "https://www.microsoft.com", true); // Send the HTTP request. WinHttpReq.Send(); // Wait for the response. WinHttpReq.WaitForResponse(); // Display the response text. WScript.Echo( WinHttpReq.ResponseText); ``` -------------------------------- ### Get HTTP Status Code (C++) Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-status This C++ example demonstrates how to open an HTTP connection, send a request, and retrieve the HTTP status code and status text using IWinHttpRequest. Ensure COM is initialized and the WinHttpRequest object is created successfully. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // variable for return value HRESULT hr; // initialize COM hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varFalse; VARIANT varEmpty; LONG lStatus = 0; BSTR bstrStatusText = NULL; CLSID clsid; VariantInit(&varFalse); V_VT(&varFalse) = VT_BOOL; V_BOOL(&varFalse) = VARIANT_FALSE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->get_Status(&lStatus); hr = pIWinHttpRequest->get_StatusText(&bstrStatusText); } if (SUCCEEDED(hr)) { // Get Response text. hr = pIWinHttpRequest->GetAllResponseHeaders(&bstrResponse); } if (SUCCEEDED(hr)) { // Print response to console. wprintf(L"%s\n\n", bstrResponse); wprintf(L"%u - %s\n\n", lStatus, bstrStatusText); } // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (bstrStatusText) SysFreeString(bstrStatusText); if (bstrResponse) SysFreeString(bstrResponse); CoUninitialize(); return 0; } ``` -------------------------------- ### WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/option-flags Instructs the stack to start a WebSocket handshake process. ```APIDOC ## WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET ### Description Instructs the stack to start a WebSocket handshake process with WinHttpSendRequest. ### Method SET ### Endpoint N/A (Option flag for WinHTTP handles) ### Parameters None ### Response Success (200 OK) ``` -------------------------------- ### JScript Example: Sending Data with PUT Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-send This JScript example demonstrates how to send data to an HTTP server using the PUT method. It initializes the request, specifies the URL, and sends a string as the request body. ```javascript // Instantiate a WinHttpRequest object. var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); // Initialize an HTTP request. WinHttpReq.Open("PUT", "https://postserver/newdoc.htm", false); // Post data to the HTTP server. WinHttpReq.Send("Post data"); ``` -------------------------------- ### WINHTTP_OPTION_TLS_FALSE_START Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/option-flags Enables TLS False Start for the connection. ```APIDOC ## WINHTTP_OPTION_TLS_FALSE_START ### Description Enables TLS False Start for the connection. ### Method SET ### Endpoint N/A (Option flag for WinHTTP handles) ### Parameters - **enable** (BOOL) - TRUE to enable TLS False Start, FALSE otherwise. ### Request Example ```json { "enable": true } ``` ### Response Success (200 OK) ``` -------------------------------- ### Set Proxy Settings in C++ Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-setproxy This C++ example demonstrates how to set proxy server and bypass list for WinHttpRequest. It requires COM initialization and specific WinHTTP library linkage. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // Variable for return value HRESULT hr; // Initialize COM hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varFalse; VARIANT varEmpty; VARIANT varProxy; VARIANT varUrl; CLSID clsid; VariantInit(&varFalse); V_VT(&varFalse) = VT_BOOL; V_BOOL(&varFalse) = VARIANT_FALSE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; VariantInit(&varProxy); VariantInit(&varUrl); hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Specify proxy and URL. varProxy.vt = VT_BSTR; varProxy.bstrVal = SysAllocString(L"proxy_server:80"); varUrl.vt = VT_BSTR; varUrl.bstrVal = SysAllocString(L"*.microsoft.com"); hr = pIWinHttpRequest->SetProxy(HTTPREQUEST_PROXYSETTING_PROXY, varProxy, varUrl); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Get Response text. hr = pIWinHttpRequest->get_ResponseText(&bstrResponse); } if (SUCCEEDED(hr)) { // Print the response to a console. wprintf(L"%.256s",bstrResponse); } // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (varProxy.bstrVal) SysFreeString(varProxy.bstrVal); if (varUrl.bstrVal) SysFreeString(varUrl.bstrVal); if (bstrResponse) SysFreeString(bstrResponse); CoUninitialize(); return 0; } ``` -------------------------------- ### Setting an HTTP Proxy for a Cellular Connection Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttpconnectionsetproxyinfo This example demonstrates how to set an HTTP proxy for a cellular connection using the WinHttpConnectionSetProxyInfo function. Ensure the WINHTTP_CONNECTION_PROXY_INFO structure is correctly initialized. ```cpp WINHTTP_CONNECTION_PROXY_INFO proxyInfo = {}; proxyInfo.Version = WINHTTP_CONNECTION_PROXY_INFO_CURRENT_VERSION; proxyInfo.Switch = WINHTTP_CONNECTION_PROXY_INFO_SWITCH_CONFIG; proxyInfo.Config.pwszServer = L"proxy.contoso.com"; proxyInfo.Config.Port = 8080; DWORD dwError = WinHttpConnectionSetProxyInfo( L"Contoso Cellular", WINHTTP_CONNECTION_PROXY_TYPE_HTTP, &proxyInfo); ``` -------------------------------- ### JavaScript Example for Web Site Authentication Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/authentication-using-script This script demonstrates requesting a web resource with and without providing authentication credentials using WinHttpRequest. It's useful for testing authentication mechanisms. ```javascript // Load the WinHttpRequest object. var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); function getText1( ) { // Specify the target resource. WinHttpReq.open( "GET", "https://[authenticationSite]", false; // Send a request to the server and wait for a response. WinHttpReq.send( ); // Display the results of the request. WScript.Echo( "No Credentials: " ); WScript.Echo( WinHttpReq.Status + " " + WinHttpReq.StatusText); WScript.Echo( WinHttpReq.GetAllResponseHeaders); WScript.Echo( ); }; function getText2( ) { // HttpRequest SetCredentials flags HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0; // Specify the target resource. WinHttpReq.open( "GET", "https://[authenticationSite]", false ); // Set credentials for server. WinHttpReq.SetCredentials( "User Name", "Password", HTTPREQUEST_SETCREDENTIALS_FOR_SERVER); // It might also be necessary to supply credentials // to the proxy if you connect to the Internet // through a proxy that requires authentication. // Send a request to the server and wait for // a response. WinHttpReq.send( ); // Display the results of the request. WScript.Echo( "Credentials: " ); WScript.Echo( WinHttpReq.Status + " " + WinHttpReq.StatusText ); WScript.Echo( WinHttpReq.GetAllResponseHeaders( ) ); WScript.Echo( ); }; getText1( ); getText2( ); ``` -------------------------------- ### Set and View WinHTTP Options in JScript Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-option This JScript example shows how to instantiate a WinHttpRequest object, set options like user agent, URL, code page, and URL escaping, and then display their values. ```javascript // Define the constants used by the option property. WinHttpRequestOption_UserAgentString = 0; // Name of the user agent WinHttpRequestOption_URL = 1; // Current URL WinHttpRequestOption_URLCodePage = 2; // Code page WinHttpRequestOption_EscapePercentInURL = 3; // Convert percents // in the URL // Instantiate a WinHttpRequest object. var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); // Initialize an HTTP request. WinHttpReq.Open("GET", "https://www.microsoft.com", false); // Send the HTTP request. WinHttpReq.Send(); // Display the WinHTTP option values. WScript.Echo( 'User agent: '+ WinHttpReq.Option(WinHttpRequestOption_UserAgentString)); WScript.Echo( 'URL: '+ WinHttpReq.Option(WinHttpRequestOption_URL)); WScript.Echo( 'Code page: '+ WinHttpReq.Option(WinHttpRequestOption_URLCodePage)); WScript.Echo( 'Escape percents: ' +WinHttpReq.Option(WinHttpRequestOption_EscapePercentInURL)); ``` -------------------------------- ### Retrieve All Response Headers (JScript) Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-getallresponseheaders This JScript example shows how to instantiate a WinHttpRequest object, open a connection, send a request, and retrieve all response headers using the GetAllResponseHeaders method. ```javascript // Instantiate a WinHttpRequest object. var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); // Initialize an HTTP request. WinHttpReq.Open("GET", "https://www.microsoft.com", false); // Send the HTTP request. WinHttpReq.Send(); // Get all response headers. WScript.Echo( WinHttpReq.GetAllResponseHeaders()); ``` -------------------------------- ### Open HTTP Request Synchronously Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/retrieving-data-using-script Initializes an HTTP request using the GET verb for a specified URL. The 'false' parameter ensures the operation is synchronous. ```javascript // Create an HTTP request. WinHttpReq.Open("GET", "https://www.microsoft.com", false); ``` -------------------------------- ### Set WinHTTP Timeouts in C++ Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-settimeouts This C++ example demonstrates setting all WinHTTP time-outs to 30 seconds, opening an HTTP connection, sending a request, and reading the response. Ensure COM is initialized before use. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // variable for return value HRESULT hr; // initialize COM hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varFalse; VARIANT varEmpty; CLSID clsid; VariantInit(&varFalse); V_VT(&varFalse) = VT_BOOL; V_BOOL(&varFalse) = VARIANT_FALSE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Set Time-outs. hr = pIWinHttpRequest->SetTimeouts(30000, 30000, 30000, 30000); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Get Response text. hr = pIWinHttpRequest->GetAllResponseHeaders(&bstrResponse); } if (SUCCEEDED(hr)) { // Print response to console. wprintf(L"%.256s",bstrResponse); } // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (bstrResponse) SysFreeString(bstrResponse); CoUninitialize(); return 0; } ``` -------------------------------- ### Setting Connection Manager Policies Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttpconnectionsetpolicyentries This example demonstrates how to delete existing connection manager policies and then set a new on-demand policy for all apps connecting to hosts matching '*.contoso.com'. ```APIDOC ## WinHttpConnectionSetPolicyEntries ### Description Sets connection manager policy entries for WinHTTP. This function can be used to configure on-demand connection policies, specifying which hosts and applications should use specific connection names. ### Method WinHttp API function call ### Parameters #### Function Parameters - **hSession** (HINTERNET) - Handle to a WinHTTP session. - **PolicyTag** (DWORD) - Specifies the type of policy to set. Use `TAG_WINHTTP_CONNECTION_POLICY_TAG_CONNECTION_MANAGER` for connection manager policies. - **pList** (const WINHTTP_CONNECTION_POLICY_ENTRY_LIST*) - Pointer to a `WINHTTP_CONNECTION_POLICY_ENTRY_LIST` structure that contains the policy entries to set. ### WINHTTP_CONNECTION_POLICY_ENTRY_LIST Structure - **nEntries** (DWORD) - The number of entries in the `pPolicyEntries` array. - **pPolicyEntries** (const WINHTTP_CONNECTION_POLICY_ENTRY*) - Pointer to an array of `WINHTTP_CONNECTION_POLICY_ENTRY` structures. ### WINHTTP_CONNECTION_POLICY_ENTRY Structure - **pwszHost** (PCWSTR) - Pointer to a null-terminated string that specifies the host name. Wildcards are supported. - **pwszAppId** (PCWSTR) - Pointer to a null-terminated string that specifies the application identifier. Use `L"*"` to apply to all applications. - **dwPolicyEntryFlags** (DWORD) - Flags that specify the policy entry behavior. Use `WINHTTP_CONNECTION_POLICY_ENTRY_ONDEMAND` for on-demand policies. - **nConnections** (DWORD) - The number of connection names in the `ppwszConnections` array. - **ppwszConnections** (PCWSTR*) - Pointer to an array of null-terminated strings, where each string is a connection name to be used. ### Request Example ```c++ // First delete existing connection manager policies WinHttpConnectionDeletePolicyEntries(hSession, TAG_WINHTTP_CONNECTION_POLICY_TAG_CONNECTION_MANAGER); // Then set new policies WINHTTP_CONNECTION_POLICY_ENTRY entry = {}; entry.pwszHost = L"*.contoso.com"; entry.pwszAppId = L"*"; entry.dwPolicyEntryFlags = WINHTTP_CONNECTION_POLICY_ENTRY_ONDEMAND; PCWSTR connectionName = L"Contoso Cellular"; entry.nConnections = 1; entry.ppwszConnections = &connectionName; WINHTTP_CONNECTION_POLICY_ENTRY_LIST list = {}; list.pPolicyEntries = &entry; list.nEntries = 1; WinHttpConnectionSetPolicyEntries( hSession, TAG_WINHTTP_CONNECTION_POLICY_TAG_CONNECTION_MANAGER, &list); ``` ### Requirements - **Header**: N/A - **Library**: winhttp.lib - **DLL**: winhttp.dll ``` -------------------------------- ### Set Proxy Settings in JScript Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-setproxy This JScript example shows how to configure proxy settings for WinHttpRequest. It defines proxy constants and uses the SetProxy method to specify a proxy server and bypass list. ```javascript // HttpRequest SetCredentials flags. HTTPREQUEST_PROXYSETTING_DEFAULT = 0; HTTPREQUEST_PROXYSETTING_PRECONFIG = 0; HTTPREQUEST_PROXYSETTING_DIRECT = 1; HTTPREQUEST_PROXYSETTING_PROXY = 2; // Instantiate a WinHttpRequest object. var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); // Use proxy_server for all requests outside of // the microsoft.com domain. WinHttpReq.SetProxy( HTTPREQUEST_PROXYSETTING_PROXY, "proxy_server:80", "*.microsoft.com"); // Initialize an HTTP request. WinHttpReq.Open("GET", "https://www.microsoft.com", false); // Send the HTTP request. WinHttpReq.Send(); // Display the response text. WScript.Echo( WinHttpReq.ResponseText); ``` -------------------------------- ### Set and View WinHTTP Options in C++ Source: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-option This C++ example demonstrates how to initialize COM, create a WinHttpRequest object, set the user agent string, send a request, and retrieve various options including the user agent, URL, URL code page, and URL escaping preference. ```cpp #include #include #include #include "httprequest.h" #pragma comment(lib, "ole32.lib") #pragma comment(lib, "oleaut32.lib") // IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} }; int main() { // Variable for return value HRESULT hr; // Initialize COM hr = CoInitialize( NULL ); IWinHttpRequest * pIWinHttpRequest = NULL; BSTR bstrResponse = NULL; VARIANT varFalse; VARIANT varEmpty; VARIANT varUserAgent; CLSID clsid; VariantInit(&varFalse); V_VT(&varFalse) = VT_BOOL; V_BOOL(&varFalse) = VARIANT_FALSE; VariantInit(&varEmpty); V_VT(&varEmpty) = VT_ERROR; varUserAgent.vt = VT_BSTR; varUserAgent.bstrVal = NULL; hr = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &clsid); if (SUCCEEDED(hr)) { hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (void **)&pIWinHttpRequest); } if (SUCCEEDED(hr)) { // Open WinHttpRequest. BSTR bstrMethod = SysAllocString(L"GET"); BSTR bstrUrl = SysAllocString(L"https://microsoft.com"); hr = pIWinHttpRequest->Open(bstrMethod, bstrUrl, varFalse); SysFreeString(bstrMethod); SysFreeString(bstrUrl); } if (SUCCEEDED(hr)) { // Specify the user agent. varUserAgent.vt = VT_BSTR; varUserAgent.bstrVal = SysAllocString( L"A WinHttpRequest Example Program"); //varUserAgent is L"A WinHttpRequest Example Program"); hr = pIWinHttpRequest->put_Option( WinHttpRequestOption_UserAgentString, varUserAgent); } if (SUCCEEDED(hr)) { // Send Request. hr = pIWinHttpRequest->Send(varEmpty); } if (SUCCEEDED(hr)) { // Get user agent string. hr = pIWinHttpRequest->get_Option( WinHttpRequestOption_UserAgentString, &varUserAgent); // Print response to console. wprintf(L"%s\n\n", varUserAgent.bstrVal); // Get URL. hr = pIWinHttpRequest->get_Option(WinHttpRequestOption_URL, &varUserAgent); // Print response to console. wprintf(L"%s\n\n", varUserAgent.bstrVal); // Get URL Code Page. hr = pIWinHttpRequest->get_Option( WinHttpRequestOption_URLCodePage, &varUserAgent); // Print response to console. wprintf(L"%u\n\n", varUserAgent.lVal); // Convert percent symbols to escape sequences. hr = pIWinHttpRequest->get_Option( WinHttpRequestOption_EscapePercentInURL, &varUserAgent); // Print response to console. wprintf(L"%s\n", varUserAgent.boolVal?L"True":L"False"); } // Release memory. if (pIWinHttpRequest) pIWinHttpRequest->Release(); if (bstrResponse) SysFreeString(bstrResponse); if (varUserAgent.bstrVal) SysFreeString(varUserAgent.bstrVal); CoUninitialize(); return 0; } ```