java根据cookie取出页面上的值

根据cookie取出页面上的值

拼接请求头,替换cookie

需要引入的包

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
Map<String, String> headers = new Map<>();
headers.put("cookie",cookie);

新建map放入 请求接口需要的参数

FormBody map = new FormBody.Builder()
	.add("pageAnchor","1")
	.add("pageSize","10").build();

String url =“http…”;

request

Request request = new Request.Builder()
	.url(url)
	.addHeader("Cookie",cookie)
	.addHeader("Access-Control-Allow-Origin","*")
	.addHeader("Access-Control-Allow-Credentials","true")
	.post(map)
	.build();
	Response execute = okHttpClient.newCall(request).execute();
	String requestBody = new String(execute.body().bytes(),"utf-8");
	

接收数据

JSONObject jsonObject = JSONObject.parseObject(requestBody);
if(StringUtils.isNotBlank(jsonObject.getString("response"))){
	String res =  jsonObject.getString("response");
	JSONObject jsonObject1 = JSONObject.parseObject(res);
	// 页面接口的响应值
	String num = jsonObject1.getString("num");
	System.out.println("获取的数据:"+num);
}else{
	System.out.println("获取失败!");
}

你可能感兴趣的:(技术积累,java)