获取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

// 5.4.1
{
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

//5.3.6
public Result searchList(Vo vo) {
HashMap 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);
}

你可能感兴趣的:(java,json,http)