java中如何使用http工具类请求已有URL路径,并获取response结果

使用http工具类访问已有URL路径的方式有很多种。下面做一下简单介绍:

1,使用HttpUtil工具

 String apiUrl = "https://api.jianzhugang.com/v1/pub/";
        Boolean flag = true ;
        List<GroupInfo> groupInfoList = new ArrayList<>();
        Map<String, Object> map = new HashMap<String, Object>(16) {{
                put("size", 100);
            }};
            String result = HttpUtil.post(apiUrl, map);
            JSONObject resultObject = JSONObject.parseObject(result);
            JSONArray resultArray = resultObject.getJSONArray("data");  

直接使用post方法,两个参数为接口路径–url,以及接口请求参数map;该工具类除了post还有postJson等多个方法。

2,使用restTemplate工具

 HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        List list = new ArrayList<>();
        list.add(check);
        Map<String, Object> map = new HashMap<String, Object>(16) {{
            put("keyt", key);
            put("batchNo", GenerateUUID.getUUID());
            put("reason", "身份核验");
            put("persons", list);
        }};
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(map, headers);
        ResponseEntity<JSONObject> text = restTemplate.exchange(apiUrl, HttpMethod.POST, requestEntity, JSONObject.class);
        

restTemplate工具下提供了多个方法。有不同种参数类型,选择合适的使用即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,可以使用Apache HttpClient库来发送HTTP请求,包括PUT和DELETE请求。下面是一个示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class HttpUtils { private static final HttpClient httpClient = HttpClients.createDefault(); public static String sendPutRequest(String url, String requestBody) throws IOException { HttpPut httpPut = new HttpPut(url); httpPut.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON)); HttpResponse response = httpClient.execute(httpPut); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity); EntityUtils.consume(responseEntity); return responseString; } public static String sendDeleteRequest(String url) throws IOException { HttpDelete httpDelete = new HttpDelete(url); HttpResponse response = httpClient.execute(httpDelete); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity); EntityUtils.consume(responseEntity); return responseString; } } ``` 使用时,可以调用`sendPutRequest`方法发送PUT请求,调用`sendDeleteRequest`方法发送DELETE请求。例如: ```java try { String putUrl = "http://example.com/api/resource"; String putRequestBody = "{\"key\": \"value\"}"; String putResponse = HttpUtils.sendPutRequest(putUrl, putRequestBody); System.out.println("PUT Response: " + putResponse); String deleteUrl = "http://example.com/api/resource/123"; String deleteResponse = HttpUtils.sendDeleteRequest(deleteUrl); System.out.println("DELETE Response: " + deleteResponse); } catch (IOException e) { e.printStackTrace(); } ``` 请注意,这只是一个简单的示例,实际使用可能需要根据具体情况进行相应的错误处理和参数设置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值