在将其发送到java之前打印HttpPost请求

时间:2017-05-18 03:43:39

标签: java json http-headers http-post access-token

我正在向API发送Json消息,我收到错误但我想知道我发送给API的内容。顺便说一下,验证方法是通过令牌访问。

方法就是这个:

    public static void sendRequestPost2(JSONObject json) throws IOException, JSONException {

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    try {

        HttpPost request = new HttpPost(Config.urlJSON);
        StringEntity params = new StringEntity(json.toString());
        request.addHeader("Authorization", "Token " + Config.renamToken);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);

        int statusCode = httpClient.execute(request).getStatusLine().getStatusCode();

        Log.debug("[STATUS:" + statusCode + "]");

    } catch (Exception ex) {
        Log.debug(ex.toString());
    } finally {
        httpClient.close();
    }
}

我已经尝试了几个请求对象,但我分别得到了结果:

    request.setEntity(params);

    Log.debug(request.getMethod());

    Header[] heads = request.getAllHeaders();

    String headsString = "";
    for (int i = 0; i < heads.length; i++) {
        headsString += heads[i];
    }
    String entity = EntityUtils.toString(request.getEntity());
    Log.debug(entity);
    Log.debug(request.getRequestLine().toString());
    Log.debug(headsString);
    Log.debug(request.getURI().toURL().toString());

    CloseableHttpResponse response = httpClient.execute(request);

    Log.debug(response.getStatusLine().toString());
    String content = EntityUtils.toString(response.getEntity());
    Log.debug(content);

我可以获得大量信息但是在将其发送到API之前我没有得到完整的字符串,我需要知道字符串是否正确。

谢谢。

0 个答案:

没有答案