WinHttp 参考资料

WinHttpRequest,WinHttp.WinHttpRequest.5.1,WinHttpRequest使用,WinHttpRequest参考,WinHttpRequest说明文档

WinHttp; // Microsoft WinHTTP Services, version 5.1

Alias HTTPREQUEST_PROXY_SETTING;
  I4

Alias HTTPREQUEST_SETCREDENTIALS_FLAGS;
  I4

Enum WinHttpRequestOption; // WinHttpRequest Options
GUID={12782009-FE90-4877-9730-E5E183669B19};
  WinHttpRequestOption_UserAgentString = 0;
  WinHttpRequestOption_URL = 1;
  WinHttpRequestOption_URLCodePage = 2;
  WinHttpRequestOption_EscapePercentInURL = 3;
  WinHttpRequestOption_SslErrorIgnoreFlags = 4;
  WinHttpRequestOption_SelectCertificate = 5;
  WinHttpRequestOption_EnableRedirects = 6;
  WinHttpRequestOption_UrlEscapeDisable = 7;
  WinHttpRequestOption_UrlEscapeDisableQuery = 8;
  WinHttpRequestOption_SecureProtocols = 9;
  WinHttpRequestOption_EnableTracing = 10;
  WinHttpRequestOption_RevertImpersonationOverSsl = 11;
  WinHttpRequestOption_EnableHttpsToHttpRedirects = 12;
  WinHttpRequestOption_EnablePassportAuthentication = 13;
  WinHttpRequestOption_MaxAutomaticRedirects = 14;
  WinHttpRequestOption_MaxResponseHeaderSize = 15;
  WinHttpRequestOption_MaxResponseDrainSize = 16;
  WinHttpRequestOption_EnableHttp1_1 = 17;
  WinHttpRequestOption_EnableCertificateRevocationCheck = 18;

Enum WinHttpRequestAutoLogonPolicy;
GUID={9D8A6DF8-13DE-4B1F-A330-67C719D62514};
  AutoLogonPolicy_Always = 0;
  AutoLogonPolicy_OnlyIfBypassProxy = 1;
  AutoLogonPolicy_Never = 2;

Enum WinHttpRequestSslErrorFlags;
GUID={152A1CA2-55A9-43A3-B187-0605BB886349};
  SslErrorFlag_UnknownCA = $100;
  SslErrorFlag_CertWrongUsage = $200;
  SslErrorFlag_CertCNInvalid = $1000;
  SslErrorFlag_CertDateInvalid = $2000;
  SslErrorFlag_Ignore_All = $3300;

Enum WinHttpRequestSecureProtocols;
GUID={6B2C51C1-A8EA-46BD-B928-C9B76F9F14DD};
  SecureProtocol_SSL2 = 8;
  SecureProtocol_SSL3 = 32;
  SecureProtocol_TLS1 = 128;
  SecureProtocol_ALL = 168;

Dispatch IWinHttpRequest; // IWinHttpRequest Interface
GUID={016FE2EC-B2C8-45F8-B23B-39E53A75396B};
  function QueryInterface(riid:^GUID; out ppvObj:^^void);
  function AddRef: UI4;
  function Release: UI4;
  function GetTypeInfoCount(out pctinfo:^UINT);
  function GetTypeInfo(itinfo:UINT; lcid:UI4; out pptinfo:^^void);
  function GetIDsOfNames(riid:^GUID; rgszNames:^^I1; cNames:UINT; lcid:UI4; out rgdispid:^I4);
  function Invoke(dispidMember:I4; riid:^GUID; lcid:UI4; wFlags:UI2; pdispparams:^DISPPARAMS; out pvarResult:^variant; out pexcepinfo:^EXCEPINFO; out puArgErr:^UINT);
  function SetProxy(ProxySetting:HTTPREQUEST_PROXY_SETTING; [ProxyServer:variant; BypassList:variant]);
  function SetCredentials(UserName:BSTR; Password:BSTR; Flags:HTTPREQUEST_SETCREDENTIALS_FLAGS);
  function Open(Method:BSTR; Url:BSTR; [Async:variant]);
  function SetRequestHeader(Header:BSTR; Value:BSTR);
  function GetResponseHeader(Header:BSTR): BSTR;
  function GetAllResponseHeaders: BSTR;
  function Send([Body:variant]);
  property-get Status: I4;
  property-get StatusText: BSTR;
  property-get ResponseText: BSTR;
  property-get ResponseBody: variant;
  property-get ResponseStream: variant;
  property-get Option(Option:WinHttpRequestOption): variant;
  property-put Option(Option:WinHttpRequestOption; variant);
  function WaitForResponse([Timeout:variant]): bool;
  function Abort;
  function SetTimeouts(ResolveTimeout:I4; ConnectTimeout:I4; SendTimeout:I4; ReceiveTimeout:I4);
  function SetClientCertificate(ClientCertificate:BSTR);
  function SetAutoLogonPolicy(AutoLogonPolicy:WinHttpRequestAutoLogonPolicy);

Interface IWinHttpRequestEvents; // IWinHttpRequestEvents Interface
GUID={F97F4E15-B787-4212-80D1-D380CBBF982E};
  function OnResponseStart(Status:I4; ContentType:BSTR);
  function OnResponseDataAvailable(Data:^^UI1);
  function OnResponseFinished;
  function OnError(ErrorNumber:I4; ErrorDescription:BSTR);

Class WinHttpRequest; // WinHttpRequest component
GUID={2087C2F4-2CEF-4953-A8AB-66779B670495};
  function QueryInterface(riid:^GUID; out ppvObj:^^void);
  function AddRef: UI4;
  function Release: UI4;
  function GetTypeInfoCount(out pctinfo:^UINT);
  function GetTypeInfo(itinfo:UINT; lcid:UI4; out pptinfo:^^void);
  function GetIDsOfNames(riid:^GUID; rgszNames:^^I1; cNames:UINT; lcid:UI4; out rgdispid:^I4);
  function Invoke(dispidMember:I4; riid:^GUID; lcid:UI4; wFlags:UI2; pdispparams:^DISPPARAMS; out pvarResult:^variant; out pexcepinfo:^EXCEPINFO; out puArgErr:^UINT);
  function SetProxy(ProxySetting:HTTPREQUEST_PROXY_SETTING; [ProxyServer:variant; BypassList:variant]);
  function SetCredentials(UserName:BSTR; Password:BSTR; Flags:HTTPREQUEST_SETCREDENTIALS_FLAGS);
  function Open(Method:BSTR; Url:BSTR; [Async:variant]);
  function SetRequestHeader(Header:BSTR; Value:BSTR);
  function GetResponseHeader(Header:BSTR): BSTR;
  function GetAllResponseHeaders: BSTR;
  function Send([Body:variant]);
  property-get Status: I4;
  property-get StatusText: BSTR;
  property-get ResponseText: BSTR;
  property-get ResponseBody: variant;
  property-get ResponseStream: variant;
  property-get Option(Option:WinHttpRequestOption): variant;
  property-put Option(Option:WinHttpRequestOption; variant);
  function WaitForResponse([Timeout:variant]): bool;
  function Abort;
  function SetTimeouts(ResolveTimeout:I4; ConnectTimeout:I4; SendTimeout:I4; ReceiveTimeout:I4);
  function SetClientCertificate(ClientCertificate:BSTR);
  function SetAutoLogonPolicy(AutoLogonPolicy:WinHttpRequestAutoLogonPolicy);
  function OnResponseStart(Status:I4; ContentType:BSTR);
  function OnResponseDataAvailable(Data:^^UI1);
  function OnResponseFinished;
  function OnError(ErrorNumber:I4; ErrorDescription:BSTR);

示例:

同步获取数据

 

function getText(strURL)
{
    var strResult;
    
    try
    {
        // Create the WinHTTPRequest ActiveX Object.
        var WinHttpReq = new ActiveXObject(
                                  "WinHttp.WinHttpRequest.5.1");
        
        //  Create an HTTP request.
        var temp = WinHttpReq.Open("GET", strURL, false);
        //  Send the HTTP request.
        WinHttpReq.Send();
        
        //  Retrieve the response text.
        strResult = WinHttpReq.ResponseText;
    }
    catch (objError)
    {
        strResult = objError + "\n"
        strResult += "WinHTTP returned error: " + 
            (objError.number & 0xFFFF).toString() + "\n\n";
        strResult += objError.description;
    }
    
    //  Return the response text.
    return strResult;
}
WScript.Echo(getText(http://www.microsoft.com/default.htm));

改为异步获取

//  Create a HTTP request.
    var temp = WinHttpReq.Open("GET", strURL, true);//此处由false改成true

在访问ResponseText之前调用WaitForResponse方法以确保获取的是完整的响应。

//  Send the HTTP request.
    WinHttpReq.Send();
        
    // Wait for the entire response.
    WinHttpReq.WaitForResponse();
        
    //  Retrieve the response text.
    strResult = WinHttpReq.ResponseText;

你可能感兴趣的:(WinHttpRequest)