栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java发送http请求

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java发送http请求

设置请求头、参数(字节流写入)
 public static void main(String[] args) {
        CloseableHttpResponse response=null;
        String entityStr = null;
        //登录url
        String url ="https://";
        // 获取连接客户端工具
        CloseableHttpClient httpClient= HttpClients.createDefault();
        try {
            // 创建POST请求对象
            HttpPost httpPost = new HttpPost(url3);
            httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
            httpPost.addHeader("publicKey", publicKey);
            httpPost.addHeader("reqid", reqid);
            String stringDate = SimpleDateFormatUtils.getStringDate(new Date(), SimpleDateFormatUtils.PATTERN_TYPE_3);
            httpPost.addHeader("reqtime", stringDate);
            //设置签名
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("year", "2021");
            String s = JSON.toJSONString(jsonObject);
            BasicHttpEntity httpEntity = new BasicHttpEntity();
            httpEntity.setContent(new BufferedServletInputStream(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8)));
            httpPost.setEntity(httpEntity);
            String paramStr = s + "|" + reqid + "|" + stringDate + "|" + publicKey + "|" + privateKey;
            System.out.println("加密前字符串:" + paramStr);
            String resultHexString = SmRsaUtils.encrypt(paramStr, publicKey);
            System.out.println("加密后字符串:" + resultHexString);
            httpPost.addHeader("sign", resultHexString);
            httpPost.addHeader("User-Agent:", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36");
            // 执行请求
            response = httpClient.execute(httpPost);
            // 获得响应的实体对象
            HttpEntity entity = response.getEntity();
            // 使用Apache提供的工具类进行转换成字符串
            int code =response.getStatusLine().getStatusCode();
            System.out.println("StatusCode: " + code);
            Assert.assertEquals(200,code);
            String result= EntityUtils.toString(entity,"UTF-8");
            System.out.println("接口返回结果是:="+result);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                //释放资源
                if(httpClient!=null) {
                    httpClient.close();
                }
                if (response!=null){
                    response.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
发送json格式的参数
 public static void main(String[] args) {
        CloseableHttpResponse response=null;
        String entityStr = null;
        //登录url
        String url ="https://";
        // 获取连接客户端工具
        CloseableHttpClient httpClient= HttpClients.createDefault();
        // 创建POST请求对象
        HttpPost httpPost=new HttpPost(url);
        // httpPost.addHeader post请求 header
        httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        httpPost.addHeader("User-Agent:","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36");
        try{
        //参数封装对象
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id","122551");
            StringEntity stringEntity = new StringEntity(jsonObject.toJSONString());
            httpPost.setEntity(stringEntity);
            // 执行请求
            response=httpClient.execute(httpPost);
            // 获得响应的实体对象
            HttpEntity entity=response.getEntity();
            // 使用Apache提供的工具类进行转换成字符串
            int code =response.getStatusLine().getStatusCode();
            System.out.println("StatusCode: " + code);
            Assert.assertEquals(200,code);
            entityStr= EntityUtils.toString(entity,"UTF-8");
            System.out.println("接口返回结果是:="+entityStr);

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                //释放资源
                if(httpClient!=null) {
                    httpClient.close();
                }
                if (response!=null){
                    response.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356726.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号