2015-09-30 44 views
3

我曾經在終端中使用curl命令來訪問php網頁,以測試網頁中的一些API。它工作正常。如何使用curl命令行通過HTTP基本認證來訪問網頁

例如:

curl www.somesite.com -d parmetername=value 

現在這個頁面有基本的HTTP認證。我知道我只需要添加-u來提供用戶名和密碼。我也是一派,幾乎每個人都建議做同樣如下:

curl -u username:password www.somesite.com -d parmetername=value 
curl --user username:password www.somesite.com -d parmetername=value 

curl http://username:[email protected] -d parmetername=value 

我曾嘗試他們兩個,他們沒有工作。我得到了如下相同的錯誤信息:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>401 Authorization Required</title> 
</head><body> 
<h1>Authorization Required</h1> 
<p>This server could not verify that you 
are authorized to access the document 
requested. Either you supplied the wrong 
credentials (e.g., bad password), or your 
browser doesn't understand how to supply 
the credentials required.</p> 
<p>Additionally, a 401 Authorization Required 
error was encountered while trying to use an ErrorDocument to handle the request.</p> 
<hr> 
<address>Apache/2.2.29 (Unix) mod_ssl/2.2.29 OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Server at www.somesite.com Port 80</address> 
</body></html> 

我試圖通過瀏覽器訪問網站。在提示窗口中輸入用戶名和密碼後。我可以訪問這個網站。

有人知道這裏發生了什麼嗎? 謝謝。

+0

請重新檢查在URL的用戶名和密碼,你嘗試,因爲這是爲我工作(個人服務器上)。 – watery

+0

可能重複的http://stackoverflow.com/questions/3044315/how-to-set-the-authorization-header-using-curl – Jan

+0

我試了幾次,並在不同的計算機上......不工作......我是思考如果這必須做一些與服務器設置... – user3293338

回答

0

當我遇到這個問題時,事實證明,服務器不會接受認證方案'基本',但curl使用'基本',除非告知不同。

查看有關驗證方案的捲曲命令行選項。您可以使用--basic--digest,--ntlm,--negotiate--anyauth。詳情請參閱curl的手冊頁。

雖然--anyauth--negotiate聽起來像curl可以與服務器協商正確的身份驗證方法,但他們都不適合我。相反,我必須明確地使用--digest,就像這樣:

curl --digest -u username:password -d parm1=value1&parm2=value2&submit=true https://www.somesite.com 
相關問題