Apache HTTP Client POST抛出500

时间:2017-05-25 20:02:55

标签: java apache rest http apache-httpclient-4.x

我正在尝试使用apache http client

执行POST请求

http://hc.apache.org/

这是代码段

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://xav.com/asd");

        JSONObject jon = new JSONObject();
        jon.put("param", "val");
        jon.put("param2", "val2");

        String json = jon.toString();
        StringEntity entity = new StringEntity(json);
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        CloseableHttpResponse response = client.execute(httpPost);
        BufferedReader rd = new BufferedReader
                (new InputStreamReader(
                response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
        client.close();

但是我得到500错误。我可以使用存在的swagger UI执行相同的POST请求。我假设在构造我的请求时出现了问题,并且参数没有正确地传递到后端。

我是否正确创建了POST请求?有没有办法在添加参数后获取实际的URL?

1 个答案:

答案 0 :(得分:0)

它只是起作用,它返回404而不是500.

...
<title>Error 404 Document Not Found on http://xav.com/asd</title>
...
在您的示例中,

404实际上是http://xav.com/asd的正确答案。

以下是该计划:

package com.stackoverflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;

public class ApacheHttpClientTest {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://xav.com/asd");

        JSONObject jon = new JSONObject();
        jon.put("param", "val");
        jon.put("param2", "val2");

        String json = jon.toString();
        StringEntity entity = new StringEntity(json);
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        CloseableHttpResponse response = client.execute(httpPost);
        BufferedReader rd = new BufferedReader
                (new InputStreamReader(
                response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
        client.close();
    }
}

以下是我使用的库:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20170516</version>
</dependency>

如果您尝试其他公共网络服务http://www.posttestserver.com/,您将获得HTTP状态200:

Successfully dumped 0 post variables.
View it at http://www.posttestserver.com/data/2017/05/25/22.39.57585465710
No Post body.

以下是该计划:

package com.stackoverflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;

public class ApacheHttpClientTest {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://posttestserver.com/post.php");

        JSONObject jon = new JSONObject();
        jon.put("param", "val");
        jon.put("param2", "val2");

        // Returns the original request URI. Please note URI remains 
        // unchanged in the course of request execution and is not 
        // updated if the request is redirected to another location.
        System.out.println("URI: " + httpPost.getURI());
        // URI: http://posttestserver.com/post.php            

        String json = jon.toString();
        StringEntity entity = new StringEntity(json);
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        CloseableHttpResponse response = client.execute(httpPost);
        BufferedReader rd = new BufferedReader
                (new InputStreamReader(
                response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
        client.close();
    }
}