使用HttpClient发送和接收JSON请求 推荐给大家

使用HttpClient发送和接收JSON请求 推荐

/**
     * post请求
     * @param url
     * @param json
     * @return
     */
    public static JSONObject doPost(String url,JSONObject json){
        
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            HttpResponse res = httpclient.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient 是一个开源的 HTTP 客户端库,用于发送 HTTP 请求接收响应。它提供了一系列的方法来发送 GET 和 POST 请求,并设置请求头。 以下是使用 HttpClient 发送 GET 请求的示例代码: ```java import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); try { URI uri = new URI("http://example.com/api"); HttpGet getRequest = new HttpGet(uri); // 设置请求头 getRequest.addHeader("User-Agent", "Mozilla/5.0"); HttpResponse response = httpClient.execute(getRequest); // 处理响应 System.out.println(response.getStatusLine().getStatusCode()); // 其他处理逻辑... } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } } ``` 以上示例中,我们创建了一个 HttpClient 对象,并使用 `HttpGet` 请求发送 GET 请求。然后,我们通过 `addHeader` 方法设置了请求头中的 User-Agent 字段,模拟了一个浏览器的 User-Agent。最后,使用 `httpClient.execute(getRequest)` 方法发送请求并获取响应。 以下是使用 HttpClient 发送 POST 请求的示例代码: ```java import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); try { URI uri = new URI("http://example.com/api"); HttpPost postRequest = new HttpPost(uri); // 设置请求头 postRequest.addHeader("Content-Type", "application/json"); // 设置请求体 StringEntity requestBody = new StringEntity("{\"key\":\"value\"}"); postRequest.setEntity(requestBody); HttpResponse response = httpClient.execute(postRequest); // 处理响应 System.out.println(response.getStatusLine().getStatusCode()); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity); System.out.println(responseString); // 其他处理逻辑... } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } } ``` 以上示例中,我们使用 `HttpPost` 请求发送 POST 请求。同样,我们通过 `addHeader` 方法设置了请求头中的 Content-Type 字段为 application/json。然后,我们设置了请求体为一个 JSON 字符串。最后,使用 `httpClient.execute(postRequest)` 方法发送请求并获取响应。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值