9

当我发出 http 请求时,我遇到了一个奇怪的错误。

请求代码是这样的;

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\">"

提高了这条线

response = http.request(request)

任何帮助表示赞赏。

4

2 回答 2

17

在尝试通过 https 访问资源之前,我遇到了类似的错误。你没有提到是否是这种情况,但如果是这样的话,在可能修复它http.use_ssl = true之前设置。post

于 2013-11-10T01:14:11.693 回答
4

这就是我让它工作的方式

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)
于 2013-11-10T21:24:48.957 回答