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();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值