场景
webview已经加载登录页面,然后用户输入账号和密码,登录成功了。需要调用java的post或者get方法请求某个接口。本例以get请求为例,根据实际情况处理。
获得cookies
Map<String, List<String>> result = cookieManager.get(new URI("http://xxxxx"), new HashMap<>());
java.util.List cookies = result.get("Cookie");
String cookie = (String) cookies.get(0);
String[] cookieArray = cookie.split("; ");
String[] uuidd=cookieArray[1].split("=");
String uuid=uuidd[1];
String[] appidd=cookieArray[2].split("=");
String appid=appidd[1];
System.out.println(appid+":"+uuid);
使用Unirest构造请求header
appid、uuid、cookie是上一步获取的值。按照实际情况拼接
HttpResponse<JsonNode> jsonResponse= Unirest.get("http://xxxxx")
.header("Accept", "application/json, text/javascript, */*; q=0.01")
.header("Accept-Encoding", "gzip, deflate")
.header("Accept-Language", "zh-CN,zh;q=0.9")
.header("appid", appid)
.header("Connection", "keep-alive")
.header("Content-Type", "application/json")
.header("Cookie",cookie)
.header("Referer", "http://xxxx/")
//todo TraceID
// .header("TraceID", "484b4ae0-53ed-430b-8e02-44d138e70df2")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36")
.header("uuid", uuid)
.header("Host", "xxxx")
.header("X-Requested-With", "XMLHttpRequest")
.asJson();