views:

248

answers:

1

Hi all,

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

A: 

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