【问题标题】:Get http response code to url that with username password获取带有用户名密码的 url 的 http 响应代码
【发布时间】:2020-03-16 12:07:14
【问题描述】:

我想获取 url 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 未经授权的访问

【问题讨论】:

  • usernamepassword 之间的冒号 (:) 后面有一个空格。
  • 不是这里的错字,运行时没问题

标签: bash shell curl http-status-code-401 http-response-codes


【解决方案1】:

如果没有关于网页、服务等的更多信息和知识,可能无法给出完整和/或正确的答案。但是,由于我过去遇到过类似的问题,因此我将在这里提供一些提示。

根据您非常简短的描述,我假设像

这样的命令
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 或 TOKEN 并进一步使用它。 IE。对于我正在使用的服务

curl -u "${USERNAME}:${PASSWORD}" -H 'Connection: keep-alive' -D - "https://example.com" |  grep "ID=[a-z0-9]*\|TOKEN = '"

所以我后来“重用”了 ID 和 TOKEN,而不是像

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

并获取页面的全部内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 2019-08-14
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多