@@ -31,7 +31,7 @@ public class HttpClient {
3131 /**
3232 * 默认 http 客户端
3333 */
34- private static final OkHttpClient CLIENT ;
34+ private static OkHttpClient CLIENT ;
3535 /**
3636 * 连接超时时间 单位秒(默认10s)
3737 */
@@ -63,15 +63,12 @@ public class HttpClient {
6363
6464 private static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8" ;
6565
66+ private static Dispatcher dispatcher = new Dispatcher ();
67+ private static ConnectionPool pool = new ConnectionPool (CONNECTION_POOL_MAX_IDLE_COUNT , CONNECTION_POOL_MAX_IDLE_MINUTES , TimeUnit .MINUTES );
6668
6769 static {
68- Dispatcher dispatcher = new Dispatcher ();
6970 dispatcher .setMaxRequests (DISPATCHER_MAX_REQUESTS );
7071 dispatcher .setMaxRequestsPerHost (DISPATCHER_MAX_REQUESTS_PER_HOST );
71-
72- ConnectionPool pool = new ConnectionPool (CONNECTION_POOL_MAX_IDLE_COUNT ,
73- CONNECTION_POOL_MAX_IDLE_MINUTES , TimeUnit .MINUTES );
74-
7572 CLIENT = new OkHttpClient .Builder ()
7673 .callTimeout (CALL_TIMEOUT , TimeUnit .SECONDS )
7774 .connectTimeout (CONNECT_TIMEOUT , TimeUnit .SECONDS )
@@ -81,13 +78,26 @@ public class HttpClient {
8178 .build ();
8279 }
8380
81+ public HttpClient () {
82+ }
83+
84+ public HttpClient (int timeout ) {
85+ CLIENT = new OkHttpClient .Builder ()
86+ .callTimeout (timeout , TimeUnit .SECONDS )
87+ .connectTimeout (timeout , TimeUnit .SECONDS )
88+ .readTimeout (timeout * 3 , TimeUnit .SECONDS )
89+ .dispatcher (dispatcher )
90+ .connectionPool (pool )
91+ .build ();
92+ }
93+
8494 /**
8595 * Get 请求 无参数,无请求头
8696 *
8797 * @param url 地址
8898 * @return 响应
8999 */
90- public static Response get (String url ) {
100+ public Response get (String url ) {
91101 return get (url , null , null );
92102 }
93103
@@ -98,7 +108,7 @@ public static Response get(String url) {
98108 * @param header
99109 * @return
100110 */
101- public static Response get (String url , StringMap header ) {
111+ public Response get (String url , StringMap header ) {
102112 return get (url , null , header );
103113 }
104114
@@ -110,7 +120,7 @@ public static Response get(String url, StringMap header) {
110120 * @param header 请求头 map
111121 * @return 响应
112122 */
113- public static Response get (String url , @ Nullable StringMap params , StringMap header ) {
123+ public Response get (String url , @ Nullable StringMap params , StringMap header ) {
114124 if (params != null ) {
115125 String urlParams = StringUtils .parseUrlParams (params );
116126 url = url + "?" + urlParams ;
@@ -127,7 +137,7 @@ public static Response get(String url, @Nullable StringMap params, StringMap hea
127137 * @param headers 请求头 map
128138 * @return 响应
129139 */
130- public static Response postForm (String url , StringMap params , StringMap headers ) {
140+ public Response postForm (String url , StringMap params , StringMap headers ) {
131141 final FormBody .Builder fb = new FormBody .Builder ();
132142 for (Map .Entry <String , Object > entry : params .entrySet ()) {
133143 fb .add (entry .getKey (), entry .getValue ().toString ());
@@ -138,14 +148,14 @@ public static Response postForm(String url, StringMap params, StringMap headers)
138148 /**
139149 * 请求体为 字符串, 默认媒体类型-JSON
140150 */
141- public static Response post (String url , String body , StringMap header ) {
151+ public Response post (String url , String body , StringMap header ) {
142152 return post (url , StringUtils .utf8Bytes (body ), header , JSON_MIME );
143153 }
144154
145155 /**
146156 * 复杂Map(包含字节数组)对象 以 json 格式请求,
147157 */
148- public static Response postComplex (String url , StringMap params , StringMap header ) {
158+ public Response postComplex (String url , StringMap params , StringMap header ) {
149159 Gson gson = new GsonBuilder ().enableComplexMapKeySerialization ().create ();
150160 RequestBody requestBody = RequestBody .create (MediaType .get (APPLICATION_JSON_UTF8_VALUE ), gson .toJson (params .map ()));
151161 return post (url , requestBody , header );
@@ -154,26 +164,26 @@ public static Response postComplex(String url, StringMap params, StringMap heade
154164 /**
155165 * 请求体为 字节数组,默认媒体类型-JSON
156166 */
157- public static Response post (String url , byte [] body , StringMap header ) {
167+ public Response post (String url , byte [] body , StringMap header ) {
158168 return post (url , body , header , JSON_MIME );
159169 }
160170
161171 /**
162172 * 请求体为 字节数组,指定 媒体类型
163173 */
164- public static Response post (String url , byte [] body , StringMap header , String contentType ) {
174+ public Response post (String url , byte [] body , StringMap header , String contentType ) {
165175 RequestBody requestBody = RequestBody .create (MediaType .parse (contentType ), body );
166176 return post (url , requestBody , header );
167177 }
168178
169179 /**
170180 * 批量文件上传
171181 */
172- public static Response multipartPost (String url ,
173- StringMap fields ,
174- String name ,
175- File [] files ,
176- StringMap headers ) {
182+ public Response multipartPost (String url ,
183+ StringMap fields ,
184+ String name ,
185+ File [] files ,
186+ StringMap headers ) {
177187 final MultipartBody .Builder mb = new MultipartBody .Builder ();
178188 for (File file : files ) {
179189 RequestBody fileBody = RequestBody .create (MediaType .parse (Constants .MULTIPART_MIME ), file );
@@ -193,12 +203,12 @@ public static Response multipartPost(String url,
193203 /**
194204 * 文件上传 文件体为 file
195205 */
196- public static Response multipartPost (String url ,
197- StringMap fields ,
198- String name ,
199- String fileName ,
200- File fileBody ,
201- StringMap headers ) {
206+ public Response multipartPost (String url ,
207+ StringMap fields ,
208+ String name ,
209+ String fileName ,
210+ File fileBody ,
211+ StringMap headers ) {
202212 RequestBody file = RequestBody .create (MediaType .parse (Constants .MULTIPART_MIME ), fileBody );
203213 Request .Builder requestBuilder = getBuilder (url , fields , name , fileName , file );
204214 return send (requestBuilder , headers );
@@ -216,7 +226,7 @@ public static Response multipartPost(String url,
216226 * @param contentType 请求体类型
217227 * @param cb 异步回调
218228 */
219- public static void asyncPost (String url , byte [] body , int offset , int size , StringMap header , String contentType , AsyncCallback cb ) {
229+ public void asyncPost (String url , byte [] body , int offset , int size , StringMap header , String contentType , AsyncCallback cb ) {
220230 RequestBody requestBody = RequestBody .create (MediaType .parse (contentType ), body , offset , size );
221231 Request .Builder requestBuilder = new Request .Builder ().url (url ).post (requestBody );
222232 asyncSend (requestBuilder , header , cb );
@@ -225,27 +235,27 @@ public static void asyncPost(String url, byte[] body, int offset, int size, Stri
225235 /**
226236 * 异步文件上传 文件体为 字节数组
227237 */
228- public static void asyncMultipartPost (String url ,
229- StringMap fields ,
230- String name ,
231- String fileName ,
232- byte [] fileBody ,
233- StringMap headers ,
234- AsyncCallback cb ) {
238+ public void asyncMultipartPost (String url ,
239+ StringMap fields ,
240+ String name ,
241+ String fileName ,
242+ byte [] fileBody ,
243+ StringMap headers ,
244+ AsyncCallback cb ) {
235245 RequestBody file = RequestBody .create (MediaType .parse (Constants .MULTIPART_MIME ), fileBody );
236246 asyncMultipartPost (url , fields , name , fileName , file , headers , cb );
237247 }
238248
239249 /**
240250 * 异步文件上传 文件体为 file
241251 */
242- public static void asyncMultipartPost (String url ,
243- StringMap fields ,
244- String name ,
245- String fileName ,
246- File fileBody ,
247- StringMap headers ,
248- AsyncCallback cb ) {
252+ public void asyncMultipartPost (String url ,
253+ StringMap fields ,
254+ String name ,
255+ String fileName ,
256+ File fileBody ,
257+ StringMap headers ,
258+ AsyncCallback cb ) {
249259 RequestBody file = RequestBody .create (MediaType .parse (Constants .MULTIPART_MIME ), fileBody );
250260 asyncMultipartPost (url , fields , name , fileName , file , headers , cb );
251261 }
0 commit comments