• post发送请求


    post发送文件请求
    public static void main(String[] args) throws IOException {
            //请求 url
            String url = "http://";
    
            // keyValues 保存普通参数
            Map<String, Object> keyValues = new HashMap<>();
    
            // filePathMap 保存文件类型的参数名和文件路径
            Map<String, String> filePathMap = new HashMap<>();
            String paramName = "file";
            String filePath = "E:\\dailySoft\\postman\\files\\加密文件.zip";
            filePathMap.put(paramName, filePath);
    
            //headers
            Map<String, Object> headers = new HashMap<>();
            headers.put("Authorization","xxxxxxxxxxxxxxxxxxx");
            HttpResponse response = postFormData(url, filePathMap, keyValues, headers);
            System.out.println(response);
        }
    
    public static HttpResponse postFormData(String urlStr, Map<String, String> filePathMap, Map<String, Object> keyValues, Map<String, Object> headers) throws IOException {
            HttpResponse response;
            HttpURLConnection conn = getHttpURLConnection(urlStr, headers);
            //分隔符,可以任意设置,这里设置为 MyBoundary+ 时间戳(尽量复杂点,避免和正文重复)
            String boundary = "MyBoundary" + System.currentTimeMillis();
            //设置 Content-Type 为 multipart/form-data; boundary=${boundary}
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    
            //发送参数数据
            try (DataOutputStream out = new DataOutputStream(conn.getOutputStream())) {
                //发送普通参数
                if (keyValues != null && !keyValues.isEmpty()) {
                    for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
                        writeSimpleFormField(boundary, out, entry);
                    }
                }
                //发送文件类型参数
                if (filePathMap != null && !filePathMap.isEmpty()) {
                    for (Map.Entry<String, String> filePath : filePathMap.entrySet()) {
                        writeFile(filePath.getKey(), filePath.getValue(), boundary, out);
                    }
                }
    
                //写结尾的分隔符--${boundary}--,然后回车换行
                String endStr = BOUNDARY_PREFIX + boundary + BOUNDARY_PREFIX + LINE_END;
                out.write(endStr.getBytes());
            } catch (Exception e) {
                LOGGER.error("HttpUtils.postFormData 请求异常!", e);
                response = new HttpResponse(500, e.getMessage());
                return response;
            }
    
            return getHttpResponse(conn);
        }
    
    • 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
    接受Java发送的post请求方法
        @PostMapping(value="testjavasendpost")
        public void testjavasendpost(@RequestBody List<Object> params){
            System.out.println("dataSource = 1");
        }
    
    • 1
    • 2
    • 3
    • 4
    发送post请求
    public static String sendJsonPost(String url, String sendData) throws MyException {
            String body = "";
            try {
                CloseableHttpClient client = HttpClients.createDefault();
    
                //创建post方式请求对象
                HttpPost httpPost = new HttpPost(url);
    
                //装填参数
                StringEntity s = new StringEntity(sendData, "UTF-8");
                s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                //设置参数到请求对象中
                httpPost.setEntity(s);
                //设置header信息
                httpPost.setHeader("Content-type", "application/json");
                httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                httpPost.setHeader("Authorization","Basic Z2F0aGVyOiU4QzhlU0Fmfk5IWGFPamtRbnA=");
    
                //执行请求操作,并拿到结果(同步阻塞)
                HttpResponse response = client.execute(httpPost);
                //获取结果实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    //按指定编码转换结果实体为String类型
                    body = EntityUtils.toString(entity, "UTF-8");
                }
                EntityUtils.consume(entity);
                //为防止频繁调用一个接口导致接口爆掉,每次调用完成后停留100毫秒
                //Thread.sleep(100);
            } catch (Exception e) {
                throw new MyException("JSON数据发送失败,异常:"+e.getMessage());
            }
            return body;
        }
    
    • 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
  • 相关阅读:
    如何在PHP应用中处理跨域请求?
    LC142.环形链表II
    点云绪论(点云数据及获取、点云数据处理、常用软件及开源库)
    文件上传 [ACTF2020 新生赛]Upload1
    SSM基于WEB的房屋出租管理系统 毕业设计-附源码261620
    驱动应该怎么学
    初探Vue3环境搭建与vnm使用
    Anaconda常用操作(亲测有效果)
    七月集训(第26天) —— 并查集
    Word控件Spire.Doc 【页面设置】教程(3):在 C#、VB.NET 中设置 Word 页边距
  • 原文地址:https://blog.csdn.net/dabaoai123123/article/details/132826940