热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java验证iap支付收据

方法一:使用HttpsURLConnection(响应速度比方法二快)publicstaticJSONObjectverifyReceipt1(Stringrecepit){

方法一:使用HttpsURLConnection(响应速度比方法二快)

public static JSONObject verifyReceipt1(String recepit) {
return verifyReceipt1("https://buy.itunes.apple.com/verifyReceipt", recepit);
}

public static JSONObject verifyReceipt1(String url, String receipt) {
try {
HttpsURLConnection cOnnection= (HttpsURLConnection) new URL(url).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setAllowUserInteraction(false);
PrintStream ps = new PrintStream(connection.getOutputStream());
ps.print("{\"receipt-data\": \"" + receipt + "\"}");
ps.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String str;
StringBuffer sb = new StringBuffer();
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();
String resultStr = sb.toString();
JSONObject result = JSONObject.parseObject(resultStr);
if (result != null && result.getInteger("status") == 21007) {
return verifyReceipt1("https://sandbox.itunes.apple.com/verifyReceipt", receipt);
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}



方法二:使用HttpClient

public static JSONObject verifyReceipt2(String receipt) {
return verifyReceipt2("https://buy.itunes.apple.com/verifyReceipt", receipt);
}

public static JSONObject verifyReceipt2(String url, String receipt) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
JSONObject data = new JSONObject();
data.put("receipt-data", receipt);
StringEntity entity = new StringEntity(data.toJSONString());
entity.setContentEncoding("utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse respOnse= httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
String resultStr = EntityUtils.toString(httpEntity);
JSONObject result = JSONObject.parseObject(resultStr);
httpPost.releaseConnection();
if (result.getInteger("status") == 21007) {
return verifyReceipt2("https://sandbox.itunes.apple.com/verifyReceipt", receipt);
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}





推荐阅读
author-avatar
1234567山02
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有