HttpClient发送json、普通参数类型的Post请求

前言:
  当传递json参数时,需要使用StringEntity将Content-Type设置为text/plain类型
  当传递非json参数,正常的表单数据时,使用
UrlEncodedFormEntity将Content-Type设置为application/x-www-form-urlencoded类型

1、Post请求传json数据

   // 省略前面声明请求、设置Header等操作,直接从传递参数开始
   JSONObject json = new JSONObject();
   json.put("filePath","js");
   json.put("projectId","61020ccdfd33d86b6abe8745");
   json.put("type","fileFolder");
      
   // 将参数放到Post中
   // 通过new StringEntity(),可将Content-Type设置为text/plain类型
   httpPost.setEntity(new StringEntity(json.toString(),"UTF-8"));

2、Post请求传普通参数 

   JSONObject json = new JSONObject();
   json.put("filePath","js");
   json.put("projectId","61020ccdfd33d86b6abe8745");
   json.put("type","fileFolder");
    // 设置参数
   List<NameValuePair> parameters = new ArrayList<NameValuePair>();
   for(String key:json.keySet()) {
     parameters.add(new BasicNameValuePair(key, json.getString(key)));
   }
 
   // 将Content-Type设置为application/x-www-form-urlencoded类型
   UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
   httpPost.setEntity(formEntity); 

3、maven依赖

<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.76</version>
</dependency>

 


完整代码:


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yc
 * @Description:
 * @Date: 2021/07/27/18:32
 */
public class HttpClientUtil {

    public static String url = "http://192.168.9.27:3080/co/cmd/deleteProject";

    public static String deletePost() throws IOException {
        String string = "";

        // 获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 声明post请求
        HttpPost httpPost = new HttpPost(url);

        // 设置请求头,在post请求中限制了浏览器才能访问
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");
        httpPost.addHeader("Accept", "*/*");
        httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.addHeader("Content-Type", "application/json");
//        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
        httpPost.addHeader("Connection", "keep-alive");
        // 设置token
        httpPost.addHeader("Authorization","eyJ0eXAiOiJKV1QiLCJhbGciOiJIDASDUzI1NiJ9.eyJleHAiOjE2Mjc0NTQzODYsInVzZXJuYW1lIjoiYWJjZCJ9.MYvNg03txeNm_KiI27fdS0KViVxWhLntDjBjiP44UYQDASCSACCSA");

     JSONObject json = new JSONObject();
     json.put("filePath","js");
     json.put("projectId","61020ccdfd33d86b6abe8745");
     json.put("type","fileFolder");
     
// Post请求发送json类型数据,将Content-Type设置为text/plain类型 httpPost.setEntity(new StringEntity(json.toString(),"UTF-8"));      // 设置参数(Post请求发送非json数据类型) //List<NameValuePair> parameters = new ArrayList<NameValuePair>(); //for(String key:json.keySet()) { // parameters.add(new BasicNameValuePair(key, json.getString(key))); //}      // 将Content-Type设置为application/x-www-form-urlencoded类型 //UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8"); //httpPost.setEntity(formEntity); // 发送请求 CloseableHttpResponse response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); string = EntityUtils.toString(entity, "utf-8"); } else { string = "操作失败"; return string; } // 关闭资源 response.close(); httpClient.close(); return string; } }

posted @ 2021-07-28 14:58  可楽  阅读(5161)  评论(0编辑  收藏  举报