Java使用hutool工具类发送网络请求

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.8.6</version>
</dependency>

使用案例
1.httpUtil使用post和get请求

String url = "https://xxx/xx";//指定URL
Map<String, Object> map = new HashMap<>();//存放参数
map.put("a", 1);
HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
headers.put("xxx", xxx);
//发送get请求并接收响应数据
String result= HttpUtil.createGet(url).addHeaders(headers).form(map).execute().body();
//发送post请求并接收响应数据
String result= HttpUtil.createPost(url).addHeaders(headers).form(map).execute().body();

2.向指定URL发送DELETE请求,并携带请求头headers。

String url = "https://xxx/delete/"+id;//指定URL携带ID
HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
headers.put("xxx", xxx);
//发送delete请求并接收响应数据
String result= HttpUtil.createRequest(Method.DELETE, url).addHeaders(headers).execute().body();

3.Http请求-HttpRequest

本质上,HttpUtil中的get和post工具方法都是HttpRequest对象的封装,因此如果想更加灵活操作Http请求,可以使用HttpRequest。

@Test
public void testHttps() throws Exception {
    
    JSONObject json = new JSONObject();
    json.put("username", "1332788xxxxxx");
    json.put("password", "123456.");
    
    String result = HttpRequest.post("https://api2.bmob.cn/1/users")
            .header("Content-Type", "application/json")//头信息,多个头信息多次调用此方法即可
            .header("X-Bmob-Application-Id","2f0419a31f9casdfdsf431f6cd297fdd3e28fds4af")
            .header("X-Bmob-REST-API-Key","1e03efdas82178723afdsafsda4be0f305def6708cc6")
            .body(json)
            .execute().body();
       System.out.println(result);   
}

### 如何使用Hutool工具类发送HTTP请求 #### 发送GET请求 为了发送GET请求,可以利用`HttpUtil.get()`方法。此方法允许指定URL以及可选的请求参数和其他配置项。 ```java import cn.hutool.http.HttpUtil; public class HttpGetExample { public static void main(String[] args) { String url = "http://example.com/api"; // 构建查询字符串 String param = "param1=value1&param2=value2"; // 执行GET请求并接收响应结果作为String类型 String result = HttpUtil.get(url, param); System.out.println(result); } } ``` #### 发送POST请求 对于POST请求,则应采用`HttpUtil.post()`函数。该函数同样支持传递额外的数据到服务器端,并能自定义更多选项如头部信息等。 ```java import cn.hutool.http.Header; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; public class HttpPostExample { public static void main(String[] args) throws Exception { String postUrl = "http://example.com/postApi"; String postData = "{\"key\":\"value\"}"; HttpResponse response = HttpRequest.post(postUrl) .header(Header.CONTENT_TYPE, "application/json") // 设置Content-Type为JSON格式 .body(postData) // 添加要提交的内容体 .execute(); // 发起请求 int statusCode = response.getStatus(); String body = response.body(); System.out.println("Status Code: " + statusCode); // 输出状态码 System.out.println("Body:\n" + body); // 输出响应主体 } } ``` #### 处理超时设置及其他高级特性 当需要更精确地控制网络通信行为时,比如调整连接或读取超时时限,可以通过链式调用来完成这些设定: ```java HttpResponse respWithTimeouts = HttpRequest.get("http://slowapi.example.com") .timeout(5000) // 超时时间为5秒 .execute(); ``` 以上展示了基本的GET和POST请求方式及其扩展功能[^1]。除此之外,还能够轻松构建PUT、DELETE以及其他类型的RESTful API调用[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值