java实现http post 请求

1.构建Post请求

    public JSONObject getHttpInfo(Param param) {
        HttpPost httpPost;
        try {
            httpPost = new HttpPost("");
            StringEntity stringEntity = new StringEntity(JSON.toJSONString(param), StandardCharsets.UTF_8);
            stringEntity.setContentType("application/json");
            httpPost.setEntity(stringEntity);
        } catch (Exception e) {
            throw new BusinessException("genPost error" + e.getMessage());
        }
        return getHttpResponse(httpPost);
    }

2.获取请求结果

    private JSONObject getHttpResponse(HttpUriRequest request) {
        CloseableHttpClient httpClient;
        try {
            SSLContext sslContext = SSLContexts.custom()
                    .loadTrustMaterial(null, (X509Certificate[] x509Certificates, String s) -> true).build();
            //创建httpClient
            httpClient = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy())
                    .setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
            CloseableHttpResponse httpResponse = httpClient.execute(request);
            //此处可设置解码方式
            BufferedReader reader = IoUtil.getUtf8Reader(httpResponse.getEntity().getContent());
            String inputLine;
            StringBuilder stringBuffer = new StringBuilder();
            while ((inputLine = reader.readLine()) != null) {
                stringBuffer.append(inputLine);
            }
            reader.close();
            JSONObject respResult = JSONObject.parseObject(stringBuffer.toString());

            return respResult ;
        } catch (Exception e) {
            throw new BusinessException("Http请求失败" + e.getMessage());
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,这里是一个Java代码示例,用于发起HTTP POST请求: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpPostRequestExample { public static void main(String[] args) throws Exception { // 定义POST请求的URL String url = "http://example.com/api/endpoint"; // 创建URL对象 URL obj = new URL(url); // 创建HttpURLConnection对象 HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为POST con.setRequestMethod("POST"); // 设置请求头部信息 con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Content-Type", "application/json"); // 设置可写入请求内容 con.setDoOutput(true); // 构建请求体 String requestBody = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; // 获取输出流并写入请求体 OutputStream outputStream = con.getOutputStream(); outputStream.write(requestBody.getBytes()); outputStream.flush(); outputStream.close(); // 获取请求响应码 int responseCode = con.getResponseCode(); System.out.println("Response Code : " + responseCode); // 读取响应内容 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 打印响应结果 System.out.println("Response : " + response.toString()); } } ``` 请注意,上述代码中的URL和请求头部信息是示例值,你需要根据实际情况进行修改。此外,你还可以根据需要修改请求体内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值