1010import org .springframework .web .client .DefaultResponseErrorHandler ;
1111import org .springframework .web .client .ResponseErrorHandler ;
1212import org .springframework .web .client .RestTemplate ;
13+ import org .springframework .web .util .DefaultUriBuilderFactory ;
1314import org .springframework .web .util .UriComponentsBuilder ;
1415
1516import java .io .IOException ;
2223public class HttpClient {
2324
2425 public interface IHttpResponseHandler {
25- String toResponse (HttpClient client ,URI uri ,ResponseEntity <String > response );
26+ String toResponse (HttpClient client ,String uri ,ResponseEntity <String > response );
2627 }
2728
2829 private final RestTemplate restTemplate ;
@@ -40,7 +41,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers){
4041 }
4142
4243 @ Override
43- public String toResponse (HttpClient client , URI uri , ResponseEntity <String > response ) {
44+ public String toResponse (HttpClient client , String url , ResponseEntity <String > response ) {
4445 if (response .getStatusCode ().equals (HttpStatus .OK )){
4546 return response .getBody ();
4647 }
@@ -50,11 +51,12 @@ public String toResponse(HttpClient client, URI uri, ResponseEntity<String> resp
5051 }
5152
5253 if (response .getStatusCode ().equals (HttpStatus .FOUND )){
54+ URI uri = URI .create (url );
5355 HttpHeaders headers = response .getHeaders ();
5456 String location = Objects .requireNonNull (headers .getLocation ()).toString ();
5557 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 );
5860 }
5961 return response .getBody ();
6062 }
@@ -96,34 +98,35 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
9698 }
9799 this .restTemplate .setErrorHandler (defaultErrorHandler );
98100 this .restTemplate .setRequestFactory (requestFactory );
101+ DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory ();
102+ uriBuilderFactory .setEncodingMode (DefaultUriBuilderFactory .EncodingMode .NONE );
103+ this .restTemplate .setUriTemplateHandler (uriBuilderFactory );
99104 }
100105
101106 public String post (String url , HttpHeaders headers , JSON jsonObject ) {
102107 HttpEntity <String > httpEntity = new HttpEntity <>(jsonObject .toString (), headers );
103- UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
104- URI uri = uriComponentsBuilder .build ().toUri ();
105108 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 );
107110 }
108111
109-
110112 public String post (String url , HttpHeaders headers , MultiValueMap <String , String > formData ) {
111113 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 );
116116 }
117117
118118 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 ();
124119 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 );
127130 }
128131
129132
0 commit comments