使用java处理应用程序内的苹果收据

时间:2012-01-03 03:20:54

标签: java iphone json in-app-purchase

我正在使用java后端。用户执行应用内购买后,前端会将收据发送给我。反过来,我要把收据寄给苹果确认;然后苹果将解码收据并发回给我一个JSON字典。我的问题是将收据发回苹果,以便我可以得到json的回复。我正在使用下面的代码。但我一直从苹果获得{“status”:21002}以及“无效的cookie标题”。任何想法如何解决这个问题?

    String url = "https://buy.itunes.apple.com/verifyReceipt";
    DefaultHttpClient client = null;
    try {
        String input = IOUtils.toString(is);
        log.info("THE INPUTSTREAM: " + input);
        JSONObject receipt = new JSONObject();
        receipt.put("receipt-data", input);

        client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        StringEntity entity = new StringEntity(receipt.toString());
        entity.setContentType("application/json");
        post.setEntity(entity);
        HttpClientParams.setRedirecting(post.getParams(), false);

        HttpResponse response = client.execute(post);
        if (300 <= response.getStatusLine().getStatusCode()) {
            throw new RuntimeException("No response from iTune store. status code: " + response.getStatusLine().getStatusCode());
        }

        if (null == response.getEntity()) {
            log.info("Response is null");
            throw new Exception("Response is null");
        }

        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        for (String line; null != (line = reader.readLine());) {
            sb.append(line);
        }
        JSONObject json = new JSONObject(sb.toString());
        log.info("THE JSON" + json);
//Then work with json below

    } catch (Exception e) {
        throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build());
    } finally {
        if (null != client) {
            client.getConnectionManager().shutdown();
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为不需要cookie吗?尝试

HttpClientParams.setCookiePolicy(client.getParams(), CookiePolicy.IGNORE_COOKIES);