我想获取网址https://example.com/xyz的http响应代码但是要访问此网址,我必须先将用户名密码传递给https://example.com
我尝试跑步
#!/bin/sh
curl -u username:password https://example.com
curl -s -o /dev/null -w "{http_code}" https://example.com/xyz
但是我正在获得401次未经授权的访问
没有关于网页,服务等的进一步信息和知识,可能无法给出完整和/或正确的答案。但是,由于过去我遇到过类似的问题,因此我将在此处提供一些提示。
根据您的简短描述,我假设命令类似
curl --silent -u "${USERNAME}:${PASSWORD}" "https://example.com/xyz" --output /dev/null --write-out "{http_code}\n"
curl --silent "https://${USERNAME}:${PASSWORD}@example.com/xyz" --output /dev/null --write-out "{http_code}\n"
将不是正常工作,对吧?
取决于页面和服务,可能需要使会话保持活动状态,在登录后存储ID或令牌并进一步使用它。即我正在使用的服务
curl -u "${USERNAME}:${PASSWORD}" -H 'Connection: keep-alive' -D - "https://example.com" | grep "ID=[a-z0-9]*\|TOKEN = '"
因此,我后来比“]重用了“ ID和令牌”>
curl 'https://example.com/xyz' -H 'Cookie: ID=${ID}' -H 'Origin: https://example.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4' -H 'X-CSRF: ${TOKEN}' -H 'User-Agent: cURL' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: */*' -H 'Referer: https://example.com/xyz' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive
并获得页面的完整内容。