2010-08-04 86 views
6

这是我的代码。如何在Ruby Net :: HTTP中实现Cookie

现在我需要发送一个cookie到主机,但我找不到解决方案。


def get_network_file(url=nil) 
    begin 
    http = Net::HTTP.new(@service_server, 80) 
    resp, data = http.get(url, { "Accept-Language" => @locale }) 
    if resp.code.to_i != 200 
     RAILS_DEFAULT_LOGGER.error "*** return code != 200. code = #{resp.code}" 
     return "" 
    end 
    rescue Exception => exc 
     RAILS_DEFAULT_LOGGER.error "*** message --> #{exc.message}" 
     return "" 
    end 
    return data 
    end 
end 

+0

[如何实现的cookie可能重复饼干支持在红宝石网/ http?](http://stackoverflow.com/questions/1486703/how-to-implement-cookie-support-in-ruby-net-http) – 2013-03-11 20:12:00

回答

5

你可以通过你发送相同的散列通饼干“接受语言”头,是这样的:

resp, data = http.get(url, { 
    "Accept-Language" => @locale, 
    "Cookie" => "YOUR_COOKIE" 
}) 

赔率是你需要拍摄首先,cookie。有关cookie处理的示例,请参阅this

+0

谢谢。我会尽力。 – Juanin 2010-08-07 18:31:29