如何使用修改后的标头进行 HTTP GET?

发布于 2024-07-14 03:21:53 字数 523 浏览 3 评论 0原文

在 Ruby 中使用修改后的标头发出 HTTP GET 请求的最佳方法是什么?

我想从日志文件末尾获取一定范围的字节,并且一直在玩弄以下代码,但是服务器返回一个响应,表示“这是服务器无法理解的请求”(服务器是阿帕奇)。

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)
  1. 有没有更好的方法在 Ruby 中定义标头?
  2. 有谁知道为什么这对 Apache 会失败? 如果我在浏览器中访问 http://[address]:[port]/[path] 我会毫无问题地获得我正在寻找的数据。

What is the best way to make an HTTP GET request in Ruby with modified headers?

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)
  1. Is there a better way to define headers in Ruby?
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的轨迹 2024-07-21 03:21:53

创建了一个对我有用的解决方案(效果很好) - 这个示例获取范围偏移:

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

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
戏蝶舞 2024-07-21 03:21:53

如果您有权访问服务器日志,请尝试将浏览器的请求与 Ruby 的请求进行比较,看看是否能告诉您任何信息。 如果这不切实际,请启动 Webrick 作为文件服务器的模拟。 不用担心结果,只需比较请求,看看它们做了什么不同的事情。

至于 Ruby 风格,您可以内联移动标头,如下所示:

httpcall = Net::HTTP.new(@address, @port)

resp, data = httpcall.get2(@path, 'Range' => 'bytes=1000-')

另外,请注意,在 Ruby 1.8+ 中,您几乎可以肯定正在运行的内容, Net::HTTP#get2 返回单个 HTTPResponse 对象,而不是 resp, data 对。

If you have access to the server logs, try comparing the request from the browser with the one from Ruby and see if that tells you anything. If this isn't practical, fire up Webrick as a mock of the file server. Don't worry about the results, just compare the requests to see what they are doing differently.

As for Ruby style, you could move the headers inline, like so:

httpcall = Net::HTTP.new(@address, @port)

resp, data = httpcall.get2(@path, 'Range' => 'bytes=1000-')

Also, note that in Ruby 1.8+, what you are almost certainly running, Net::HTTP#get2 returns a single HTTPResponse object, not a resp, data pair.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文