package com.yinchengku.apollo;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
public class KeepCookies {
String JSESSIONID;
public static void main(String[] args) {
}
public String getJSESSIONID(URI uri) throws Exception, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(uri);
HttpClientContext context = HttpClientContext.create();
HttpResponse f = httpclient.execute(httppost, context);
String j = EntityUtils.toString(f.getEntity());
System.out.println(j);
List<Cookie> list = context.getCookieStore().getCookies();// 获取Cookie
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals("JSESSIONID")) {
// 获取JSESSIONID
JSESSIONID = list.get(i).getValue();
System.out.println("JSESSIONID = " + JSESSIONID);
}
// if(list.get(i).getName().equals("cookie_user")){
// String cookie_user = list.get(i).getValue();
// System.out.println("cookie_user = "+cookie_user);
// }
}
return JSESSIONID;
}
public String test(URI u,String jsessionid) throws Exception {
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", JSESSIONID);
System.out.println(cookie);
cookie.setVersion(0);
cookie.setDomain("host");
cookie.setPath("/");
cookieStore.addCookie(cookie);
CloseableHttpClient httpclienta = HttpClients.custom()
.setDefaultCookieStore(cookieStore)//设置Cookie
.build();
HttpPost httpgPost = new HttpPost(u);
CloseableHttpResponse response = httpclienta.execute(httpgPost);
return EntityUtils.toString(response.getEntity());
}
}
import java.io.IOException;
import java.net.URI;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
public class KeepCookies {
String JSESSIONID;
public static void main(String[] args) {
}
public String getJSESSIONID(URI uri) throws Exception, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(uri);
HttpClientContext context = HttpClientContext.create();
HttpResponse f = httpclient.execute(httppost, context);
String j = EntityUtils.toString(f.getEntity());
System.out.println(j);
List<Cookie> list = context.getCookieStore().getCookies();// 获取Cookie
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals("JSESSIONID")) {
// 获取JSESSIONID
JSESSIONID = list.get(i).getValue();
System.out.println("JSESSIONID = " + JSESSIONID);
}
// if(list.get(i).getName().equals("cookie_user")){
// String cookie_user = list.get(i).getValue();
// System.out.println("cookie_user = "+cookie_user);
// }
}
return JSESSIONID;
}
public String test(URI u,String jsessionid) throws Exception {
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", JSESSIONID);
System.out.println(cookie);
cookie.setVersion(0);
cookie.setDomain("host");
cookie.setPath("/");
cookieStore.addCookie(cookie);
CloseableHttpClient httpclienta = HttpClients.custom()
.setDefaultCookieStore(cookieStore)//设置Cookie
.build();
HttpPost httpgPost = new HttpPost(u);
CloseableHttpResponse response = httpclienta.execute(httpgPost);
return EntityUtils.toString(response.getEntity());
}
}