View Javadoc
1   package com.foxinmy.weixin4j.http;
2   
3   import java.net.Proxy;
4   
5   import javax.net.ssl.HostnameVerifier;
6   import javax.net.ssl.SSLContext;
7   
8   /**
9    * Http请求参数
10   *
11   * @className HttpParams
12   * @author jinyu(foxinmy@gmail.com)
13   * @date 2015年8月13日
14   * @since JDK 1.6
15   */
16  public final class HttpParams {
17  	/**
18  	 * 连接超时时间(单位毫秒)
19  	 */
20  	private final int connectTimeout;
21  	/**
22  	 * 读取超时时间(单位毫秒)
23  	 */
24  	private final int readTimeout;
25  	/**
26  	 * 最大连接数
27  	 */
28  	private final int maxConnections;
29  	/**
30  	 * 每个host最大连接数
31  	 */
32  	private final int maxConnectionsPerHost;
33  	/**
34  	 * 代理对象
35  	 */
36  	private Proxy proxy;
37  	/**
38  	 * SSL对象
39  	 */
40  	private SSLContext sslContext;
41  	/**
42  	 * hostname对象
43  	 */
44  	private HostnameVerifier hostnameVerifier;
45  
46  	/**
47  	 * connectTimeout = 15000,readTimeout=20000,maxConnection=100,maxConnectionPerHost=32
48  	 */
49  	public HttpParams() {
50  		this(5000, 15000,100,32);
51  	}
52  
53  	public HttpParams(int connectTimeout, int readTimeout,int maxConnections,int maxConnectionsPerHost) {
54  		this.connectTimeout = connectTimeout;
55  		this.readTimeout = readTimeout;
56  		this.maxConnections = maxConnections;
57  		this.maxConnectionsPerHost = maxConnectionsPerHost;
58  	}
59  
60  	public int getConnectTimeout() {
61  		return connectTimeout;
62  	}
63  
64  	public int getReadTimeout() {
65  		return readTimeout;
66  	}
67  
68  	public int getMaxConnections() {
69  		return maxConnections;
70  	}
71  
72  	public int getMaxConnectionsPerHost() {
73  		return maxConnectionsPerHost;
74  	}
75  
76  	public Proxy getProxy() {
77  		return proxy;
78  	}
79  
80  	public HttpParams setProxy(Proxy proxy) {
81  		this.proxy = proxy;
82  		return this;
83  	}
84  
85  	public SSLContext getSSLContext() {
86  		return sslContext;
87  	}
88  
89  	public HttpParams setSSLContext(SSLContext sslContext) {
90  		this.sslContext = sslContext;
91  		return this;
92  	}
93  
94  	public HostnameVerifier getHostnameVerifier() {
95  		return hostnameVerifier;
96  	}
97  
98  	public HttpParams setHostnameVerifier(HostnameVerifier hostnameVerifier) {
99  		this.hostnameVerifier = hostnameVerifier;
100 		return this;
101 	}
102 
103 	public static HttpParams copy(final HttpParams params) {
104 		return new HttpParams(params.getConnectTimeout(),
105 				params.getReadTimeout(),params.getMaxConnections(),params.getMaxConnectionsPerHost()).setProxy(params.getProxy())
106 				.setHostnameVerifier(params.getHostnameVerifier())
107 				.setSSLContext(params.getSSLContext());
108 	}
109 
110 	@Override
111 	public String toString() {
112 		return "HttpParams [connectTimeout=" + connectTimeout
113 				+ ", readTimeout=" + readTimeout + ", proxy=" + proxy
114 				+ ", sslContext=" + sslContext + ", hostnameVerifier="
115 				+ hostnameVerifier + "]";
116 	}
117 }