您的位置:首页 > 理论基础 > 计算机网络

使用httpclient模拟登陆

2015-03-27 18:46 411 查看
1,导入jar包



2,配置文件:roadThread.properties

#\u7EBF\u7A0B\u6C60\u7EF4\u62A4\u7EBF\u7A0B\u7684\u6700\u5C11\u6570\u91CF
ExecutorServicePool.corePoolSize=5
#\u7EBF\u7A0B\u6C60\u7EF4\u62A4\u7EBF\u7A0B\u7684\u6700\u5927\u6570\u91CF
ExecutorServicePool.maximumPoolSize=5
#\u7EBF\u7A0B\u6C60\u7EF4\u62A4\u7EBF\u7A0B\u6240\u5141\u8BB8\u7684\u7A7A\u95F2\u65F6\u95F4
#默认5分钟
ExecutorServicePool.keepAliveTime=5
ExecutorServicePool.corePoolSizeThread=5
ExecutorServicePool.maximumPoolSizeThread=7

#初始化线程
ExecutorServicePool.corePoolSizeThreadController=5
#controller最大线程数
ExecutorServicePool.maximumPoolSizeThreadController=10
#controller承受最大的任务队列
ExecutorServicePool.maxTaskController=2000
#重发次数
ExecuteAbstract.resendTimes=4
#是否直接请求直接交付给线程池管理
ExecuteAbstract.isThreadManagement=true
#是否启用代理
ExecuteAbstract.isproxy=true


3,java实现类:

CHeader.java

package test;

/**
* httpClient请求头实体bean
*
* @author fastw
*
*/
public class CHeader {
/** 服务器接受类型 */
private String Accept;
/** 引用那个地址过来 */
private String Referer;
/** 服务器接受类型编码 gzip.default */
private String Accept_Encoding;
/** 服务器接受类型语言 */
private String Accept_Language;
/** 是否有缓存 */
private String Cache_Control;
/** 连接状态 keep-alive */
private String Connection;
/** 文本格式渲染格式 */
private String Content_Type;
private String Host;
/** xml request */
private Boolean x_requested_with;
// 默认就行
private String User_Agent;
/** 响应解析编码 */
private String respCharset;
private String Origin;

/***
* cookie信息
*/
private String cookie;

public CHeader() {
}

public CHeader(String accept, String content_Type, String host) {
Accept = accept;
Content_Type = content_Type;
Host = host;
}

public CHeader(String referer, Boolean x_requested_with) {
this.Referer = referer;
this.x_requested_with = x_requested_with;
}

/**
* @param referer
*            设置referer
*/
public CHeader(String referer) {
this.Referer = referer;
}

public CHeader(String referer, String origin) {
this.Referer = referer;
this.Origin = origin;
}

public CHeader(String accept, String referer, String content_Type,
String host) {
super();
Accept = accept;
Referer = referer;
Content_Type = content_Type;
Host = host;
}

public CHeader(String accept, String referer, String content_Type,
String host, Boolean x_requested_with) {
super();
Accept = accept;
Referer = referer;
Content_Type = content_Type;
Host = host;
this.x_requested_with = x_requested_with;
}

public String getAccept() {
return Accept;
}

/** 服务器接受类型 */
public void setAccept(String accept) {
Accept = accept;
}

public String getReferer() {
return Referer;
}

/** 引用那个地址过来 */
public void setReferer(String referer) {
Referer = referer;
}

public String getAccept_Encoding() {
return Accept_Encoding;
}

/** 服务器接受类型编码 gzip.default ,可为空 */
public void setAccept_Encoding(String accept_Encoding) {
Accept_Encoding = accept_Encoding;
}

public String getAccept_Language() {
return Accept_Language;
}

/** 服务器接受类型语言 可为空默认zh-cn */
public void setAccept_Language(String accept_Language) {
Accept_Language = accept_Language;
}

public String getCache_Control() {
return Cache_Control;
}

/** 是否有缓存,可为空 */
public void setCache_Control(String cache_Control) {
Cache_Control = cache_Control;
}

public String getConnection() {
return Connection;
}

/** 连接状态 keep-alive,可为空 */
public void setConnection(String connection) {
Connection = connection;
}

public String getContent_Type() {
return Content_Type;
}

/** 文本格式渲染格式 */
public void setContent_Type(String content_Type) {
Content_Type = content_Type;
}

public String getHost() {
return Host;
}

public void setHost(String host) {
Host = host;
}

public Boolean getX_requested_with() {
return x_requested_with;
}

/** xml request */
public void setX_requested_with(Boolean x_requested_with) {
this.x_requested_with = x_requested_with;
}

public String getUser_Agent() {
return User_Agent;
}

public void setUser_Agent(String user_Agent) {
User_Agent = user_Agent;
}

public String getCookie() {
return cookie;
}

public void setCookie(String cookie) {
this.cookie = cookie;
}

/**
* @return 编码
*/
public String getRespCharset() {
return respCharset;
}

/**
* 设置响应编码
*/
public void setRespCharset(String respCharset) {
this.respCharset = respCharset;
}

/**
* @return origin
*/
public String getOrigin() {
return Origin;
}

/**
* @param origin
*            要设置的 origin
*/
public void setOrigin(String origin) {
Origin = origin;
}
}


ComparableFutureTask.java

package test;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
* 实现Comparable 接口
*
* @author fastw
* @date 2014-10-4 下午11:29:31
* @param <V>
*/
public class ComparableFutureTask<V> extends FutureTask<V> implements
Comparable<ComparableFutureTask<V>> {
private Object object;

public ComparableFutureTask(Callable<V> callable) {
super(callable);
object = callable;
}
public ComparableFutureTask(Runnable runnable, V result) {
super(runnable, result);
object = runnable;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(ComparableFutureTask<V> o) {
if (this == o) {
return 0;
}
if (o == null) {
return -1; // high priority
}
if (object != null && o.object != null) {
if (object.getClass().equals(o.object.getClass())) {
if (object instanceof Comparable) {
return ((Comparable) object).compareTo(o.object);
}
}
}
return 0;
}
}

class ThreadCallable implements Callable<String> {
private ExecuteRequest request;
private SendRequestPojo pojo;

public ThreadCallable(ExecuteRequest request, SendRequestPojo pojo) {
this.request = request;
this.pojo = pojo;
}

@Override
public String call() throws Exception {
return request.execute(pojo);
}

}


ExecuteRequest.java

package test;

import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;

/**
* 请求执行类
*
* @author fastw
* @date 2014-8-13 下午3:18:21
*/
public class ExecuteRequest {

public static CloseableHttpClient client = HttpClientConnectionPool.getInstance();

public static final String isThreadManagement = InfoUtil.getInstance().getInfo("roadThread", "ExecuteAbstract.isThreadManagement");
public static final int resendTimes = Integer.parseInt(InfoUtil.getInstance().getInfo("roadThread", "ExecuteAbstract.resendTimes"));// 重发次数

private static final Integer corePoolSize = Integer.parseInt(InfoUtil .getInstance().getInfo("roadThread", "ExecutorServicePool.corePoolSize"));

private static final Integer maximumPoolSize = Integer.parseInt(InfoUtil.getInstance().getInfo("roadThread", "ExecutorServicePool.maximumPoolSize"));

private static final Integer keepAliveTime = Integer.parseInt(InfoUtil.getInstance().getInfo("roadThread", "ExecutorServicePool.keepAliveTime"));

public HttpClientContext context;

public ExecuteRequest() {
super();
}

public ExecuteRequest(HttpClientContext context) {
this.context = context;
}

public HttpClientContext getContext() {
return context;
}

/**
* 禁止直接使用
*
* @param requestPojo
* @return
*/
public String execute(SendRequestPojo requestPojo) {

Integer statusCode = 0;// 响应头
String text = null;
long startTime = 0;
for (int j = 0; j < resendTimes; j++) {
startTime = System.currentTimeMillis();
long time = 0;
try {
HttpResponse response = client.execute(requestPojo.getHttpRequestBase(), this.context);
time = System.currentTimeMillis() - startTime;
System.out.println("地址:" + requestPojo.getUrl() + "响应时间:" + time);
statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
if (requestPojo.getHeader() != null && requestPojo.getHeader().getRespCharset() != null) {
text = ParseResponse.parse(response, requestPojo.getHeader().getRespCharset());
} else {
text = ParseResponse.parse(response);
}
} else if (statusCode == 302 || statusCode == 301) {
text = ParseResponse.getLocation(response);
} else if (statusCode >= 400 && statusCode < 500) {
if (statusCode != 404) {
statusCode = 403;
}
} else {
if (statusCode < 400) {
statusCode = 200;
}
}
} catch (Exception e) {

}
if (time != 0) {
break;
} else {
}
}
if (text == null) {
requestPojo.abort();
}
return text;
}

public String getResult(SendRequestPojo pojo) {
String result = null;
if (StringUtils.isNotBlank(pojo.getUrl())) {
//是否交给线程池去管理,暂时不用
if (!isThreadManagement.equals("false")) {
result = this.execute(pojo);
} else {
ComparableFutureTask<String> futureTask = new ComparableFutureTask<String>(new ThreadCallable(this, pojo));
BlockingQueue<Runnable> workQueue = new PriorityBlockingQueue<Runnable>();// 带有执行优先级的队列
new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MINUTES, workQueue).submit(futureTask);
try {
result = futureTask.get();
} catch (Exception e) {

}
}
}
return result;
}

public String get(String url) {
return get(url, null);
}

public String get(String url, CHeader h) {
return getResult(new SendRequestPojo(url, h, true));
}

public String getURL(String url) {
return getResult(new SendRequestPojo(url, null, false));
}

public String getURL(String url, CHeader h) {
return getResult(new SendRequestPojo(url, h, false));
}

public String post(String url, CHeader h, String XmlsOrJson) {
SendRequestPojo pojo = new SendRequestPojo(url, h);
pojo.setXmlsOrJsonEntity(XmlsOrJson);
return getResult(pojo);
}

public String post(String url, Map<String, String> param) {
return post(url, null, param);
}

public String post(String url, CHeader h, Map<String, String> param) {
return getResult(new SendRequestPojo(url, param, h, true));// 为兼容之前的请求默认是true
// uri请求访问
}

public String postURL(String url, Map<String, String> param) {
return getResult(new SendRequestPojo(url, param, null, false));
}

public String postURL(String url, CHeader h, Map<String, String> param) {
return getResult(new SendRequestPojo(url, param, h, false));
}
}


HttpClientConnectionPool.java

package test;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.CodingErrorAction;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.Consts;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.MessageConstraints;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;

/**
* client池类
*
* @author fastw
* @date 2014-9-14 上午11:10:24
*/
public class HttpClientConnectionPool {
/** 将最大连接数增加到200 **/
private final Integer maxTotal = 2000;
/** defaultMaxPerRoute */
private final Integer defaultMaxPerRoute = 1000;
/** 如果内部失败就重发!,最多重发4次 **/
private final int resendTimes = 4;

private static CloseableHttpClient client = null;
private HttpClientBuilder builder = null;
static {
client = getCloseableClient();
}

public static CloseableHttpClient getInstance() {
return client;
}

private static CloseableHttpClient getCloseableClient() {
HttpClientConnectionPool clientPool = new HttpClientConnectionPool();
clientPool.setExecutionCount().setClientConnectionPool()
.setKeepAliveDuration();
return clientPool.getBuilder().build();
}

public HttpClientBuilder getBuilder() {
if (builder == null) {
builder = HttpClientBuilder.create();
}
return builder;
}

/**
* 默认请求次数
*
* @param builder
*/
private HttpClientConnectionPool setExecutionCount() {
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

public boolean retryRequest(IOException exception,
int executionCount, HttpContext context) {
if (executionCount >= resendTimes) {
// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof InterruptedIOException) {
// 超时
return false;
}
if (exception instanceof UnknownHostException) {
// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {
// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {
// ssl握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext
.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// 如果请求是幂等的,就再次尝试
return true;
}
return false;
}

};
getBuilder().setRetryHandler(myRetryHandler);
return this;
}

/**
* 设置线程池
*
* @param builder
*/
private HttpClientConnectionPool setClientConnectionPool() {
X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}

};

SSLContext ctx = null;
Registry<ConnectionSocketFactory> r = null;
try {
// TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext
ctx = SSLContext.getInstance("TLS");
// 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
ctx.init(null, new TrustManager[] { xtm }, new SecureRandom());
SSLContext.setDefault(ctx);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
ctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
r = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sslsf).build();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
r);
SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true)
.build();
cm.setDefaultSocketConfig(socketConfig);
MessageConstraints messageConstraints = MessageConstraints.custom()
.setMaxHeaderCount(200).setMaxLineLength(2000).build();
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE)
.setCharset(Consts.UTF_8)
.setMessageConstraints(messageConstraints).build();
cm.setDefaultConnectionConfig(connectionConfig);
// 将最大连接数增加到200
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加到20
cm.setDefaultMaxPerRoute(defaultMaxPerRoute);
getBuilder().setConnectionManager(cm);
return this;
}

private HttpClientConnectionPool setKeepAliveDuration() {
ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response,
HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
// 如果服务器没有设置keep-alive这个参数,我们就把它设置成5秒
keepAlive = 5000;
}
return keepAlive;
}

};
// 定制我们自己的httpclient

getBuilder().setKeepAliveStrategy(keepAliveStrat);
return this;

}

/** 关闭连接 */
public static void colse(CloseableHttpClient client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}


InfoUtil.java

package test;

import java.util.ResourceBundle;

/**
* 类说明 提取指定资源文件key=>value
*/
public class InfoUtil {
private static InfoUtil instance = null;

private InfoUtil() {
}

public synchronized static InfoUtil getInstance() {
if (instance == null) {
instance = new InfoUtil();
}
return instance;
}

public String getInfo(String name, String key) {
ResourceBundle rb = ResourceBundle.getBundle(name);
return rb.getString(key);
}
}
ParseResponse.java

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

/**
* httpResponse 内容解析
*
* @author fastw
*
*/
public class ParseResponse {
/**
* @param response
*            HttpResponse响应
* @param charset
*            响应编码 默认GBK
* @return
*/
public static String parse(HttpResponse response, String charset) {
return parse(response, charset, false);
}

/**
* 理论上万能解析
*
* @param response
*            HttpResponse响应
*/
public static String parse(HttpResponse response) {
return parse(response, getContentCharset(response),
getContentEncoding(response));
}

public static String getLocation(HttpResponse response) {
return response.getFirstHeader("Location").getValue();
}

/**
* @param response
*            HttpResponse响应
* @param charset
*            响应编码 默认GBK
* @param isGzip
*            是否gzip 默认false
* @return
* @throws Exception
*/
public static String parse(HttpResponse response, String charset,
boolean isGzip) {
HttpEntity entity = response.getEntity();
BufferedReader br = null;
StringBuilder sb = null;
String s = null;
try {
if (isGzip) {
try {
br = new BufferedReader(new InputStreamReader(
new GZIPInputStream(entity.getContent()), charset));
} catch (IOException e) {
System.out.println("gzip" + e.getLocalizedMessage());
br = new BufferedReader(new InputStreamReader(
entity.getContent(), charset));
}
} else {
br = new BufferedReader(new InputStreamReader(
entity.getContent(), charset));
}

sb = new StringBuilder();
String temp = null;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
s = sb.toString();
if (s.contains("Error: The requested URL could not be checked")) {
return null;
}
} catch (Exception e) {
s = sb.toString();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}

/** */
/**
* 获取Response内容字符集
*
* @param response
* @return
*/
public static String getContentCharset(HttpResponse response) {
String strs = "GBK";
Header header = response.getEntity().getContentType();
if (header != null) {
String s = header.getValue();
if (matcher(s, "(charset)\\s?=\\s?(utf-?8)")) {
strs = "utf-8";
} else if (matcher(s, "(charset)\\s?=\\s?(gbk)")) {
strs = "gbk";
} else if (matcher(s, "(charset)\\s?=\\s?(gb2312)")) {
strs = "gb2312";
}
}
return strs;
}

public static boolean getContentEncoding(HttpResponse response) {
Header header = response.getEntity().getContentEncoding();
boolean b = false;
if (header != null) {
String s = header.getValue();
if (s.toLowerCase().contains("gzip")) {
b = true;
}
}
return b;
}

/**
* 正则匹配
*
* @param s
* @param pattern
* @return
*/
public static boolean matcher(String s, String pattern) {
Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE
+ Pattern.UNICODE_CASE);
Matcher matcher = p.matcher(s);
if (matcher.find()) {
return true;
} else {
return false;
}
}

/**
* 空关闭
**/
public static boolean closeResponse(HttpResponse response) {
boolean b = false;
try {
response.getEntity().getContent().close();
b = true;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}

/**
* @param response
*            urlconnection响应
* @param charset
*            响应编码 默认UTF-8
* @return
*/
public static String parse(URLConnection conn, String charset) {
return parse(conn, charset, false);
}

/**
* 默认utf-8
*
* @param response
*            urlconnection响应
*/
public static String parse(URLConnection conn) {
return parse(conn, "UTF-8", false);
}

/**
* @param response
*            urlconnection响应
* @param charset
*            响应编码 默认utf-8
* @param isGzip
*            是否gzip 默认false
* @return
* @throws Exception
*/
public static String parse(URLConnection conn, String charset,
boolean isGzip) {
BufferedReader br = null;
StringBuilder sb = null;
try {
if (isGzip) {
br = new BufferedReader(new InputStreamReader(
new GZIPInputStream(conn.getInputStream()), charset));
} else {
br = new BufferedReader(new InputStreamReader(
conn.getInputStream(), charset));
}

sb = new StringBuilder();
String temp = null;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}


SendRequestPojo.java

package test;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;

public class SendRequestPojo {
/**
* 设置请求POST方式
*/
public static final int POST = 0;
/***
* 设置请求GET方式
*/
public static final int GET = 1;
public static final Integer socketTimeout = 20000;// 20秒
public static final Integer connectTimeout = 20000;// 20秒

private String url;
private Map<String, String> params;
/**
* 请求方式,get or post
*/
private int method;

private CHeader header;
/** 默认是uri请求 */
private boolean isUri = true;

private String xmlsOrJsonEntity;

private Builder builder = RequestConfig.custom()
.setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout); // httpClient
// 请求参数

private String cookieSpec;

/**
* 是否禁用代理
*/
// private boolean forbid = true;
private HttpRequestBase uriRequest;

public SendRequestPojo(String url) {
this.url = url;
this.method = GET;
}

public SendRequestPojo(String url, CHeader h) {
this.url = url;
this.header = h;
this.method = GET;
}

public SendRequestPojo(String url, CHeader header, boolean isUri) {
this.url = url;
this.method = GET;
this.header = header;
this.isUri = isUri;
}

public SendRequestPojo(String url, Map<String, String> params) {
this.url = url;
this.params = params;
this.method = POST;
}

public SendRequestPojo(String url, Map<String, String> params, CHeader h) {
this.url = url;
this.params = params;
this.header = h;
this.method = POST;
}

public SendRequestPojo(String url, Map<String, String> params,
CHeader header, boolean isUri) {
this.url = url;
this.params = params;
this.method = POST;
this.header = header;
this.isUri = isUri;
}

public String getUrl() {
if (url != null && url.contains("<html>") && url.length() > 1030) {
url = "";
}
return url;
}

public void setUrl(String url) {
this.url = url;
}

public void setParams(Map<String, String> params) {
this.params = params;
}

public int getMethod() {
if (getXmlsOrJsonEntity() != null) {
this.method = POST;
}
return method;
}

/**
* get请求or post请求 格式: Request.GET/Request.POST
*
* @param method
*/
public void setMethod(int method) {
this.method = method;
}

public CHeader getHeader() {
if (header == null) {
header = new CHeader();
}
return header;
}

public void setHeader(CHeader header) {
this.header = header;
}

public boolean isUri() {
return isUri;
}

public void setUri(boolean isUri) {
this.isUri = isUri;
}

public String getXmlsOrJsonEntity() {
return xmlsOrJsonEntity;
}

/**
* XML/json格式的post提交 </br> 需要通过CHeader 设置请求头信息</br> HttpPost post = new
* HttpPost(url);</br> StringEntity s = new StringEntity(xmls);</br>
* if(c.getRespCharset()!=null){</br> s.setContentEncoding("UTF-8");</br>
* }else{</br> s.setContentEncoding("GBK");</br> }</br>
* if(c.getContent_Type()!=null){</br>
* s.setContentType(c.getContent_Type());</br> }else{</br>
* s.setContentType("application/json");</br> }</br> post.setEntity(s);</br>
*
* @param xmls
*/
public void setXmlsOrJsonEntity(String xmlsOrJsonEntity) {
this.xmlsOrJsonEntity = xmlsOrJsonEntity;
}

/**
* 默认设置超时时间 如果修改请求参数可直接使用该方法set对应的参数
*
* @return builder对象
*/
public Builder getBuilder() {
if (getCookieSpec() != null) {
builder.setCookieSpec(getCookieSpec());
}
return builder;
}

/**
* 禁止重定向
*
* @return
*/
public Builder forbidRedirect() {
builder.setRedirectsEnabled(false);
return builder;
}

/**
* 默认毫秒
*
* @param socketTimeout
* @param connectTimeout
*/
public Builder setTimeOut(int socketTimeout, int connectTimeout) {
// setConnectionRequestTimeout(50)
builder.setSocketTimeout(socketTimeout).setConnectTimeout(
connectTimeout);
return builder;
}

public String getCookieSpec() {
return cookieSpec;
}

/**
* 设置cookie格式,默认CookieSpecs.BEST_MATCH
*
* @Title: setCookieSpec
* @Description: CookieSpecs.BROWSER_COMPATIBILITY
* @param cookieSpec
*/
public void setCookieSpec(String cookieSpec) {
this.cookieSpec = cookieSpec;
}

/**
* 返回request请求没有设置 RequestConfig
*
* @return
*/
public HttpRequestBase getHttpRequestBase() {
if (uriRequest == null) {
URI uri = null;
// 判断是uri请求还是url请求
if (isUri()) {
try {
URL u = new URL(this.url);
uri = new URI(u.getProtocol(), u.getHost(), u.getPath(),
u.getQuery(), null);
} catch (Exception e) {
e.printStackTrace();
}
}

if (getMethod() == GET) {
if (uri != null) {
uriRequest = new HttpGet(uri);
} else {
uriRequest = new HttpGet(this.url);
}
} else {
if (uri != null) {
uriRequest = new HttpPost(uri);
} else {
uriRequest = new HttpPost(this.url);
}
}
// 是否xml形式的post提交
if (getXmlsOrJsonEntity() != null) {
StringEntity s = null;
try {
s = new StringEntity(getXmlsOrJsonEntity());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (getHeader().getRespCharset() != null) {
s.setContentEncoding("UTF-8");
} else {
s.setContentEncoding("GBK");
}
if (getHeader().getContent_Type() != null) {
s.setContentType(getHeader().getContent_Type());
} else {
s.setContentType("application/json");
}
uriRequest.setHeader("Content-Type", s.getContentType()
.getValue());
putHeader(getHeader(), uriRequest);
((HttpPost) uriRequest).setEntity(s);
} else {
setParam(uriRequest, getHeader(), this.params);
}

}
return uriRequest;
}

/**
* 默认调用 getHttpRequestBase().abort();中止连接
*/
public void abort() {
this.getHttpRequestBase().abort();
}

public static void putHeader(CHeader h, HttpRequestBase base) {
// 网页头信息
base.setHeader("Accept", h.getAccept());
if (h.getReferer() != null) {
base.setHeader("Referer", h.getReferer());
}
if (h.getAccept_Encoding() != null) {
base.setHeader("Accept-Encoding", h.getAccept_Encoding());
}
if (h.getHost() != null) {
base.setHeader("Host", h.getHost());
}
if (h.getCookie() != null) {
base.setHeader("Cookie", h.getCookie());
}
if (h.getAccept_Language() != null) {
base.setHeader("Accept-Language", h.getAccept_Language());
}
if (h.getCache_Control() != null) {
base.setHeader("Cache-Control", h.getCache_Control());
}
if (h.getConnection() != null) {
base.setHeader("Connection", h.getConnection());
}
if (h.getContent_Type() != null) {
base.setHeader("Content-Type", h.getContent_Type());
}
if (h.getUser_Agent() != null) {
base.setHeader("User-Agent", h.getUser_Agent());
}

}

public static void setMap(Map<String, String> map, HttpPost base) {
if (map != null) {
List<NameValuePair> formParams = new ArrayList<NameValuePair>(); // 构建POST请求的表单参数
for (Map.Entry<String, String> entry : map.entrySet()) {
formParams.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
UrlEncodedFormEntity params = null;
try {
params = new UrlEncodedFormEntity(formParams, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
base.setEntity(params);
}
}

public static void setParam(HttpRequestBase base, CHeader header,
Map<String, String> map) {
if (header != null) {
putHeader(header, base);
}
if (map != null) {
setMap(map, (HttpPost) base);
}
}
}


4,测试类:Test.java

package test;

import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) {
ExecuteRequest cutil = new ExecuteRequest();
try {
String text = cutil.get("http://www.baidu.com");
System.out.println(text);
Map<String, String> dxMap = new HashMap<String, String>();
dxMap.put("checkbox", "false");
dxMap.put("fromFlag", "doorPage");
dxMap.put("icode", "");
dxMap.put("isHasV", "false");
dxMap.put("loginType", "myjsmcc");
dxMap.put("mobile", "15101254051");
dxMap.put("password", "809713");
CHeader c = new CHeader();
c.setReferer("https://gs.ac.10086.cn/login");
c.setHost("gs.ac.10086.cn");
SendRequestPojo pojo = new SendRequestPojo("https://gs.ac.10086.cn/popDoorPopLogonNew" , dxMap, c);
pojo.setTimeOut(40000,40000);
text = cutil.getResult(pojo);
if (text != null) {
if (text.contains("rcode:\"-7\"")) {

} else if (text.contains("rcode:'-1020'")) {

} else if (text.contains("rcode:\"1000\"")) {
c.setReferer("http://www.gs.10086.cn");
c.setHost("www.gs.10086.cn");
c.setRespCharset("UTF-8");
text = cutil.get("https://gs.ac.10086.cn/gsauth/ssoCookieJsonp?_1427203319699=", c);
dxMap.put("jsonParam", "[{\"dynamicURI\":\"/userCurrentFee\",\"dynamicParameter\":{\"method\":\"queryBalance\",\"reqHandle\":\"query\",\"busiNum\":\"CX_DQYE\"},\"dynamicDataNodeName\":\"API_balInfo\"}]");
c.setReferer("http://www.gs.10086.cn/my/myAccount.html");
c.setRespCharset("UTF-8");
text = cutil.post("http://www.gs.10086.cn/gs_obsh_service/actionDispatcher.do", c, dxMap);
if (text != null && text.contains("15101254051")) {
System.out.println(text);
}
}
}

} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ok");
}
}


测试结果:

地址:http://www.baidu.com响应时间:259
<!DOCTYPE html>百度页面</html>
地址:https://gs.ac.10086.cn/popDoorPopLogonNew响应时间:1503
地址:https://gs.ac.10086.cn/gsauth/ssoCookieJsonp?_1427203319699=响应时间:63
地址:http://www.gs.10086.cn/gs_obsh_service/actionDispatcher.do响应时间:791
{"API_balInfo":{"opInfo":null,"errorMessage":"","closeSmsReslutCode":"","busiErrCode":"","eventResults":[{"errorMessage":"","result":[],"busiErrCode":"","continue":true,"errorCode":"","resultCode":"9","show":true,"actionId":"","actionType":"1","showErrorMsg":""},{"errorMessage":"","result":[],"busiErrCode":"","continue":true,"errorCode":"","resultCode":"9","show":true,"actionId":"","actionType":"1","showErrorMsg":""},{"errorMessage":"","result":[],"busiErrCode":"","continue":true,"errorCode":"","resultCode":"9","show":true,"actionId":"","actionType":"1","showErrorMsg":""}],"clientCallbackInfo":null,"errorType":0,"resultCode":"1","errorCode":"","resultObj":{"specalMoneyFee":"0","qrySMIRet":[],"user":{"uid":"0","userCounty":0,"tel":"","score":"","yxfa":"999308","errorMsg":"","userType":"1","userState":"0","balance":"2937","loginSource":0,"mpoint":"","city_jbNum":"LZDQ","userName":"白红","realTimeTotalFee":"","userBrandNum":"5","channelId":"","custIcNo":"","brand_jbNum":"SZX","userApplyDate":"","feebPwdCheckFlg":"0","cookie":"LzvhVV2HD337LXZMMX1Kv8kZVJNTFCH040WXGRJlwQ3yhx79Qj99!-664121335!1427453447813","email":"","sessionId":"","contactName":"","city_busiNum":"","payType":"","cgmodelFlag":"","lateFee":"","mobile":"15101254051","enPwd":"86EA74C0D7451D","lastMonOweFee":"0","sumScoreCycle":0,"userAreaNum":"931","gprsState":"","contactAddr":"","userAreaName":"兰州地区","contactTel":"","brand_busiNum":"","loginType":"1","lastSsoActiveTime":0,"bossCode":"","mobilePwd":"A5AE9F92270A4479289B45E9BEC97BAA","feebPwdCheckRet":"","brand_jbNum_name":"神州行","custIcType":"","email_139":"","contactEmail":"","currNeedFee":"0","billRelevanceState":"","postCode":"","userCountyName":"","userEmailInfo":null,"primPwdTime":"1","ssoCookieStr":"","userSex":"1","errorCode":"","clState":"","openAccTime":"20140609124546"}},"showErrorMsg":""}}
ok
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: