11package com .datarangers .sdk .requests ;
22
33import com .datarangers .sdk .common .Constants ;
4+ import okhttp3 .*;
45
56import java .io .*;
67import java .net .HttpURLConnection ;
1516 * 一些http请求方法
1617 */
1718public class Requests {
19+ private static final Object LOCK = new Object ();
20+ private static volatile OkHttpClient okHttpClient ;
21+
22+ public static void init (OkHttpClient okHttpClient ) {
23+ if (Requests .okHttpClient != null ) {
24+ throw new RuntimeException ("ok httpclient has init" );
25+ }
26+ Requests .okHttpClient = okHttpClient ;
27+ }
28+
29+ private static OkHttpClient getHttpClient () {
30+ if (okHttpClient == null ) {
31+ synchronized (LOCK ) {
32+ if (okHttpClient == null ) {
33+ okHttpClient = new OkHttpClient ();
34+ }
35+ }
36+ }
37+ return okHttpClient ;
38+ }
39+
1840 public static String post (String url , Map <String , String > headers , Map <String , String > params , String body ) {
19- HttpURLConnection connection = null ;
2041 try {
2142 if (params != null ) {
2243 url += formatParams (params );
2344 }
24- URL urls = new URL (url );
25- connection = (HttpURLConnection ) urls .openConnection ();
26- //设置请求方式为POST
27- connection .setRequestMethod ("POST" );
28- for (Map .Entry <String , String > entry : headers .entrySet ()) {
29- connection .setRequestProperty (entry .getKey (), entry .getValue ());
30- }
31- //允许写出
32- connection .setDoOutput (true );
33- //允许读入
34- connection .setDoInput (true );
35- //不使用缓存
36- connection .setUseCaches (false );
37- connection .connect ();//连接
38- if (body != null ) {
39- BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (connection .getOutputStream (), "UTF-8" ));
40- writer .write (body );
41- writer .close ();
42- }
43- connection .getResponseCode ();
44- InputStream inputStream = connection .getInputStream ();
45- //将流转换为字符串
46- String result = convertStreamToString (inputStream );
47- return result ;
48- } catch (ProtocolException e ) {
49- e .printStackTrace ();
50- return e .toString ();
51- } catch (MalformedURLException e ) {
52- e .printStackTrace ();
53- return e .toString ();
45+ return requestMethod ("POST" , url , body , headers );
5446 } catch (IOException e ) {
5547 e .printStackTrace ();
5648 return e .toString ();
57- } finally {
58- if (connection != null ) {
59- connection .disconnect ();
60- }
6149 }
6250 }
6351
6452 public static String get (String url ) {
65- HttpURLConnection connection = null ;
6653 try {
67- URL urls = new URL (url );
68- connection = (HttpURLConnection ) urls .openConnection ();
69- connection .setRequestMethod ("GET" );
70- connection .connect ();
71- connection .getResponseCode ();
72- InputStream inputStream = connection .getInputStream ();
73- //将流转换为字符串
74- String result = convertStreamToString (inputStream );
75- return result ;
54+ return requestGet (url , new HashMap <>());
7655 } catch (Exception e ) {
7756 e .printStackTrace ();
7857 return e .toString ();
79- } finally {
80- if (connection != null ) {
81- connection .disconnect ();
82- }
8358 }
8459 }
8560
8661 public static String get (String url , Map <String , String > headers , Map <String , String > params ) {
87- HttpURLConnection connection = null ;
8862 try {
8963 if (params != null ) {
9064 url += formatParams (params );
9165 }
92- URL urls = new URL (url );
93- connection = (HttpURLConnection ) urls .openConnection ();
94- connection .setRequestMethod ("GET" );
95- for (Map .Entry <String , String > entry : headers .entrySet ()) {
96- connection .setRequestProperty (entry .getKey (), entry .getValue ());
97- }
98- connection .connect ();
99- connection .getResponseCode ();
100- InputStream inputStream = connection .getInputStream ();
101- //将流转换为字符串
102- String result = convertStreamToString (inputStream );
103- return result ;
66+ return requestGet (url , headers );
10467 } catch (Exception e ) {
10568 e .printStackTrace ();
10669 return e .toString ();
107- } finally {
108- if (connection != null ) {
109- connection .disconnect ();
110- }
11170 }
11271 }
11372
@@ -120,17 +79,6 @@ private static String formatParams(Map<String, String> params) {
12079 }
12180
12281
123- public static String convertStreamToString (InputStream inputStream ) throws IOException {
124- BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream , StandardCharsets .UTF_8 ));
125- StringBuilder sb = new StringBuilder ();
126- String line ;
127- while ((line = reader .readLine ()) != null ) {
128- sb .append (line + System .lineSeparator ());
129- }
130- String res = sb .toString ();
131- return res ;
132- }
133-
13482 public static String requests (String method , String url , Map <String , String > headers , String body , Map <String , String > params ) {
13583 if (Constants .POST .equals (method )) {
13684 return post (url , headers , params , body );
@@ -150,40 +98,44 @@ public static String requests(String method, String url, Map<String, String> hea
15098 * <LI>TRACE
15199 */
152100 private static String call (String url , Map <String , String > headers , Map <String , String > params , String body , String method ) {
153- HttpURLConnection connection = null ;
154101 try {
155102 if (params != null ) {
156103 url += formatParams (params );
157104 }
158- URL urls = new URL (url );
159- connection = (HttpURLConnection ) urls .openConnection ();
160- //设置请求方式为POST
161- connection .setRequestMethod (method );
162- headers .forEach (connection ::setRequestProperty );
163- //允许写出
164- connection .setDoOutput (true );
165- //允许读入
166- connection .setDoInput (true );
167- //不使用缓存
168- connection .setUseCaches (false );
169- //连接
170- connection .connect ();
171- if (body != null ) {
172- BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (connection .getOutputStream (), "UTF-8" ));
173- writer .write (body );
174- writer .close ();
175- }
176- connection .getResponseCode ();
177- InputStream inputStream = connection .getInputStream ();
178- //将流转换为字符串。
179- return convertStreamToString (inputStream );
105+ return requestMethod (method , url , body , headers );
180106 } catch (Exception e ) {
181107 e .printStackTrace ();
182108 return e .toString ();
183- } finally {
184- if (connection != null ) {
185- connection .disconnect ();
109+ }
110+ }
111+
112+ private static String requestGet (String url , Map <String , String > headers ) throws IOException {
113+ Request request = new Request .Builder ()
114+ .url (url )
115+ .headers (Headers .of (headers ))
116+ .build ();
117+ return getResponse (request );
118+ }
119+
120+ private static String requestMethod (String method , String url , String bodyContent ,
121+ Map <String , String > headers ) throws IOException {
122+ RequestBody body = RequestBody .create (MediaType .parse ("charset=utf-8" ), bodyContent );
123+ Request request = new Request .Builder ()
124+ .url (url )
125+ .headers (Headers .of (headers ))
126+ .method (method , body )
127+ .build ();
128+ return getResponse (request );
129+ }
130+
131+ private static String getResponse (Request request ) throws IOException {
132+ OkHttpClient client = getHttpClient ();
133+ try (Response response = client .newCall (request ).execute ()) {
134+ if (response == null ) {
135+ return null ;
186136 }
137+ return response .body () == null ? response .toString () : response .body ().string ();
187138 }
188139 }
140+
189141}
0 commit comments