Ruby post请求失败,getaddrinfo错误

时间:2013-07-04 03:13:30

标签: ruby sockets http soap

我正在尝试向eBay的网络服务发送SOAP POST请求以添加项目请求:

    require 'uri'
    require 'net/https'


    # Create the http object
    http = Net::HTTP.new('https://api.sandbox.ebay.com', 443)
    http.use_ssl = true
    path = '/wsapi?callname=AddItem&siteid=0&version=733&Routing=new'

    # Create the SOAP Envelope
data = <<-eot
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ebay:apis:eBLBaseComponents">
   <soapenv:Header>
      <urn:RequesterCredentials>
         <urn:eBayAuthToken>TOKEN HERE</urn:eBayAuthToken>
         <urn:Credentials>
            <urn:AppId>APP_ID</urn:AppId>
            <urn:DevId>DEV_ID</urn:DevId>
            <urn:AuthCert>AUTH_CERT</urn:AuthCert>
         </urn:Credentials>
      </urn:RequesterCredentials>
   </soapenv:Header>
   <soapenv:Body>
      <urn:AddItemRequest>
         <urn:DetailLevel>ReturnAll</urn:DetailLevel>
         <urn:ErrorLanguage>en_US</urn:ErrorLanguage>
         <urn:Version>733</urn:Version>
      </urn:AddItemRequest>
   </soapenv:Body>
</soapenv:Envelope>
eot

# Set Headers
header = {
  'Accept-Encoding' => 'gzip,deflate',
  'Content-Type' => 'text/xml;charset=UTF-8',
  'Host' => 'api.sandbox.ebay.com',
  'Connection' => 'Keep-Alive',
  'SOAPAction' => '',
  'Content-Lenth' => '160000',
  "X-EBAY-SOA-MESSAGE-PROTOCOL" => "SOAP12", 
  "X-EBAY-SOA-SECURITY-APPNAME" => "APP_ID_HERE"}

# Post the request
resp, data_end = http.post(path, data, header)

# Output the results
puts 'Code = ' + resp.code
puts 'Message = ' + resp.body
resp.each { |key, val| puts key + ' = ' + val }
puts data_end

我让它工作了一瞬间。现在,每当我在Ubuntu的终端上运行IRB中的代码时,我都会收到getaddrinfo错误。

我正在玩创建套接字,我认为这就是我如何使用它。但是当我尝试重新创建套接字时,我无法再复制结果。

是否有更好的环境来启动此代码?我应该不得不搞乱套接字吗?

Ruby是否具有使用HTTP请求内置的那种配置?如果套接字是其中很重要的一部分,我应该研究哪种主题?是否有一个很好的资源可以告诉我如何设置套接字连接?

1 个答案:

答案 0 :(得分:1)

Net :: HTTP.new的第一个参数是主机名或IP地址。您提供了一个URI。 Ruby尝试使用DNS将“http:// ...”解析为主机名,但它失败了。

将该行替换为:

http = Net::HTTP.new('api.sandbox.ebay.com', 443)

......它有效。 (或者至少它超过了那个错误。)

相关问题