1、使用httpClient4.1.2
/**
* post请求 获取cookie
* @param url
* @param params
* @param cookie
* @param tk
* @return
*/
public static Map<String,Object> httpPostGetCookie(String url,Map<String,String> params,String cookie,String tk){
Map<String,Object> map = new HashMap<>();
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000); // 连接超时时间
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 90000); // 数据传输时间
HttpPost post = null;
post = new HttpPost(url);
post.setHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
post.setHeader("Cookie",cookie);
post.setHeader("tk",tk);
// 参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Set<String> keySet = params.keySet();
for(String key:keySet){
list.add(new BasicNameValuePair(key, params.get(key)));
}
try {
post.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);
String result = reponse2String(response);
CookieStore cookieStore = httpClient.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
String WEBTOKEN = null;
String tk2 = null;
String k = null;
for(int i = 0; i < cookies.size(); i++){
if (cookies.get(i).getName().equals("WEBTOKEN")) {
WEBTOKEN = cookies.get(i).getValue();
}
if (cookies.get(i).getName().equals("tk")) {
tk2 = cookies.get(i).getValue();
}
if (cookies.get(i).getName().equals("k")) {
k = cookies.get(i).getValue();
}
}
String cookiess = "WEBTOKEN="+WEBTOKEN+"; tk="+tk2+"; k="+k;
map.put("cookie", cookiess);
map.put("tk", tk2);
map.put("result", result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 关闭
httpClient.getConnectionManager().shutdown();
return map;
}
/**
* 转化返回参数
* @param response
* @return
*/
public static String reponse2String(HttpResponse response){
HttpEntity entity = response.getEntity();
String body = null;
try {
body = EntityUtils.toString(entity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return body;
}
通过 CookieStore cookieStore = httpClient.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
来获取
2、使用java 自带的url来获取 需要使用CookieManager
/**
* url转为二进制
* @param str
* @return
*/
public static Map<String,Object> url2Byte(String str){
BufferedInputStream buffer = null;
DataInputStream dataIn = null;
ByteArrayOutputStream bos = null;
DataOutputStream dos = null;
byte[] bArray = null;
Map<String,Object> map = new HashMap<>();
try {
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(manager);
URL url = new URL(str);
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
buffer = new BufferedInputStream(is);
dataIn = new DataInputStream(buffer);
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
byte[] buf = new byte[1024];
while (true) {
int len = dataIn.read(buf);
if (len < 0)
break;
dos.write(buf, 0, len);
}
List<HttpCookie> listCookie = manager.getCookieStore().getCookies();
String JSESSIONID = null;
String tk = null;
String k = null;
for(int i = 0; i < listCookie.size(); i++){
if (listCookie.get(i).getName().equals("JSESSIONID")) {
JSESSIONID = listCookie.get(i).getValue();
}
if (listCookie.get(i).getName().equals("tk")) {
tk = listCookie.get(i).getValue();
}
if (listCookie.get(i).getName().equals("k")) {
k = listCookie.get(i).getValue();
}
}
String cookie = "JSESSIONID="+JSESSIONID+"; tk="+tk+"; k="+k;
map.put("cookie", cookie);
map.put("JSESSIONID", JSESSIONID);
bArray = bos.toByteArray();
map.put("bArray", bArray);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}