SetProxy Method |
Description
Sets proxy server information.
C/C++ Syntax
PowerBASIC Syntax
Parameter
ProxySetting
[in] A value of type LONG that receives the flags that control this method. Can be one of the following values.
vProxyServer
[in, optional] A value of type Variant that is set to a proxy server string when ProxySetting equals HTTPREQUEST_PROXYSETTING_PROXY.
vBypassList
[in, optional] A value of type Variant that is set to a domain bypass list string when ProxySetting equals HTTPREQUEST_PROXYSETTING_PROXY.
Return Value
This method does not return a value.
OBJRESULT
Returns S_OK if successful or an error value otherwise.
Remarks
Enables the calling application to specify use of default proxy information (configured by the proxy configuration tool) or to override Proxycfg.exe. This method must be called before calling the Send method. If this method is called after the Send method, it has no effect.
IWinHttpRequest passes these parameters to Microsoft Windows HTTP Services (WinHTTP).
The following code example shows how to set the proxy settings for a particular proxy server, open an HTTP connection, send an HTTP request, and read the response text. It assumes that you have a command button named Command1, a text box named Text1 on your form, and that the Multiline property of Text1 is set to TRUE. These proxy settings work only if you have a proxy server named "proxy_server" that uses port 80 and your computer can bypass the proxy server when the host name ends with ".microsoft.com".
#INCLUDE
ONCE "win32api.inc" DIM pHttpReq AS IWinHttpRequest DIM bstrResponseText AS STRING
' Create an instance of the HTTP service pHttpReq = NEWCOM "WinHttp.WinHttpRequest.5.1"
' Set proxy settings. pHttpReq.SetProxy %HTTPREQUEST_PROXYSETTING_PROXY, _ "proxy_server:80", "*.microsoft.com"
' Open an HTTP connection pHttpReq.Open UCODE$("GET"), UCODE$("http://microsoft.com"), %FALSE
' Send the HTTP Request pHttpReq.Send
' Get the response text bstrResponseText = pHttpReq.ResponseText MSGBOX ACODE$(bstrResponseText)
Example Code [C++]
#include "stdafx.h" #include "objbase.h"
#include "httprequest.h"
// IID for IWinHttpRequest. const IID IID_IWinHttpRequest = { 0x06f29373, 0x5c5a, 0x4b54, {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e} };
int main(int argc, char* argv[]) { // 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;
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"http://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; }
Example Code [Script]
// 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", "http://www.microsoft.com", false);
// Send the HTTP request. WinHttpReq.Send();
// Display the response text. WScript.Echo( WinHttpReq.ResponseText);
Example Code [Visual Basic]
Option Explicit
'WinHttpRequest proxy settings. Const HTTPREQUEST_PROXYSETTING_DEFAULT = 0 Const HTTPREQUEST_PROXYSETTING_PRECONFIG = 0 Const HTTPREQUEST_PROXYSETTING_DIRECT = 1 Const HTTPREQUEST_PROXYSETTING_PROXY = 2
Private Sub Command1_Click() Dim HttpReq As Object
' Create the WinHTTPRequest ActiveX Object. Set HttpReq = New WinHttpRequest
' Switch the mouse pointer to an hourglass while busy. MousePointer = vbHourglass
'Set proxy settings. HttpReq.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, _ "proxy_server:80", "*.microsoft.com"
' Open an HTTP connection. HttpReq.Open "GET", "http://microsoft.com", False
' Send the HTTP Request. HttpReq.Send
' Get all response text. Text1.Text = HttpReq.ResponseText
' Switch the mouse pointer back to default. MousePointer = vbDefault
End Sub
|