如何使用curl命令行访问具有HTTP基本身份验证的网页

use*_*338 5 curl

我曾经在终端中使用curl命令访问php网页来测试网页中的一些API。效果很好。

例如:

curl www.somesite.com -d parmetername=value 
Run Code Online (Sandbox Code Playgroud)

现在这个页面已经有了基本的http认证。我知道我只需要添加 -u 来提供用户名和密码。我还用谷歌搜索,几乎每个人都建议执行以下操作:

curl -u username:password www.somesite.com -d parmetername=value
curl --user username:password www.somesite.com -d parmetername=value 
Run Code Online (Sandbox Code Playgroud)

或者

curl http://username:password@www.somesite.com -d parmetername=value
Run Code Online (Sandbox Code Playgroud)

我已经尝试过它们两个,但它们都不起作用。我收到了与以下相同的错误消息:

<!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>
Run Code Online (Sandbox Code Playgroud)

我尝试通过浏览器访问该网站。在提示窗口中输入用户名和密码后。我可以访问这个网站。

有谁知道这是怎么回事?谢谢。

not*_*vvy 1

当我遇到这个问题时,结果发现服务器不接受身份验证方案“Basic”,但curl 使用“Basic”,除非另有说明。

查看有关身份验证方案的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 
Run Code Online (Sandbox Code Playgroud)