I use httpclient-4.5.1.jar. If the version of httpclient is too low, this method may not be used. It needs to set cookie manually. That method, not discussed for the moment, is mainly to get the returned cookie when landing, and then to fill the value of coolie in the new request header. However, this method is rarely encountered. Basically, the opposite interface will directly return to you a token. The second visit passes this token directly as a parameter to get the data you want.
package test; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONArray; public class JKTest03 { public static void main(String[] args) throws Exception { long st=new Date().getTime(); CloseableHttpClient http = HttpClients.createDefault(); try { String url = "post"; HashMap<String, String> map = new HashMap<String, String>(); map.put("username", "admin"); map.put("password", "pass"); getlogin(http, map, url);//Landing Verification String url2 = "get"; String list = Get(http, url2);//get data System.out.println(list); } finally { http.close(); } long et=new Date().getTime(); System.out.println("Time-consuming:"+(et-st)); // System.out.println("end"); } public static String getlogin(CloseableHttpClient client, Map<String, String> map, String url) throws Exception { String body = ""; String encoding = "utf-8"; // Create a post-style request object HttpPost httpPost = new HttpPost(url); // Loading parameters, conventional format /* * List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if (map != null) { * for (Entry<String, String> entry : map.entrySet()) { * //System.out.println(entry.getKey()+"=="+ entry.getValue()); nvps.add(new * BasicNameValuePair(entry.getKey(), entry.getValue())); } } // Set parameters to the request object * httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); * // Set parameters to the request object httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); */ //The parameter is json format httpPost.setEntity(new StringEntity(JSONArray.toJSONString(map))); // Setting header information // Specified headers [Content-type], [User-Agent] //The parameter is json's requirement. Set Content-type to application/json httpPost.setHeader("Content-type", "application/json");//The default is: application/x-www-form-urlencoded httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // Perform the request operation and get the result (synchronous blocking) CloseableHttpResponse response = client.execute(httpPost); // response. // Get the result entity HttpEntity entity = response.getEntity(); if (entity != null) { // Convert the result entity to String type by the specified encoding body = EntityUtils.toString(entity, encoding); } // Release links response.close(); return body; } public static String Get(CloseableHttpClient http, String url) throws Exception { HttpGet httpget = new HttpGet(url); String body = ""; CloseableHttpResponse response = http.execute(httpget); // response. // Get the result entity HttpEntity entity = response.getEntity(); if (entity != null) { // Convert the result entity to String type by the specified encoding body = EntityUtils.toString(entity, "utf-8"); } // Release links response.close(); return body; } }