ruby-on-rails - 使用HTTP::Net例程连接到HTTPS时出错(Ruby on Rails)

标签 ruby-on-rails sockets

下面的代码详细解释了该问题

#this returns error Net::HTTPBadResponse
      url = URI.parse('https://sitename.com')
      response = Net::HTTP.start(url.host, url.port) {|http|
        http.get('/remote/register_device')
      }

#this works
      url = URI.parse('http://sitename.com')
      response = Net::HTTP.start(url.host, url.port) {|http|
        http.get('/remote/register_device')
      }

#this returns error Net::HTTPBadResponse
      response = Net::HTTP.post_form(URI.parse('https://sitename.com/remote/register_device'), {:text => 'hello world'})

#this returns error Errno::ECONNRESET (Connection reset by peer)
      response = Net::HTTP.post_form(URI.parse('https://sandbox.itunes.apple.com/verifyReceipt'), {:text => 'hello world'})

#this works
      response = Net::HTTP.post_form(URI.parse('http://sitename.com/remote/register_device'), {:text => 'hello world'})

那么...在此示例中,如何将POST参数发送到https://sitename.comhttps://sandbox.itunes.apple.com/verifyReceipt

更多信息,我正在尝试在Rails中工作:http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html#//apple_ref/doc/uid/TP40008267-CH104-SW1

最佳答案

http://snippets.dzone.com/posts/show/788找到了一个解决方案

我是通过以下代码完成的:

     http = Net::HTTP.new('sandbox.itunes.apple.com', 443)
     http.use_ssl = true
     path = '/verifyReceipt'

     # GET request -> so the host can set his cookies
     resp, data = http.get(path, nil)
     cookie = resp.response['set-cookie']

     # POST request -> logging in
     receipt_data_json = {"receipt-data" => Base64.encode64(receipt_data)}.to_json
     #logger.debug "receipt data is now #{receipt_data_json.inspect}"
     data = receipt_data_json
     headers = {
       'Cookie' => cookie,
       'Referer' => 'http://sandbox.itunes.apple.com/verifyReceipt',
       'Content-Type' => 'application/x-www-form-urlencoded'
     }

     response, data = http.post(path, data, headers)

关于ruby-on-rails - 使用HTTP::Net例程连接到HTTPS时出错(Ruby on Rails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2801468/

相关文章:

ruby-on-rails - ruby 64 位 aix 编译中的正则表达式限制

ruby-on-rails - VirtualBox 上的 Rails 服务器运行速度非常慢

c - 如何将原始套接字绑定(bind)到特定接口(interface)

Python 套接字连接在 Ubuntu 16.04 上每 9 个连接延迟一秒

没有HTTP或HTTPS通信的android ssl通信

linux - exec 3<>/dev/tcp/anyaddress.com/80 在 ash 中的等价物

C++ LibCurl 发送 HTTPS 请求

ruby-on-rails - Windows 上的 Rails 部署环境

ruby-on-rails - session 变量存储在Rails中的什么位置?

ruby-on-rails - solr sunspot - 搜索belongs_to 关联