方式一,通过HttpClientContext 传递cookie
package com.httpclient; import org.apache.http.HttpEntity; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.protocol.HttpClientContext; 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; import java.io.IOException; // httpGet public class TestCookie5 { public static void main(String[] args) throws IOException { CloseableHttpClient closeableHttpClient =null; try { CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie( "JSESSIONID","E40C177615A2C5AA98DAAE3226E4FF03" ); cookie.setVersion( 0 ); cookie.setDomain( "192.168.31.193" ); cookie.setPath( "/consumer" ); cookieStore.addCookie( cookie ); closeableHttpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://192.168.31.193:8080/consumer/menu.do" ); System.out.println( httpGet.getRequestLine()); CloseableHttpResponse httpResponse =null; try { HttpClientContext localContext = HttpClientContext.create();// 通过HttpClientContext 传递Cookie localContext.setCookieStore( cookieStore ); // 执行get httpResponse = closeableHttpClient.execute( httpGet,localContext ); //获取响应实体 HttpEntity entity = httpResponse.getEntity(); //响应状态 System.out.println(httpResponse.getStatusLine()); if(entity!=null){ System.out.println(EntityUtils.toString( entity,"utf-8" )); } }catch (IOException e){ e.printStackTrace(); }finally { httpResponse.close(); } }catch (Exception e){ e.printStackTrace(); }finally { closeableHttpClient.close(); } } }
方式二,通过cookieStore 传递cookie
package com.httpclient;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
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.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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// httpPost ,Ok
public class TestCookie6 {
public static void main(String[] args) throws IOException {
CloseableHttpClient closeableHttpClient =null;
try {
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie( "JSESSIONID","93DAA2B26DE595BE1936DBCBAF52DD1B" );
cookie.setVersion( 0 );
cookie.setDomain( "172.16.87.74" );
cookie.setPath( "/consumer" );
cookieStore.addCookie( cookie );
closeableHttpClient = HttpClients.custom()
.setDefaultCookieStore( cookieStore )
.build();
//HttpGet httpGet = new HttpGet( "http://172.16.87.74:8080/consumer/menu.do" );
HttpPost httpPost = new HttpPost( "http://172.16.87.74:8080/consumer/repFileAction.do?method=exportTemplate" ) ;
List<NameValuePair> list = new ArrayList<>( );
list.add( new BasicNameValuePair( "urlpkid","" ) );
list.add( new BasicNameValuePair( "urlreasons","" ) );
list.add( new BasicNameValuePair( "organ_name","滨海农商行(全辖汇总)" ) );
list.add( new BasicNameValuePair( "databatch","2020-07-28" ) );
list.add( new BasicNameValuePair( "organ_id","10" ) );
list.add( new BasicNameValuePair( "report_id","0" ) );
list.add( new BasicNameValuePair( "target_id","0" ) );
System.out.println( httpPost.getRequestLine());
CloseableHttpResponse httpResponse =null;
try {
httpResponse = closeableHttpClient.execute( httpPost );
HttpEntity entity = httpResponse.getEntity();
System.out.println(httpResponse.getStatusLine());
if(entity!=null){
System.out.println(EntityUtils.toString( entity,"utf-8" ));
}
}catch (IOException e){
e.printStackTrace();
}finally {
httpResponse.close();
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeableHttpClient.close();
}
}
}