如何使用修改后的标头制作 HTTP GET? [英] How to make an HTTP GET with modified headers?
本文介绍了如何使用修改后的标头制作 HTTP GET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Ruby 中使用修改后的标头发出 HTTP GET 请求的最佳方法是什么?
What is the best way to make an HTTP GET request in Ruby with modified headers?
我想从日志文件的末尾获取一系列字节,并且一直在玩弄以下代码,但服务器正在返回一个响应,说这是一个服务器无法理解的请求"(服务器是 Apache).
I want to get a range of bytes from the end of a log file and have been toying with the following code, but the server is throwing back a response saying that "it is a request that the server could not understand" (the server is Apache).
require 'net/http'
require 'uri'
#with @address, @port, @path all defined elsewhere
httpcall = Net::HTTP.new(@address, @port)
headers = {
'Range' => 'bytes=1000-'
}
resp, data = httpcall.get2(@path, headers)
- 有没有更好的方法在 Ruby 中定义标头?
- 有谁知道为什么这对 Apache 会失败?如果我在浏览器中访问
http://[address]:[port]/[path]
我会毫无问题地获得我正在寻找的数据.
- Is there a better way to define headers in Ruby?
- Does anyone know why this would be failing against Apache? If I do a get in a browser to
http://[address]:[port]/[path]
I get the data I am seeking without issue.
推荐答案
创建了一个对我有用的解决方案(效果很好)——这个例子得到了一个范围偏移:
Created a solution that worked for me (worked very well) - this example getting a range offset:
require 'uri'
require 'net/http'
size = 1000 #the last offset (for the range header)
uri = URI("http://localhost:80/index.html")
http = Net::HTTP.new(uri.host, uri.port)
headers = {
'Range' => "bytes=#{size}-"
}
path = uri.path.empty? ? "/" : uri.path
#test to ensure that the request will be valid - first get the head
code = http.head(path, headers).code.to_i
if (code >= 200 && code < 300) then
#the data is available...
http.get(uri.path, headers) do |chunk|
#provided the data is good, print it...
print chunk unless chunk =~ />416.+Range/
end
end
这篇关于如何使用修改后的标头制作 HTTP GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文