在C#中,它非常简单,并且谷歌不需要花费几分钟的时间:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://www.example.com?q=someValue");
request.Headers.Add("Authorization: OAuth realm=\"example.com\" oauth_consumer_key=\"BCqrstoO\" ... so on and so forth");
string resultString = "";
using (StreamReader read = new StreamReader(request.GetResponse().GetResponseStream(), true))
{
resultString = read.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
试图在Ruby中做到这一点并不是那么直截了当(或者只是我缺少的一些愚蠢行为).我一直在寻找,我找到答案的最接近的事情是如何使用修改后的标头进行HTTP GET?并在Ruby中发送自定义标头.
所以我的问题,我想,归结为
到目前为止,我尝试了两个例子,这是我最近的非工作尝试:
headers = "Authorization: OAuth realm=\"example.com\" oauth_consumer_key=\"BCqrstoO\" ... so on and so forth"
uri = URI("www.example.com")
http = Net::HTTP.new(uri.host, uri.port)
http.get(uri.path, headers) do |chunk|
puts chunk
end
Run Code Online (Sandbox Code Playgroud)
使用open-uri.例:
require 'open-uri'
open("http://www.ruby-lang.org/en/",
"User-Agent" => "Ruby/#{RUBY_VERSION}",
"From" => "foo@bar.invalid",
"Referer" => "http://www.ruby-lang.org/") {|f|
# ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1818 次 |
| 最近记录: |