static String loginIndexUrl = "http://127.0.0.1/xiangmuming";
public String getCookieVal(String url, String JSESSIONID) {
String cookieVal = "";
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
// 发送get请求
HttpGet request = new HttpGet(url);
request.setHeader("Cookie", JSESSIONID);
try {
CloseableHttpResponse response = httpclient.execute(request);
HttpEntity entity = null;
try {
Header[] headers = response.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
// System.out.println(headers[i].getName() +"=="+ headers[i].getValue());
if ("Set-Cookie".equals(headers[i].getName())) {
cookieVal = headers[i].getValue().substring(0, headers[i].getValue().indexOf(";"));
break;
}
}
} finally {
EntityUtils.consume(entity);
response.close();
}
} finally {
httpclient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return cookieVal;
}
public int doLogin(String userName, String password, String cookie) {
int statusCode = 0;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(loginUrl);
httpPost.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
httpPost.setHeader("Cookie", cookie);
httpPost.setHeader("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("usertype", "0"));
nvps.add(new BasicNameValuePair("type", "login"));
nvps.add(new BasicNameValuePair("username", userName));
nvps.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
httpClient = HttpClients.createDefault();
response = httpClient.execute(httpPost);
statusCode = response.getStatusLine().getStatusCode();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return statusCode;
}
/**
* post请求(用于请求json格式的参数)
*
* @param url
* @param params
* @return
*/
public String doUrlPost(String url, List<NameValuePair> nvps, String JSESSIONID) {
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "text/plain, */*; q=0.01");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpPost.setHeader("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
httpPost.setHeader("Cookie", JSESSIONID);
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
} else {
System.out.println("请求返回:" + state + "(" + url + ")");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}