2014-10-31 181 views
2

我正在嘗試使用LWP :: UserAgent設置憑據,但我無法登錄。$username$passwd是正確的。我不明白我應該把什麼放在3rd argument(根據dos $realm,這裏是Authentication)。這裏摘錄:LWP :: UserAgent和登錄憑證

my $browser = LWP::UserAgent->new(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/31.2.0', 
$browser->credentials("domain.com:80", "Authentication",$username, $passwd); 
my $response=$browser->get("http://domain.com/page"); 

print $response->content; 
+0

「Realm」是否與服務器所要求的一致?如果你做'憑證($用戶名,$密碼);'它是否工作呢? – Sobrique 2014-10-31 15:14:43

+0

@Sobrique這是我的問題......我說我不知道​​我應該放在那裏......憑證($ username,$ passwd)也不能工作。 – user897237 2014-10-31 15:17:29

+3

關閉瀏覽器並重新打開它。導航到「http:// domain.com/page」。如果該網站使用基本身份驗證,則應該看到一個類似於「http://domain.com請求的用戶名和密碼」的彈出窗口。該網站說:「富吧」。在這種情況下,'foo bar'就是領域。 – ThisSuitIsBlackNot 2014-10-31 15:24:37

回答

3

我糾正境界爲@ThisSuitIsBlackNot建議:

Close your browser and reopen it. Navigate to http://domain.com/page. 
If the site is using basic authentication, 
you should get a popup that says something like 
A username and password are being requested by 
http://domain.com. The site says: "foo bar". 
In this case, foo bar is the realm. 

然後我就能夠登錄,但頁面是空的。 所以我添加了一個cookie:

$browser->cookie_jar({ file => ".mycookies.txt" }); 
+0

無需使用單獨的瀏覽器。如果你安裝了LWP,你可能有腳本'GET'和'HEAD'。對受保護的資源執行「HEAD」操作,您將在**主機**:**端口**:獲得一個提示輸入**域的用戶名**。 – david 2016-01-14 15:57:03