随笔 - 127  文章 - 0  评论 - 0  阅读 - 74293

HttpClient发送Post请求传递json、普通参数

首先引入依赖

复制代码
        <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>
复制代码

 

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类型或则json类型
        httpPost.setEntity(new StringEntity(json.toString(),"UTF-8"));
复制代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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: HttpClient发送Post请求
 * @Date: 2021/07/27/18:32
 */
public class HttpClientUtil {
 
    //发送请求的url
    public static String url = "http://192.168.9.247:3080/co/cmd/deleteProject";
 
    public static void deletePost() throws IOException {
     
        // 获取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");
     
     // 发送 json 类型数据,通过new StringEntity(),可将Content-Type设置为text/plain类型
        httpPost.setEntity(new StringEntity(json.toString(),"UTF-8"));
 
     // 设置参数(发送 普通参数 数据类型)
     /*
        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 string = EntityUtils.toString(entity, "UTF-8");
            System.out.println(string);
        }
        else
        {
            System.out.println("删除失败,请重试!!!");
        }
 
        // 关闭response、HttpClient资源
        response.close();
        httpClient.close();
    }
}<br><br>

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);
复制代码

 

  

原文来自:https://blog.csdn.net/qq_43758789/article/details/119222073

posted on   groby  阅读(2972)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示