Transferred from: http://blog.csdn.net/saindy5828/article/details/6414014
1, from the transaction transactionreceipt attribute received data, and Base64 encoding;
2, create JSON object, dictionary format, single key value pair, the key name is "Receiptdata", the value is the last encoded data, the effect:
{"Receipt-data": "Base64 Data after encoding}"
3. Send an HTTP POST request to send the data to the app Store with the address: Https://buy.itunes.apple.com/verifyReceipt
4. The return value of the APP store is also a JSON-formatted object, including two key-value pairs:
{
"Status": 0,
"Receipt": {...}
}
Description
If the value of status is 0, it means that the receipt is valid, otherwise it is invalid.
1 2 3 Public intVerifyreceipt (byte[] receipt) {4 intStatus =-1;5 6 //The the URL of the REST WebService in ITunes App Store7URL url =NewURL ("Https://buy.itunes.apple.com/verifyReceipt");8 9 //Make connection, use post modeTenHttpsurlconnection connection =(httpsurlconnection) url.openconnection (); OneConnection.setrequestmethod ("POST"); AConnection.setdooutput (true); -Connection.setallowuserinteraction (false); - the //Encode the binary receipt data into Base - //Here I ' m using org.apache.commons.codec.binary.Base64 as a encoder, since Commons-codec is already in Grails Classpat H -BASE64 encoder =NewBase64 (); -String Encodedreceipt =NewString (Encoder.encode (receipt)); + - //Create a JSON query object + //Here I ' m using Grails ' Org.codehaus.groovy.grails.web.json.JSONObject AMap map =NewHashMap (); atMap.put ("Receipt-data", encodedreceipt); -Jsonobject Jsonobject =Newjsonobject (map); - - //Write The JSON query object to the connection output stream -PrintStream PS =NewPrintStream (Connection.getoutputstream ()); - Ps.print (jsonobject.tostring ()); in ps.close (); - to //Call the service +BufferedReader br =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); - //Extract Response the String str; *StringBuffer SB =NewStringBuffer (); $ while(str = br.readline ())! =NULL) {Panax Notoginseng sb.append (str); -Sb.append ("/n"); the } + br.close (); AString response =sb.tostring (); the + //Deserialize Response -Jsonobject result =NewJsonobject (response); $Status = Result.getint ("status"); $ if(Status = = 0) { - //Provide content -}Else { the //signal Error, throw an exception, do your stuff honey! - }Wuyi the returnstatus; - Wu -}
The authentication can also take the HTTP protocol: HttpClient, download httpcomponents-client, and then import several jar packages in lib.
This is also a way to no longer make redundant explanations. The so-called roads Pass "apples".
Verify an App Store Transaction Receipt "Apple server verifies the validity of a transaction receipt for an app shop"