10
10
import org .springframework .web .client .DefaultResponseErrorHandler ;
11
11
import org .springframework .web .client .ResponseErrorHandler ;
12
12
import org .springframework .web .client .RestTemplate ;
13
+ import org .springframework .web .util .DefaultUriBuilderFactory ;
13
14
import org .springframework .web .util .UriComponentsBuilder ;
14
15
15
16
import java .io .IOException ;
22
23
public class HttpClient {
23
24
24
25
public interface IHttpResponseHandler {
25
- String toResponse (HttpClient client ,URI uri ,ResponseEntity <String > response );
26
+ String toResponse (HttpClient client ,String uri ,ResponseEntity <String > response );
26
27
}
27
28
28
29
private final RestTemplate restTemplate ;
@@ -40,7 +41,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers){
40
41
}
41
42
42
43
@ Override
43
- public String toResponse (HttpClient client , URI uri , ResponseEntity <String > response ) {
44
+ public String toResponse (HttpClient client , String url , ResponseEntity <String > response ) {
44
45
if (response .getStatusCode ().equals (HttpStatus .OK )){
45
46
return response .getBody ();
46
47
}
@@ -50,11 +51,12 @@ public String toResponse(HttpClient client, URI uri, ResponseEntity<String> resp
50
51
}
51
52
52
53
if (response .getStatusCode ().equals (HttpStatus .FOUND )){
54
+ URI uri = URI .create (url );
53
55
HttpHeaders headers = response .getHeaders ();
54
56
String location = Objects .requireNonNull (headers .getLocation ()).toString ();
55
57
String baseUrl = uri .getScheme () + "://" + uri .getHost ()+":" +uri .getPort ();
56
- String url = baseUrl +location ;
57
- return client .get (url ,copyHeaders (headers ),null );
58
+ String locationUrl = baseUrl +location ;
59
+ return client .get (locationUrl ,copyHeaders (headers ),null );
58
60
}
59
61
return response .getBody ();
60
62
}
@@ -96,34 +98,35 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
96
98
}
97
99
this .restTemplate .setErrorHandler (defaultErrorHandler );
98
100
this .restTemplate .setRequestFactory (requestFactory );
101
+ DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory ();
102
+ uriBuilderFactory .setEncodingMode (DefaultUriBuilderFactory .EncodingMode .NONE );
103
+ this .restTemplate .setUriTemplateHandler (uriBuilderFactory );
99
104
}
100
105
101
106
public String post (String url , HttpHeaders headers , JSON jsonObject ) {
102
107
HttpEntity <String > httpEntity = new HttpEntity <>(jsonObject .toString (), headers );
103
- UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
104
- URI uri = uriComponentsBuilder .build ().toUri ();
105
108
ResponseEntity <String > httpResponse = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
106
- return responseHandler .toResponse (this ,uri ,httpResponse );
109
+ return responseHandler .toResponse (this ,url ,httpResponse );
107
110
}
108
111
109
-
110
112
public String post (String url , HttpHeaders headers , MultiValueMap <String , String > formData ) {
111
113
HttpEntity <MultiValueMap <String , String >> httpEntity = new HttpEntity <>(formData , headers );
112
- UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
113
- URI uri = uriComponentsBuilder .build ().toUri ();
114
- ResponseEntity <String > httpResponse = restTemplate .exchange (uri , HttpMethod .POST , httpEntity , String .class );
115
- return responseHandler .toResponse (this ,uri ,httpResponse );
114
+ ResponseEntity <String > httpResponse = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
115
+ return responseHandler .toResponse (this ,url ,httpResponse );
116
116
}
117
117
118
118
public String get (String url , HttpHeaders headers , MultiValueMap <String , String > uriVariables ) {
119
- UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
120
- if (uriVariables != null ) {
121
- uriComponentsBuilder = uriComponentsBuilder .queryParams (uriVariables );
122
- }
123
- URI uri = uriComponentsBuilder .build ().toUri ();
124
119
HttpEntity <String > httpEntity = new HttpEntity <>(headers );
125
- ResponseEntity <String > httpResponse = restTemplate .exchange (uri , HttpMethod .GET , httpEntity , String .class );
126
- return responseHandler .toResponse (this ,uri ,httpResponse );
120
+ ResponseEntity <String > httpResponse ;
121
+ if (uriVariables !=null &&!uriVariables .isEmpty ()) {
122
+ URI uri = UriComponentsBuilder .fromHttpUrl (url )
123
+ .queryParams (uriVariables )
124
+ .build (true ).toUri ();
125
+ httpResponse = restTemplate .exchange (uri , HttpMethod .GET , httpEntity , String .class );
126
+ }else {
127
+ httpResponse = restTemplate .exchange (url , HttpMethod .GET , httpEntity , String .class );
128
+ }
129
+ return responseHandler .toResponse (this , url , httpResponse );
127
130
}
128
131
129
132
0 commit comments