gpt4 book ai didi

http - Ruby HTTParty - 获取重定向的 URL

转载 作者:可可西里 更新时间:2023-11-01 15:20:05 24 4
gpt4 key购买 nike

我正在尝试使用带有一些参数的 HTTPParty 发送 HTTP 请求,

例如:

GET URL: http://example1.com?a=123&b=456

response_to_get = HTTParty.get('http://example1.com?a=123&b=456')

这个特定请求的 redirect_urlhttp://example2.com

当我在浏览器中尝试访问 URL http://example1.com?a=123&b=456 时,它重定向到预期的 URL,并附加了参数值,如 http://example2.com?c=123trt58,但是当我使用 HTTParty 执行此操作时,我只收到 HTML 响应。

我的要求是从响应中获取 URL( http://example2.com?c=123trt58 )。

提前致谢。

最佳答案

如果您不想跟随重定向,但仍想知道页面重定向到哪里(例如,对网络爬虫很有用),您可以使用 response.headers['location']:

response = HTTParty.get('http://httpstat.us/301', follow_redirects: false)

if response.code >= 300 && response.code < 400
redirect_url = response.headers['location']
end

关于http - Ruby HTTParty - 获取重定向的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32732207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com