簡體   English   中英

無法使用NET :: HTTP或httpclient發送POST請求並正確獲得響應?

[英]Can't send POST request and get response correctly using NET::HTTP or httpclient?

我需要將帶有標頭和正文的其他請求發送到PayPal。 我想使用標准類NET :: HTTP,所以這是我的代碼( IS N'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

這種方法-不斷給我-帳戶類型不是null,但不是有效的PayPal帳戶類型。 但這已在沙盒中驗證! 試圖留空,但它給了我同樣的感覺!

預先感謝您的幫助!

我不熟悉您的第二個代碼示例,但是我使用了第一個代碼示例(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 ,這意味着后端服務器將無法訪問(在這種情況下,我想那是Errno::ECONNRESET寶服務器)-您是否確定服務器正在運行? 您是否肯定沒有防火牆或任何妨礙您連接的內容?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM