问题描述
我正在尝试使用HTTP :: get从我创建的URL下载Google图表的图像.
I am trying to use HTTP::get to download an image of a Google chart from a URL I created.
这是我的第一次尝试:
failures_url = [title, type, data, size, colors, labels].join("&")
require 'net/http'
Net::HTTP.start("http://chart.googleapis.com") { |http|
resp = http.get("/chart?#{failures_url")
open("pie.png" ,"wb") { |file|
file.write(resp.body)
}
}
仅生成一个空的PNG文件.
Which produced only an empty PNG file.
第二次尝试使用存储在http.get()
调用内failure_url
内部的值.
For my second attempt I used the value stored inside failure_url
inside the http.get()
call.
require 'net/http'
Net::HTTP.start("http://chart.googleapis.com") { |http|
resp = http.get("/chart?chtt=Builds+in+the+last+12+months&cht=bvg&chd=t:296,1058,1217,1615,1200,611,2055,1663,1746,1950,2044,2781,1553&chs=800x375&chco=4466AA&chxl=0:|Jul-2010|Aug-2010|Sep-2010|Oct-2010|Nov-2010|Dec-2010|Jan-2011|Feb-2011|Mar-2011|Apr-2011|May-2011|Jun-2011|Jul-2011|2:|Months|3:|Builds&chxt=x,y,x,y&chg=0,6.6666666666666666666666666666667,5,5,0,0&chxp=3,50|2,50&chbh=23,5,30&chxr=1,0,3000&chds=0,3000")
open("pie.png" ,"wb") { |file|
file.write(resp.body)
}
}
并且由于某种原因,即使第一次尝试在http.get()
调用中具有相同的数据,该版本仍然有效.有人知道为什么吗?
And, for some reason, this version works even though the first attempt had the same data inside the http.get()
call. Does anyone know why this is?
解决方案:
试图弄清为什么会发生这种情况后,我发现"".
After trying to figure why this is happening I found "How do I download a binary file over HTTP?".
其中一条评论提到在Net::HTTP.start(...)
调用中删除http://
,否则将不会成功.确定之后,我就确定了:
One of the comments mentions removing http://
in the Net::HTTP.start(...)
call otherwise it won't succeed. Sure enough after I did this:
failures_url = [title, type, data, size, colors, labels].join("&")
require 'net/http'
Net::HTTP.start("chart.googleapis.com") { |http|
resp = http.get("/chart?#{failures_url")
open("pie.png" ,"wb") { |file|
file.write(resp.body)
}
}
有效.
推荐答案
我会使用Ruby的打开:: URI :
require "open-uri"
File.open('pie.png', 'wb') do |fo|
fo.write open("http://chart.googleapis.com/chart?#{failures_url}").read
end
我之所以喜欢Open :: URI是因为它会自动处理重定向,因此当Google对后端进行更改并尝试重定向URL时,代码会神奇地处理它.如果我没记错的话,它还可以处理超时并更优雅地重试.
The reason I prefer Open::URI is it handles redirects automatically, so WHEN Google makes a change to their back-end and tries to redirect the URL, the code will handle it magically. It also handles timeouts and retries more gracefully if I remember right.
如果您必须具有较低级别的控制权,那么我将看看Ruby的许多其他HTTP客户端之一; Net :: HTTP非常适合创建新服务或不存在客户端时使用,但我会使用Open :: URI或Net :: HTTP之外的其他工具,直到需求出现为止.
If you must have lower level control then I'd look at one of the many other HTTP clients for Ruby; Net::HTTP is fine for creating new services or when a client doesn't exist, but I'd use Open::URI or something besides Net::HTTP until the need presents itself.
URL:
http://chart.googleapis.com/chart?chtt=Builds+in+the+last+12+months&cht=bvg&chd=t:296,1058,1217,1615,1200,611,2055,1663,1746,1950,2044,2781,1553&chs=800x375&chco=4466AA&chxl=0:|Jul-2010|Aug-2010|Sep-2010|Oct-2010|Nov-2010|Dec-2010|Jan-2011|Feb-2011|Mar-2011|Apr-2011|May-2011|Jun-2011|Jul-2011|2:|Months|3:|Builds&chxt=x,y,x,y&chg=0,6.6666666666666666666666666666667,5,5,0,0&chxp=3,50|2,50&chbh=23,5,30&chxr=1,0,3000&chds=0,3000
使URI失效.我怀疑它看到应该在URL中编码的字符.
makes URI upset. I suspect it is seeing characters that should be encoded in URLs.
出于文档目的,这是URI尝试按原样解析该URL时所说的内容:
For documentation purposes, here is what URI says when trying to parse that URL as-is:
URI::InvalidURIError: bad URI(is not URI?)
如果我首先对URI进行编码,那么我将获得成功的解析.使用Open :: URI进行的进一步测试表明,它能够在那时检索文档并返回23701个字节.
If I encode the URI first, I get a successful parse. Testing further using Open::URI shows it is able to retrieve the document at that point and returns 23701 bytes.
如果某些字符对于URI 确实不可接受,并且它们不在RFC中,我认为这是解决该问题的适当方法.
I think that is the appropriate fix for the problem if some of those characters are truly not acceptable to URI AND they are out of the RFC.
仅供参考, Addressable :: URI gem是内置URI的理想替代品
Just for information, the Addressable::URI gem is a great replacement for the built-in URI.
这篇关于从URL下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!