原文:https://ptfrontline.wordpress.com/2009/12/23/loadrunner-http-401-authentication/
譯文(轉載請标注smooth的部落格):
在筆者最近做的一個項目中,偶然發現了一個HTTP驗證機制方面的有趣問題。
本人有一個裝置對裝置(M2M)的接口,在這個接口上使用者(用戶端)使用HTTP驗證來确認是他們自己向伺服器傳輸資料。在這種情況下,沒有收到來自伺服器對于HTTP
401的響應,因為該使用者(用戶端)在初始請求中包含了身份驗證的資訊。
筆者發現LoadRunner不會出現相同的情況。它會在實際發送驗證資訊之前,先等待HTTP 401響應。如果我們模拟一個專為使用者服務的網絡服務,那麼這是一個正确的動作,但是在本次案例中,LoadRunner是執行了一個額外的POST請求,而這導緻了災難性的結果,因為這樣我一共執行了兩個額外的POST請求!
這帶來的後果是,我們向整個網絡送出了雙倍的資料量、向伺服器送出了雙倍的請求,并是以使連接配接到伺服器的實際負載和吞吐量加倍!
為了解決這個問題,筆者隻能在初始請求中包含驗證表頭。為實作這一操作,筆者使用了Base64Encoder (Base64編碼/解碼工具)制作所需的HTTP驗證基礎表頭資訊,然後使用web_add_header功能添加到請求中。
以下是本項目中展現如何建立和使用自定義驗證表頭的代碼段:
// Must remove stdauth mechanism since this causes 2POSTS instead of one
// web_set_user( "{Username}","{Password}", "{Domain}:8000" );
// Create Base64 encoded string
b64_encode_string( "{Username}:{Password}","BasicAuth" ); //b64_encode_string函數在網上能搜尋到,在此不另提供
// Add HTTP Authorization header "Authorization:Basic XXXXXXXXXXXXXXXXXX==\r\n"
web_add_header("Authorization",lr_eval_string("Basic {BasicAuth}"));
lr_start_transaction("Custom_HTTP_Auth");
web_custom_request("Custom_HTTP_Auth",
"Method=POST",
"EncType=text/xml;charset=\"UTF-8\"",
"URL=http://{Domain}/{Path}/{Document}"
"BodyFilePath=XML.xml",
LAST);
lr_end_transaction("Custom_HTTP_Auth",LR_AUTO);
此外我還發現了另外一種可能有效的辦法。這種方法目前隻用LR11.03試驗過有效。參見如下:
// Standard Web Set User here
web_set_user( "{Username}","{Password}", "{Domain}:8000" );
// Set UNDOCUMENTED socket option to make LR sendAuthentication headers with every request to the domain
web_set_sockets_option("INITIAL_BASIC_AUTH","1");
// Now the HTTP request adds the Authentication headerautomatically without receiving
// a HTTP 401 first and then sending the auth header.
LAST);