我有以下一点代码:
uri = URI.parse("https://rs.xxx-travel.com/wbsapi/RequestListenerServlet")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path)
req.body = searchxml
req["Accept-Encoding"] ='gzip'
res = https.request(req)
Run Code Online (Sandbox Code Playgroud)
这通常工作正常,但另一方的服务器抱怨我的XML中的某些东西需要xml消息和正在发送的标头的技术人员.
我有xml消息,但我无法弄清楚如何获得上面发送的Headers.
那是我的代码.
现在我需要向主机发送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
Run Code Online (Sandbox Code Playgroud)
这是我的红宝石计划
require 'net/http'
require 'uri'
begin
url = URI.parse("http://google.com")
rescue Exception => err
p err
exit
end
http = Net::HTTP.new(url.host, url.port)
res = http.head("/")
p res.code
Run Code Online (Sandbox Code Playgroud)
它工作正常,但是如果我从URL.parse()中删除http://,它会给我这个错误:
/usr/lib/ruby/1.9.1/net/http.rb:1196:in `addr_port': undefined method `+' for nil:NilClass (NoMethodError) ...
from /usr/lib/ruby/1.9.1/net/http.rb:1094:in `request'
from /usr/lib/ruby/1.9.1/net/http.rb:860:in `head'
Run Code Online (Sandbox Code Playgroud)
这是处理异常的正确方法吗?
我知道URL可能不正确,但它应该引发异常URI :: InvalidURIError而不是接受并继续该程序?