java - 将 JSON 发送到 Java post 并显示信息

标签 java apache http post http-post

我正在尝试将以下 JSON 字符串发送到 Java 中的 url:

其中标签名称为data,正文为

{"process": "mobileS","phone": "9999999999"}

到目前为止,我的代码如下:

HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead

try {
    HttpPost request = new HttpPost("url goes here");
    StringEntity params = new StringEntity("details={\"process\":\"mobileS\",\"phone\":\"9999999999\"}");
    request.addHeader("content-type", "application/x-www-form-urlencoded");
    request.setEntity(params);

    HttpResponse response = httpClient.execute(request);
    System.out.println(response);

    // handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.getConnectionManager().shutdown(); //Deprecated
}

将 JSON 数据发送到服务器后,我应该从上面获取一个 JSON 字符串,但我不知道发送上述请求后该去哪里。

好处是响应返回 200 响应,它显示了通常来自 HttpResponse 结果的所有其他信息,但我得到的结果类似于:

{"test": {"this is":"what I am supposed to get"}}

所以基本上它应该返回一个 JSON 字符串,而我得到的东西与我需要的完全不同

HttpResponseProxy{HTTP/1.1 200 OK [Cache-Control: no-store, no-cache, must-revalidate, max-age=0,post-check=0, pre-check=0 etc etc etc

我似乎无法理解我做错了什么。

最佳答案

HttpResponse类公开了 getEntity返回 HttpEntity 的方法实例。这提供了一种访问响应内容的机制。

您可以使用EntityUtils检索并使用实体的内容流:

private void getData(){
    CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead
    CloseableHttpResponse response = null;
    HttpEntity entity = null;

    try {
        HttpPost request = new HttpPost("url goes here");
        StringEntity params = new StringEntity("details={\"process\":\"mobileS\",\"phone\":\"9999999999\"}");
        request.addHeader("content-type", "application/x-www-form-urlencoded");
        request.setEntity(params);

        response = httpClient.execute(request);
        System.out.println(response);

        // handle response here...
        if (successful(response)) {
            entity = response.getEntity();
            String content = EntityUtils.toString(entity);

            System.out.println(content);
        }
    } catch (Exception ex) {
        // handle exception here
    } finally {
        EntityUtils.consumeQuietly(entity);
        if (response != null) response.close();
        if (httpClient != null) httpClient.close();
    }
}

// TODO Customize for your server/interaction
private boolean successful(HttpResponse response) {
    return response != null 
            && response.getStatusLine() != null 
            && response.getStatusLine().getStatusCode() == 200;
}

关于java - 将 JSON 发送到 Java post 并显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40097872/

相关文章:

java - 即使发生 IOException,作业也成功完成

python - 从请求 flask 中获取 json

c - Http POST..为什么不打印任何内容?

mysql - 负载均衡器和 SQL 备份

php - "413 Request Entity Too Large"用于 AJAX POST 请求但不用于普通文件上传

apache - Windows 上的 XAMPP - Apache 未启动

http - Flutter:如何将 POST 图像 http 发送到 Microsoft Cognitive Services

java - 从 Eclipse 在 Tomcat 中部署时更改 Web 应用程序中的文件夹名称

java - 为什么分配刚刚声明的类变量是非法的?

java - Richfaces 文件上传 UI 外观