- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我意识到 Curl {redirect_url} 并不总是显示相同的重定向 URL。例如,如果 URL header 是 Location: https:/\example.com
这将重定向到 https:/\example.com
但 curl {redirect_url} 显示 redirect_url: https://host-domain.com/https:/\example.com
并且它不会显示响应真实位置 header 。 (我喜欢看到真正的 location:
结果。)
这是我正在使用的 BASH:
#!/bin/bash
# Usage: urls-checker.sh domains.txt
FILE="$1"
while read -r LINE; do
# read the response to a variable
response=$(curl -H 'Cache-Control: no-cache' -s -k --max-time 2 --write-out '%{http_code} %{size_header} %{redirect_url} ' "$LINE")
# get the title
title=$(sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'<<<"$response")
# read the write-out from the last line
read -r http_code size_header redirect_url < <(tail -n 1 <<<"$response")
printf "***Url: %s\n\n" "$LINE"
printf "Status: %s\n\n" "$http_code"
printf "Size: %s\n\n" "$size_header"
printf "Redirect-url: %s\n\n" "$redirect_url"
printf "Title: %s\n\n" "$title"
# -c 20 only shows the 20 first chars from response
printf "Body: %s\n\n" "$(head -c 100 <<<"$response")"
done < "${FILE}"
如何printf "Redirect-url:
原始请求的 location: header
而不必使用 redirect_url
?
最佳答案
要读取服务器返回的确切 Location
header 字段值,您可以使用 -i
/--include
选项,结合 grep
。
例如:
$ curl 'http://httpbin.org/redirect-to?url=http:/\example.com' -si | grep -oP 'Location: \K.*'
http:/\example.com
或者,如果您想读取所有headers、content 和--write-out
变量行(根据您的脚本):
response=$(curl -H 'Cache-Control: no-cache' -s -i -k --max-time 2 --write-out '%{http_code} %{size_header} %{redirect_url} ' "$url")
# break the response in parts
headers=$(sed -n '1,/^\r$/p' <<<"$response")
content=$(sed -e '1,/^\r$/d' -e '$d' <<<"$response")
read -r http_code size_header redirect_url < <(tail -n1 <<<"$response")
# get the real Location
location=$(grep -oP 'Location: \K.*' <<<"$headers")
完全集成到您的脚本中,如下所示:
#!/bin/bash
# Usage: urls-checker.sh domains.txt
file="$1"
while read -r url; do
# read the response to a variable
response=$(curl -H 'Cache-Control: no-cache' -s -i -k --max-time 2 --write-out '%{http_code} %{size_header} %{redirect_url} ' "$url")
# break the response in parts
headers=$(sed -n '1,/^\r$/p' <<<"$response")
content=$(sed -e '1,/^\r$/d' -e '$d' <<<"$response")
read -r http_code size_header redirect_url < <(tail -n1 <<<"$response")
# get the real Location
location=$(grep -oP 'Location: \K.*' <<<"$headers")
# get the title
title=$(sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'<<<"$content")
printf "***Url: %s\n\n" "$url"
printf "Status: %s\n\n" "$http_code"
printf "Size: %s\n\n" "$size_header"
printf "Redirect-url: %s\n\n" "$location"
printf "Title: %s\n\n" "$title"
printf "Body: %s\n\n" "$(head -c 100 <<<"$content")"
done < "$file"
关于bash - 如何使用 Curl 检索真正的重定向位置 header ?不使用 {redirect_url},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46507336/
我编写了类似于以下的代码来获取重定向网址,此代码在我的本地计算机上运行良好,但是在我的托管服务器上,主机服务器上的curl版本不支持“redirect_url”,你知道如何实现吗?我可以解决这个问题吗
我是 Scrapy 和 Python 的新手,最近发布了我的第一个蜘蛛。有一个功能似乎以前有效,但现在它只适用于我试图废弃的一些网站。 代码行是: item['url_direct'] = respo
我使用的是最新版本的 iOS Paypal SDK (2.0.1),在进行单笔付款时出现以下错误。 payPal SDK: Request has failed with error: VALIDAT
在设置示例 Android 应用程序时,我更新了配置文件以包含我在 development portal 上注册时收到的应用程序 ID。 .然后,当我尝试在我的设备上登录应用程序时,我看到了这条错误消
我打印了 $_SERVER,发现 $_SERVER['REDIRECT_URL'] 与 $_SERVER['REQUEST_URI'] 的值相同。两者有什么区别? 最佳答案 REQUEST_URI 是
我意识到 Curl {redirect_url} 并不总是显示相同的重定向 URL。例如,如果 URL header 是 Location: https:/\example.com 这将重定向到 ht
我正在尝试使用 PayPal REST API 和 paypal-rest-api-sdk 在 PayPal Sandbox 上为 Java 设置计费协议(protocol)。所以,到目前为止,我已经
Client Side Express Checkout using REST to integrate PayPal in java to do transaction by PayPal acco
我是一名优秀的程序员,十分优秀!