开发者

Errors when connecting to HTTPS using HTTP::Net routines (Ruby on Rails)

开发者 https://www.devze.com 2022-12-29 23:02 出处:网络
the code below explains the problem in detail #this returns error Net::HTTPBadResponse url = URI.parse(\'https://sitename.com\')

the code below explains the problem in detail

#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'})

So... how do I send POST parameters to https://sitename.com or https://sandbox.itunes.apple.com/verifyReceipt in this example?

Further information, I am trying to get this working in Rails: http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html#//apple_ref/doc/uid/TP40008267-CH104-SW1


Found a solution from http://snippets.dzone.com/posts/show/788

I did it by this code:

     

     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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号