2013-11-10 53 views
9

我在發出http請求時出現奇怪的錯誤。錯誤的狀態行:「<!DOCTYPE HTML PUBLIC 」 - // IETF // DTD HTML 2.0 // EN 「>」

請求代碼是這樣的;

purchase_xml = Transaction.yo_xml(api_username,api_password,@total, account, merchant_reference) 
      uri = URI.parse(url) 
      http = Net::HTTP.new(uri.host, uri.port) 
      request = Net::HTTP::Post.new(uri.request_uri) 
      request.body = purchase_xml 
      response = http.request(request) 
      result = Hash.from_xml(response.body) 

其中yo_xml看起來像這樣;

def self.yo_xml(api_username, api_password,amount, account, transaction_id) 
     xml = "<?xml version=1.0 encoding=UTF-8?><AutoCreate><Request><APIUsername>#{api_username}</APIUsername> 
       <APIPassword>#{api_password}</APIPassword><Method>acdepositfunds</Method><Amount>#{amount}</Amount> 
       <Account>#{account}</Account><Narrative>Purchase of SMS</Narrative><InternalReference>#{transaction_id}</InternalReference> 
       <ExternalReference>#{transaction_id}</ExternalReference><ProviderReferenceText>Thank you for using Skyline SMS</ProviderReferenceText> 
       </Request></AutoCreate>" 
     return xml 
    end 

我得到這個錯誤;

Net::HTTPBadResponse 
wrong status line: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">" 

凸起O此線

response = http.request(request) 

任何幫助理解。

回答

15

在嘗試通過https訪問資源之前,我收到了與此類似的錯誤。你沒有提到是否如此,但如果是這樣的話,在post之前設置http.use_ssl = true可能會解決它。

3

這是我如何得到它的工作

purchase_xml = Transaction.yo_xml(api_username,api_password,@total, account, merchant_reference) 
      uri = URI.parse(url) 
      http = Net::HTTP.new(uri.host, uri.port) 
      request = Net::HTTP::Post.new(uri.request_uri) 
      request.body = purchase_xml 
      http.use_ssl = true 
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
      response = http.request(request) 
      result = Hash.from_xml(response.body) 
相關問題