获取token方式

方法1

{
public static String test(){
JSONObject obj = new JSONObject();
//请求的参数按照要求放入
String parm3 = “****”;
String res = “”;
String result = null;
PrintWriter out = null;
BufferedReader in = null;
try {
URL realUrl;
realUrl = new URL("*url");
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod(“POST”);
// 设置通用的请求属性
conn.setRequestProperty(“Cookie”, “sid=”);
conn.setRequestProperty(“Postman-Token”, “”);
conn.setRequestProperty(“Content-Type”, “application/json”);
conn.setRequestProperty(“Content-Length”, “”);
conn.setRequestProperty(“Host”, “”);
conn.setRequestProperty(“User-Agent”, “PostmanRuntime/7.26.10”);
conn.setRequestProperty(“Accept”, "
/
");
conn.setRequestProperty(“Accept-Encoding”, “gzip, deflate, br”);
conn.setRequestProperty(“Connection”, “keep-alive”);
conn.setRequestProperty(“Cache-Control”, “no-cache”);
//设置header信息
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
//json传参
out.print(parm3);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(res);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {// 使用finally块来关闭输出流、输入流
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
}

方法2

// <hutool.version>5.4.1</hutool.version>
{
public static String test(){
String pramEnd = “***”;
String result = HttpRequest.post("*url")
//头信息,多个头信息多次调用此方法即可
.header(Header.COOKIE, “sid=”)
.header(Header.CONTENT_TYPE, “application/json”)
.header(Header.CONTENT_LENGTH, “”)
.header(Header.HOST, “”)
.header(Header.USER_AGENT, “PostmanRuntime/7.26.10”)
.header(Header.ACCEPT, "
/
")
.header(Header.ACCEPT_ENCODING, “gzip, deflate, br”)
.header(Header.CONNECTION, “keep-alive”)
.header(Header.CACHE_CONTROL, “no-cache”)
//表单内容
.body(pramEnd)
//超时,毫秒
.timeout(20000)
.execute().body();
JSONObject.parse(result);
return result;
}
}

方法3

//<hutool.version>5.3.6</hutool.version>
public Result searchList(Vo vo) {
HashMap<String, Object> paramFileMap = new HashMap<>();
paramFileMap.put(“name”, vo.getName());
String result =HttpUtil.createGet(searchAffairBusinessListUrl)
//验证
.header(“Authorization”, "Bearer " + getToken())
//参数
.form(paramFileMap).execute().body();
return Result.success(result);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值