从红宝石中获取带有cookie的URL的图像 [英] Get image from url with cookies in ruby

查看:123
本文介绍了从红宝石中获取带有cookie的URL的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,一些验证码正在使用用户会话生成,我必须以某种方式将此图像保存到计算机,以便测试我们的应用程序,但是如何以及更好的选择?

As you know, some captchas are generating using user session, and i must to somehow save to computer this image, for testing our app, but how, and what better to choise?

例如在http :: get我有这样的代码,有cookies:

For example on http::get i have such code, with cookies:

http = Net::HTTP.new('***', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
path = '****'


# GET request -> so the host can set his cookies
resp, data = http.get(path)
body_text = resp.body
#puts "Body = #{body_text}"

cookie = resp.response['set-cookie'].split('; ')[0]
@captcha_url = (/BotDetectCaptcha.ashx?get=image&c=***;t=(.*)" \/>/.match body_text)
# POST request -> logging in
puts "captcha_url = #{@captcha_url}"
data = 'BotDetectCaptcha.ashx?get=image&c=****&t=#{@captcha_url}'
headers = {
  'Cookie' => cookie,
  'Referer' => '****',
  'Content-Type' => 'image-jpeg'
}

resp, data = http.post(path, data, headers)

所以,也许,我有我需要的图像,但是!我怎么能保存它?所有谷歌的文章都说我,我必须使用open-uri,但是当我必须使用会话时怎么样?也许我可以用ruby的http类来做某事?

so, perhaps, i have image that i need, but! how can i save it? All google'd articles say me, that i must use open-uri, but how to be, when i have to use session also? Maybe i could do it somehow with http class from ruby?

那么如何从加载的页面或通过URL下载图像?

So how can i download image from loaded page, or via url?

推荐答案

您可以将图像变为变量并保存:

You can get image to variable and save it:

IMG_PATH = '/var/www/pics/'

Class Img
  def self.download(data, filename)
    open(IMG_PATH + filename, 'wb') do |file|
      file << open(data).read
    end
    return true
  end
end

img = Img.download(data, filename)

这篇关于从红宝石中获取带有cookie的URL的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆