标签: net-library

在ruby中访问Net :: HTTP :: Post的Headers

我有以下一点代码:

    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.

ruby header http net-library

9
推荐指数
2
解决办法
9750
查看次数

如何在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
Run Code Online (Sandbox Code Playgroud)

ruby cookies http net-library

6
推荐指数
1
解决办法
8154
查看次数

Ruby URL.parse错误

这是我的红宝石计划

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而不是接受并继续该程序?

ruby exception net-library

4
推荐指数
1
解决办法
6498
查看次数

标签 统计

net-library ×3

ruby ×3

http ×2

cookies ×1

exception ×1

header ×1