我有一个URL,我正在使用HTTP GET将查询传递到页面.最新的味道(in 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 …Run Code Online (Sandbox Code Playgroud) 我有这个简单的html解析器(用于学习目的),我一直在研究:
require 'open-uri'
puts "Enter URL to parse HTML: "
url = gets.chomp
puts "Enter tag to parse from: "
tag = gets.chomp
response = open(url).read
title1 = response.index(tag)
title2 = response.index(tag.insert(1,'/')) -1
result = response[(title1 + tag.length - 1)..title2]
print result
Run Code Online (Sandbox Code Playgroud)
当我输入时http://twitter.com,我收到此错误消息:
ERROR: `open_loop': redirection forbidden: http://twitter.com -> https://twitter.com/ (RuntimeError)
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:704:in `open'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:34:in `open'
from /home/ubuntu/workspace/htmlparse.rb:6:in `<main>'
Run Code Online (Sandbox Code Playgroud)
有任何建议或帮助吗?我是Ruby新手,我知道其他html解析模块,但我这样做是为了学习Ruby基础知识.谢谢.
我想用一个网站的api来使用ruby.说明是发送带有标头的GET请求.这些是来自网站的说明和他们提供的示例php代码.我要计算HMAC哈希并将其包含在apisign标题下.
$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
Run Code Online (Sandbox Code Playgroud)
我只是在命令提示符下使用安装在windows上的ruby的.rb文件.我在ruby文件中使用net/http.如何发送带标题的GET请求并打印响应?