1

我需要向 PayPal 发送带有标题和正文的不同请求。我想使用标准类 NET::HTTP,所以这是我的代码(ISN'T WORKING):

    require "net/http"
    require "uri"
    header = {...}
    body = {...}
    url = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
    uri = URI.parse(url)
    args = { 'header' => header,'body' => body }
    res = Net::HTTP.post_form(uri, args)
    puts res.status

给我错误:

  C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': An existing connection was forcibly closed by the remote host. (Errno::ECONNRESET)

编辑 第二个变体:

      require 'httpclient'
      require 'xmlsimple'
      header =  {"X-PAYPAL-SECURITY-USERID" => "tok261_biz_api.abc.com",
           "X-PAYPAL-SECURITY-PASSWORD" => "1244612379",
           "X-PAYPAL-SECURITY-SIGNATURE" => "lkfg9groingghb4uw5",
           "X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
           "X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
           "X-PAYPAL-APPLICATION-ID" =>  "APP-80W284485P519543T"
            }
      #data to be sent in the request
      data = {"emailAddress" => "denmed_1342605975_biz@gmail.com",
       "firstName"=> "Den",
       "lastName" => "Med",
       "matchCriteria"=> "NAME",
       "requestEnvelope.errorLanguage" => "en_US"}
      #initialize the request
      clnt = HTTPClient.new
      #API end point(sandbox)
      uri = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
     #make the post
     res = clnt.post(uri, data, header)
     if res.status == 200
      @xml = XmlSimple.xml_in(res.content)
     if @xml['accountType']!=nil
    account_type = @xml['accountType'][0]
    #its pretty obvious from here init?
   if account_type.to_s() == "Business"
    puts "Business account!"
    elseif account_type.to_s() == "Premier"
    puts  "Premier Account!"
  end
   elseif account_type.to_s() == "Personal"
    puts "Personal account!"
   else
    puts "Account type not null but not a valid PayPal account type."
   end
   else
    puts "Gee! sorry! something went seriously wrong"
   end

这种方法 - 不断给我 - 帐户类型不为空,但不是有效的 PayPal 帐户类型。但它在沙盒中得到了验证!试图留下空白字段,但它给了我同样的!

提前感谢您的帮助!

4

1 回答 1

0

我不熟悉你的第二个代码示例,但是我之前使用过第一个(Net::HTTP),我发现它工作得很好。我通常只将它用于 GET,但我会尝试在 POST 上提供建议 :)

第一件事:不应将标头设置为与请求一起发送的数据(您似乎正在这样做)-相反,应将每个标头设置为请求的属性:

request['X-HEADER_NAME'] = header_value

如果使用 Net::HTTP,我建议您的代码块应该如下所示:

require "net/http"
require "uri"

url = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
uri = URI.parse(url)

req = Net::HTTP::Post.new(url)
req["X-PAYPAL-SECURITY-USERID"] = "tok261_biz_api.abc.com"
req["X-PAYPAL-SECURITY-PASSWORD"] = "1244612379"
req["...."] = "...."   .... etc for all headers

req.set_form_data( {"emailAddress" => "denmed_1342605975_biz@gmail.com",
   "firstName"=> "Den",
   "lastName" => "Med",
   "matchCriteria"=> "NAME",
   "requestEnvelope.errorLanguage" => "en_US"} )

Net::HTTP.start(uri.host, uri.port) do |http|
    response = http.request(req)
    return response
end

我建议尝试一下,如果它不起作用,你能给我给定错误的确切措辞吗?

注意 另一个建议是,每当我的应用程序接收Errno::ECONNRESET到 ,这意味着后端服务器不可访问(在这种情况下,我猜那将是贝宝服务器) - 你确定服务器正在运行吗?你确定没有防火墙或任何阻止你连接的地方吗?

于 2012-07-27T15:57:31.757 回答