如何使用 Apache HTTP 客户端将复杂参数传递给 POST 请求?

我尝试发送POST带有这样的正文的请求


{

  "method": "getAreas",

  "methodProperties": {

      "prop1" : "value1",

      "prop2" : "value2",

   }

}

这是我的代码


static final String HOST = "https://somehost.com";


  public String sendPost(String method,

      Map<String, String> methodProperties) throws ClientProtocolException, IOException {


    HttpPost post = new HttpPost(HOST);


    List<NameValuePair> urlParameters = new ArrayList<>();

    urlParameters.add(new BasicNameValuePair("method", method));


    List<NameValuePair> methodPropertiesList = methodProperties.entrySet().stream()

                .map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))

                .collect(Collectors.toList());


    // ??? urlParameters.add(new BasicNameValuePair("methodProperties", methodPropertiesList));


    post.setEntity(new UrlEncodedFormEntity(urlParameters));


    try (CloseableHttpClient httpClient = HttpClients.createDefault();

        CloseableHttpResponse response = httpClient.execute(post)) {


      return EntityUtils.toString(response.getEntity());

    }

  }

但 的构造函数BasicNameValuePair适用(String, String)。所以我需要另一堂课。


有什么办法可以添加methodPropertiesList吗urlParameters?


喵喵时光机
浏览 58回答 3
3回答

长风秋雁

您的请求看起来像 json 结构,因此发布如下数据:&nbsp;class pojo1{&nbsp; &nbsp;String method;&nbsp; &nbsp;Map<String,String> methodProperties;&nbsp;}String postUrl = "www.site.com";// put in your urlGson gson = new Gson();HttpClient httpClient = HttpClientBuilder.create().build();HttpPost post = new HttpPost(postUrl);StringEntity postingString = new StringEntity(gson.toJson(pojo1));//gson.tojson()&nbsp; &nbsp; converts your pojo to jsonpost.setEntity(postingString);post.setHeader("Content-type", "application/json");HttpResponse&nbsp; response = httpClient.execute(post);

慕无忌1623718

对于这个问题有一个众所周知的方法。在大多数情况下,您将创建自己的对象来描述要在 HttpPost 中发送的内容。所以你会得到类似的东西:static final String HOST = "https://somehost.com";  public String sendPost(String method,      Map<String, String> methodProperties) throws ClientProtocolException, IOException {    HttpPost post = new HttpPost(HOST);    MyResource resource = new MyResource();    resource.setMethod(method);    MyNestedResource nestedResource = new MyNestedResource();    nestedResource.setMethodProperties(methodProperties);    resource.setNestedResourceMethodProperties(nestedResource);    StringEntity strEntity = new StringEntity(gson.toJson(resource));    post.setEntity(strEntity);    try (CloseableHttpClient httpClient = HttpClients.createDefault();        CloseableHttpResponse response = httpClient.execute(post)) {      return EntityUtils.toString(response.getEntity());    }  }这通常是您使用嵌套结构处理更复杂的 json 对象的方式。您必须为要发送的资源创建类(在您的示例中,它可能是一个类并在其中使用映射,但通常您也为嵌套对象创建一个类,如果它具有特定的结构)。

慕妹3146593

static final String HOST = "https://somehost.com";  public String sendPost(String method, Map<String, String> methodProperties) throws ClientProtocolException, IOException {    HttpPost post = new HttpPost(HOST);      Gson gson = new Gson();    Params params = new Params(method, methodProperties);    StringEntity entity = new StringEntity(gson.toJson(params));       post.setEntity(entity);    try (CloseableHttpClient httpClient = HttpClients.createDefault();        CloseableHttpResponse response = httpClient.execute(post)) {      return EntityUtils.toString(response.getEntity());    }  }  class Params {    String method;       Map<String, String> methodProperties;    public Params(String method, Map<String, String> methodProperties) {      this.method = method;      this.methodProperties = methodProperties;    }    //getters  }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java