IOSIAP二次验证

来源:互联网 发布:mac如何免费翻墙 编辑:程序博客网 时间:2024/06/05 18:22
直接上代码

 1 import com.alibaba.fastjson.JSONObject; 2 import java.io.BufferedReader; 3 import java.io.InputStreamReader; 4 import java.io.PrintStream; 5 import java.net.URL; 6 import javax.net.ssl.HttpsURLConnection; 7  8 public class IosIAP { 9     public static JSONObject verifyReceipt1(String recepit) throws Exception {10         String url = "https://buy.itunes.apple.com/verifyReceipt";11 12         return verifyReceipt1(url, recepit);13     }14 15     public static JSONObject verifyReceipt1(String url, String receipt)16             throws Exception {17         HttpsURLConnection connection = (HttpsURLConnection) new URL(url)18                 .openConnection();19         connection.setRequestMethod("POST");20         connection.setDoOutput(true);21         connection.setAllowUserInteraction(false);22         PrintStream ps = new PrintStream(connection.getOutputStream());23         ps.print("{\"receipt-data\": \"" + receipt + "\"}");24         ps.close();25         BufferedReader br = new BufferedReader(new InputStreamReader(26                 connection.getInputStream()));27 28         StringBuffer sb = new StringBuffer();29         String str;30         while ((str = br.readLine()) != null) {31             sb.append(str);32         }33         br.close();34         String resultStr = sb.toString();35         JSONObject result = JSONObject.parseObject(resultStr);36         if ((result != null)37                 && (result.getInteger("status").intValue() == 21007)) {38             return verifyReceipt1(39                     "https://sandbox.itunes.apple.com/verifyReceipt", receipt);40         }41         return result;42     }43 }

 

原创粉丝点击