Ruby - net/http - 遵循重定向
我有一个 URL,并且正在使用 HTTP GET 将查询传递到页面。最新风格(在 net/http
中)所发生的情况是,脚本不会超出 302 响应范围。我尝试了几种不同的解决方案; HTTPClient、net/http、Rest-Client、Patron...
我需要一种方法来继续到最终页面,以便验证该页面 html 上的属性标记。重定向是由于移动用户代理点击重定向到移动视图的页面,因此移动用户代理位于标头中。这是我今天的代码:
require 'uri'
require 'net/http'
class Check_Get_Page
def more_http
url = URI.parse('my_url')
req, data = Net::HTTP::Get.new(url.path, {
'User-Agent' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5'
})
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
cookie = res.response['set-cookie']
puts 'Body = ' + res.body
puts 'Message = ' + res.message
puts 'Code = ' + res.code
puts "Cookie \n" + cookie
end
end
m = Check_Get_Page.new
m.more_http
任何建议将不胜感激!
I've got a URL and I'm using HTTP GET to pass a query along to a page. What happens with the most recent flavor (in net/http
) is that the script doesn't go beyond the 302 response. I've tried several different solutions; HTTPClient, net/http, Rest-Client, Patron...
I need a way to continue to the final page in order to validate an attribute tag on that pages html. The redirection is due to a mobile user agent hitting a page that redirects to a mobile view, hence the mobile user agent in the header. Here is my code as it is today:
require 'uri'
require 'net/http'
class Check_Get_Page
def more_http
url = URI.parse('my_url')
req, data = Net::HTTP::Get.new(url.path, {
'User-Agent' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5'
})
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
cookie = res.response['set-cookie']
puts 'Body = ' + res.body
puts 'Message = ' + res.message
puts 'Code = ' + res.code
puts "Cookie \n" + cookie
end
end
m = Check_Get_Page.new
m.more_http
Any suggestions would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要遵循重定向,您可以执行以下操作 (取自 ruby-doc)
跟随重定向
To follow redirects, you can do something like this (taken from ruby-doc)
Following Redirection
的 URL,
给定一个重定向A.
Net::HTTP
请确保在重定向过多时处理这种情况。
B.
OpenURI
OpenURI::OpenRead#open
默认遵循重定向,但不限制重定向的数量。Given a URL that redirects
A.
Net::HTTP
Make sure that you handle the case when there are too many redirects.
B.
OpenURI
OpenURI::OpenRead#open
follows redirects by default, but it doesn't limit the number of redirects.我根据这里给出的例子为此编写了另一门课程,非常感谢大家。我添加了cookie、参数和异常,终于得到了我需要的东西: https://gist.github.com/sekrett /7dd4177d6c87cf8265cd
I wrote another class for this based on examples given here, thank you very much everybody. I added cookies, parameters and exceptions and finally got what I need: https://gist.github.com/sekrett/7dd4177d6c87cf8265cd
对我有用的参考在这里: http://shadow-file.blogspot.co.uk/2009/03/handling-http-redirection-in-ruby.html
与大多数示例(包括此处接受的答案)相比,它更加稳健它处理只是一个域的 URL(http://example.com - 需要添加一个 /),专门处理 SSL ,以及相对 URL。
当然,在大多数情况下,您最好使用像 RESTClient 这样的库,但有时底层细节是必要的。
The reference that worked for me is here: http://shadow-file.blogspot.co.uk/2009/03/handling-http-redirection-in-ruby.html
Compared to most examples (including the accepted answer here), it's more robust as it handles URLs which are just a domain (http://example.com - needs to add a /), handles SSL specifically, and also relative URLs.
Of course you would be better off using a library like RESTClient in most cases, but sometimes the low-level detail is necessary.
也许你可以在这里使用 curb-fu gem https://github.com/gdi/curb-fu 唯一的事情是一些额外的代码让它遵循重定向。我之前使用过以下内容。希望有帮助。
Maybe you can use curb-fu gem here https://github.com/gdi/curb-fu the only thing is some extra code to make it follow redirect. I've used the following before. Hope it helps.
如果你不需要关心每次重定向时的细节,你可以使用库 Mechanize
它将返回目标页面。
或者您可以通过以下方式关闭重定向:
或者您可以根据请求选择更改某些设置
If you do not need to care about the details at each redirection, you can use the library Mechanize
It will return the destination page.
Or you can turn off redirection by this :
Or you can optionally change some settings at the request