本節示範如何撰寫腳本,以使用 winHttpRequest 物件,從需要 HTTP 驗證的伺服器存取數據。
必要條件和需求
除了Microsoft JScript 的工作知識之外,此範例還需要下列各項:
- Microsoft Windows 軟體開發工具包 (SDK) 的目前版本。
- 如果您的因特網連線是透過 Proxy 伺服器,則 Proxy 組態工具可建立 Microsoft Windows HTTP 服務 (WinHTTP) 的 Proxy 設定。 如需詳細資訊,請參閱 Proxycfg.exe、Proxy 組態工具。
- 熟悉 網路術語 和概念。
使用驗證存取網站
若要建立示範驗證的腳本,請執行下列動作:
開啟文本編輯器,例如Microsoft記事本。
將下列程式代碼複製到文本編輯器後,將 “[authenticationSite]” 取代為適當的文字,以指定需要 HTTP 驗證的網站 URL。
// 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( );
將檔案儲存為 「Auth.js」。
在命令提示字元中,輸入 「cscript Auth.js」,然後按 ENTER。
您現在有一個要求資源兩種不同方式的程式。 第一個方法會要求資源,而不需提供認證。 傳回 401 狀態代碼,表示伺服器需要驗證。 回應標頭也會顯示,看起來應該如下所示:
Connection: Keep-Alive
Content-Length: 0
Date: Fri, 27 Apr 2001 01:47:18 GMT
Content-Type: text/html
Server: Microsoft-IIS/5.0
WWW-Authenticate: NTLM
WWW-Authenticate: Negotiate
Cache-control: private
雖然回應指出資源存取遭到拒絕,但它仍會傳回數個標頭,以提供資源的一些資訊。 名為 「WWW-Authenticate」 的標頭表示伺服器需要此資源的驗證。 如果有名為 「Proxy-Authenticate」 的標頭,表示 Proxy 伺服器需要驗證。 每個驗證標頭都包含可用的驗證配置,有時則是領域。 領域值會區分大小寫,並定義應接受相同認證的一組伺服器或 Proxy。
有兩個名為 “WWW-Authentication” 的標頭,表示支援多個驗證配置。 如果您呼叫 GetResponseHeader 方法來尋找 “WWW-Authenticate” 標頭,此方法只會傳回該名稱的第一個標頭內容。 在此情況下,它會傳回 「NTLM」 的值。 若要確保處理所有出現的標頭,請改用 GetAllResponseHeaders 方法。
第二個方法呼叫會要求相同的資源,但會先呼叫 SetCredentials 方法來提供驗證認證。 下列程式代碼區段示範如何使用此方法。
WinHttpReq.SetCredentials( "User Name", "Password",
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER);
這個方法會將使用者名稱設定為 「UserName」、密碼設定為 「Password」,並指出授權認證會套用至資源伺服器。 驗證認證也可以傳送至 Proxy。
提供認證時,伺服器會傳回 200 狀態代碼,指出可以擷取檔。
檢查狀態代碼
上述範例為指示,但用戶必須明確提供認證。 當必要時提供認證的應用程式,且不需要時不會提供認證會比較有用。 若要實作執行這項作業的功能,您必須修改範例,以檢查回應傳回的狀態代碼。
如需可能狀態代碼的完整清單以及描述,請參閱 HTTP 狀態代碼。 不過,在此範例中,您應該只會遇到三個程序代碼的其中一個。 狀態代碼 200 表示資源可供使用,且正在與回應一起傳送。 狀態代碼 401 表示伺服器需要驗證。 狀態代碼 407 表示 Proxy 需要驗證。
將 “getText2” 函式取代為下列程式代碼來修改您在最後一節中建立的範例(將 “[authenticationSite]” 取代為您自己的文字,以指定需要 HTTP 驗證的網站 URL):
function getText2() {
WScript.Echo( "Credentials: " );
// HttpRequest SetCredentials flags.
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0;
HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1;
// Specify the target resource.
var targURL = "https://[authenticationSite]";
WinHttpReq.open( "GET", targURL, false );
var Done = false;
var Attempts = 0;
do
{
// Keep track of the number of request attempts.
Attempts++;
// Send a request to the server and wait for a response.
WinHttpReq.send( );
// Obtain the status code from the response.
var Status = WinHttpReq.Status;
switch (Status)
{
// A 200 status indicates that the resource was retrieved.
case 200:
Done = true;
break;
// A 401 status indicates that the server
// requires authentication.
case 401:
WScript.Echo( "Requires Server UserName and Password." );
// Specify the target resource.
WinHttpReq.open( "GET", targURL, false );
// Set credentials for the server.
WinHttpReq.SetCredentials( "User Name",
"Password",
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER);
break;
// A 407 status indicates that the proxy
// requires authentication.
case 407:
WScript.Echo( "Requires Proxy UserName and Password." );
// Specify the target resource.
WinHttpReq.open( "GET", targURL, false );
// Set credentials for the proxy.
WinHttpReq.SetCredentials( "User Name",
"Password",
HTTPREQUEST_SETCREDENTIALS_FOR_PROXY );
break;
}
} while( ( !Done ) && ( Attempts < 3 ) );
// Display the results of the request.
WScript.Echo( WinHttpReq.Status + " " + WinHttpReq.StatusText );
WScript.Echo( WinHttpReq.GetAllResponseHeaders( ) );
WScript.Echo( );
};
同樣地,儲存並執行檔案。 第二個方法仍會擷取檔,但只會在必要時提供認證。 “getText2” 函式會執行 Open 和 Send 方法,就好像不需要驗證一樣。 狀態會使用 Status 屬性擷取,而 switch 語句會響應產生的狀態代碼。 如果狀態為 401(伺服器需要驗證)或 407(Proxy 需要驗證),則會再次執行 Open 方法。 後面接著 SetCredentials 方法,以設定使用者名稱和密碼。 然後,程式代碼會迴圈回 Send 方法。 如果三次嘗試之後,無法成功擷取資源,則函式會停止執行。
相關主題
-
WinHTTP 中的 驗證