17
17
18
18
package com .tencent .polaris .plugin .location .remotehttp ;
19
19
20
+ import java .io .BufferedReader ;
20
21
import java .io .IOException ;
22
+ import java .io .InputStreamReader ;
23
+ import java .net .HttpURLConnection ;
24
+ import java .net .URL ;
25
+ import java .util .concurrent .TimeUnit ;
21
26
22
27
import com .google .protobuf .StringValue ;
23
- import com .squareup .okhttp .Call ;
24
- import com .squareup .okhttp .OkHttpClient ;
25
- import com .squareup .okhttp .Request ;
26
- import com .squareup .okhttp .Response ;
27
28
import com .tencent .polaris .api .utils .StringUtils ;
28
29
import com .tencent .polaris .client .pb .ModelProto ;
29
30
import com .tencent .polaris .logging .LoggerFactory ;
@@ -37,8 +38,6 @@ public class RemoteHttpLocationProvider extends BaseLocationProvider<BaseLocatio
37
38
38
39
private static final Logger LOGGER = LoggerFactory .getLogger (RemoteHttpLocationProvider .class );
39
40
40
- private final OkHttpClient httpClient = new OkHttpClient ();
41
-
42
41
public RemoteHttpLocationProvider () {
43
42
super (GetOption .class );
44
43
}
@@ -67,31 +66,39 @@ public ModelProto.Location doGet(GetOption option) {
67
66
.build ();
68
67
}
69
68
70
- private String getResponse (final String url , String label ) {
71
- if (StringUtils .isEmpty (url )) {
69
+ private String getResponse (final String path , String label ) {
70
+ if (StringUtils .isEmpty (path )) {
72
71
LOGGER .warn ("[Location][Provider][RemoteHttp] get {} from remote url is empty" , label );
73
72
return "" ;
74
73
}
75
74
76
- Request request = new Request .Builder ()
77
- .get ()
78
- .url (url )
79
- .build ();
80
-
81
- Call call = httpClient .newCall (request );
82
-
75
+ HttpURLConnection conn = null ;
83
76
try {
84
- Response response = call .execute ();
85
- byte [] ret = response .body ().bytes ();
86
- if (response .code () != 200 ) {
87
- LOGGER .error ("[Location][Provider][RemoteHttp] get {} from remote {} fail: {}" , label , url , new String (ret ));
77
+ URL url = new java .net .URL (path );
78
+ conn = (HttpURLConnection ) url .openConnection ();
79
+
80
+ conn .setRequestMethod ("GET" );
81
+ conn .setConnectTimeout ((int ) TimeUnit .SECONDS .toMillis (2 ));// 连接超时
82
+ conn .setReadTimeout ((int ) TimeUnit .SECONDS .toMillis (2 ));// 读取超时
83
+ BufferedReader reader = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
84
+ StringBuffer buffer = new StringBuffer ();
85
+ String str ;
86
+ while ((str = reader .readLine ())!= null ){
87
+ buffer .append (str );
88
+ }
89
+ if (conn .getResponseCode () != 200 ) {
90
+ LOGGER .error ("[Location][Provider][RemoteHttp] get {} from remote {} fail: {}" , label , url , buffer );
88
91
return "" ;
89
92
}
90
- return new String ( ret );
93
+ return buffer . toString ( );
91
94
}
92
95
catch (IOException e ) {
93
- LOGGER .error ("[Location][Provider][RemoteHttp] get {} from remote {} fail : {}" , label , url , e . getMessage () );
96
+ LOGGER .error ("[Location][Provider][RemoteHttp] get {} from remote {} fail : {}" , label , path , e );
94
97
return "" ;
98
+ } finally {
99
+ if (null != conn ) {
100
+ conn .disconnect ();
101
+ }
95
102
}
96
103
}
97
104
}
0 commit comments