业务流程---登录----模块操作。。。。----退出结束
设置全局变量
static String JSESSIONID; // Cookie全局变量
static String Domain;
static String Path;
static int Version;
Cookie集成
/**
* POST请求
*
* @param url
* @param parameters
* @param headers
* @return
*/
public static String doPost(String url, String parameters, String headers) {
// 1 创建请求连接
CloseableHttpClient closeableHttpClient = null;
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie( "JSESSIONID", JSESSIONID );
cookie.setVersion( Version );
cookie.setDomain( Domain );
cookie.setPath( Path );
cookieStore.addCookie( cookie );
closeableHttpClient = HttpClients.custom()
.setRedirectStrategy( new LaxRedirectStrategy() ) //自动跟随重定向
.setDefaultCookieStore( cookieStore ) //
.build();
HttpPost httpPost = new HttpPost( url );
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
// 代理
if (openProxy) {
httpPost.setConfig( requestConfig );
}
// 2 判断参数不为空值时,进行切割
if (!CommonMethord.isEmpty( parameters )) {
String[] parameter_array = parameters.split( "&" );
for (String line : parameter_array) {
String[] parameter = line.split( "=" );
if (parameter.length >= 2) {
formparams.add( new BasicNameValuePair( parameter[0], parameter[1] ) );
} else {
formparams.add( new BasicNameValuePair( parameter[0], "" ) );
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity( formparams, Consts.UTF_8 );
httpPost.setEntity( entity );
}
// 处理头部信息
if (!CommonMethord.isEmpty( headers )) {
String[] header_array = headers.split( ";" );
for (String line : header_array) {
String[] header = line.split( "=" );
httpPost.addHeader( header[0], header[1] );
}
}
// 获取返回对象
String result = "";
CloseableHttpResponse resp;
try {
// 发送请求
HttpClientContext localContext = HttpClientContext.create();
resp = closeableHttpClient.execute( httpPost, localContext );
// 获取Cookie信息
List<Cookie> cookies = localContext.getCookieStore().getCookies();
for (int i = 0; i < cookies.size(); i++) {
if (cookies.get( i ).getName().equals( "JSESSIONID" )) {
JSESSIONID = cookies.get( i ).getValue();
Domain = cookies.get( i ).getDomain();
Path = cookies.get( i ).getPath();
Version = cookies.get( i ).getVersion();
}
System.out.println( "========完整值===========" + cookies.get( i ) );
System.out.println( "========完整值===========" + cookies.get( i ).getValue() );
}
// http 状态 200 404 302 500
StatusLine line = resp.getStatusLine(); //返回带有Cookie
log.info( "状态返回码:" + line );
// 结果
HttpEntity httpEntity = resp.getEntity();
// 结果转换
result = EntityUtils.toString( httpEntity, "utf-8" );
log.info( "返回结果:" + result );
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
// 测试
// 测试
public static void main(String[] args) {
HttpClient.openProxy = true;
doPost( "http://192.168.31.193:8080/consumer/loginAction.do?method=login",
"logonname=admin&password=abc123&logindate=2020-07-27&Submit=",
"" );
doPost( "http://192.168.31.193:8080/consumer/repFileAction.do?method=exportTemplate",
"urlpkid=&urlreasons=&organ_name=滨海农商行(全辖汇总)&databatch=2020-07-28&organ_id=10&report_id=0&target_id=0",
"" );
}
}
许久不用,生疏了,擦他大爷的蛋,真几把坑~~~~~~~~~~~~
原文:https://www.cnblogs.com/Alexr/p/13414281.html