hutool自定义请求头请求接口
一.案列代码
1.1 导入Maven依赖
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.1.0</version> </dependency>
1.2编写测试类(使用Junit单元测试)
package com.grand.modelSMS;
import org.junit.Test;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
public class JunitTest {
@Test
public void test1() {
//1. 组建请求json参数
JSONObject json = new JSONObject();
json.put("appkey", "2e351b12c5030");
json.put("androidTitle", "PUSHDEMO");
Integer[] plat=new Integer[] {1};
json.put("plats",plat);
json.put("target", 4);
String[] registrationId=new String[] {"16772de0f6071dd4024b25de"};
json.put("registrationIds",registrationId);
json.put("content","test");
json.put("type", 1);
String url=" http://api.push.mob.com/v2/push";
/*发送post请求并接收响应数据
* 采用的是一种叫链式编程的方式):
header对应的是请求头。
body对应的是请求体(包含参数和参数值)。
HttpRequest里面包含Post、GET、Delete、Put等常用的RestFul方式。*/
String post = HttpRequest.post(url)
.header("key","2e351b12c5030")
.header("sign","ee29e949d8c480a67e61e7921ac2dce7")
.body(json)
.execute().body();
System.out.println(post);
}
}
方法解释(上面采用的是一种叫链式编程的方式):
header对应的是请求头。
body对应的是请求体(包含参数和参数值)。
HttpRequest里面包含Post、GET、Delete、Put等常用的RestFul方式。
1.3打印如下:

以上代码亲测有效
作者:皓月无边*半步青莲
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号