在写这个工具类的时候发现传入的参数太多,以至于方法泛滥,只一个send方法就有30多个,所以对工具类进行了优化,把输入参数封装在一个对象里,这样以后再扩展输入参数,直接修改这个类就ok了。
不多说了,先上代码:
/**
* 请求配置类
*
* @author arron
* @date 2016年2月2日 下午3:14:32
* @version 1.0
*/
public class HttpConfig {
private HttpConfig(){};
/**
* 获取实例
* @return
*/
public static HttpConfig custom(){
return new HttpConfig();
}
/**
* HttpClient对象
*/
private HttpClient client;
/**
* CloseableHttpAsyncClient对象
*/
private CloseableHttpAsyncClient asynclient;
/**
* 资源url
*/
private String url;
/**
* Header头信息
*/
private Header[] headers;
/**
* 请求方法
*/
private HttpMethods method=HttpMethods.GET;
/**
* 请求方法名称
*/
private String methodName;
/**
* 用于cookie操作
*/
private HttpContext context;
/**
* 传递参数
*/
private Map<String, Object> map;
/**
* 输入输出编码
*/
private String encoding=Charset.defaultCharset().displayName();
/**
* 输入编码
*/
private String inenc;
/**
* 输出编码
*/
private String outenc;
/**
* 输出流对象
*/
private OutputStream out;
/**
* 异步操作回调执行器
*/
private IHandler handler;
/**
* HttpClient对象
*/
public HttpConfig client(HttpClient client) {
this.client = client;
return this;
}
/**
* CloseableHttpAsyncClient对象
*/
public HttpConfig asynclient(CloseableHttpAsyncClient asynclient) {
this.asynclient = asynclient;
return this;
}
/**
* 资源url
*/
public HttpConfig url(String url) {
this.url = url;
return this;
}
/**
* Header头信息
*/
public HttpConfig headers(Header[] headers) {
this.headers = headers;
return this;
}
/**
* 请求方法
*/
public HttpConfig method(HttpMethods method) {
this.method = method;
return this;
}
/**
* 请求方法
*/
public HttpConfig methodName(String methodName) {
this.methodName = methodName;
return this;
}
/**
* cookie操作相关
*/
public HttpConfig context(HttpContext context) {
this.context = context;
return this;
}
/**
* 传递参数
*/
public HttpConfi