|
| 1 | +package com.codingapi.springboot.framework.rest; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.codingapi.springboot.framework.rest.properties.RestApiProperties; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.http.*; |
| 7 | +import org.springframework.http.client.SimpleClientHttpRequestFactory; |
| 8 | +import org.springframework.util.MultiValueMap; |
| 9 | +import org.springframework.web.client.RestTemplate; |
| 10 | +import org.springframework.web.util.UriComponentsBuilder; |
| 11 | + |
| 12 | +import java.net.InetSocketAddress; |
| 13 | +import java.net.Proxy; |
| 14 | +import java.net.URI; |
| 15 | + |
| 16 | +@Slf4j |
| 17 | +class HttpClient { |
| 18 | + |
| 19 | + private final RestTemplate restTemplate; |
| 20 | + |
| 21 | + private final String baseUrl; |
| 22 | + |
| 23 | + public HttpClient(RestApiProperties properties, String baseUrl) { |
| 24 | + this.baseUrl = baseUrl; |
| 25 | + this.restTemplate = RestTemplateContext.getInstance().getRestTemplate(); |
| 26 | + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); |
| 27 | + requestFactory.setConnectTimeout(3000); |
| 28 | + if(properties!=null) { |
| 29 | + if (properties.isEnableProxy()) { |
| 30 | + log.info("enable proxy {}//:{}:{}", properties.getProxyType(), properties.getProxyHost(), properties.getProxyPort()); |
| 31 | + requestFactory.setProxy(new Proxy(properties.getProxyType(), |
| 32 | + new InetSocketAddress(properties.getProxyHost(), properties.getProxyPort()))); |
| 33 | + } |
| 34 | + } |
| 35 | + this.restTemplate.setRequestFactory(requestFactory); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + private String buildUrl(String api) { |
| 40 | + return baseUrl+api; |
| 41 | + } |
| 42 | + |
| 43 | + public String post(String api, JSON jsonObject) { |
| 44 | + HttpHeaders headers = new HttpHeaders(); |
| 45 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 46 | + String url = buildUrl(api); |
| 47 | + HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(), headers); |
| 48 | + ResponseEntity<String> httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); |
| 49 | + return httpResponse.getBody(); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public String get(String api, MultiValueMap<String, String> uriVariables) { |
| 54 | + HttpHeaders headers = new HttpHeaders(); |
| 55 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 56 | + String url = buildUrl(api); |
| 57 | + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url); |
| 58 | + if(uriVariables!=null){ |
| 59 | + uriComponentsBuilder = uriComponentsBuilder.queryParams(uriVariables); |
| 60 | + } |
| 61 | + URI uri = uriComponentsBuilder.build().toUri(); |
| 62 | + HttpEntity<String> httpEntity = new HttpEntity<>(headers); |
| 63 | + ResponseEntity<String> httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class); |
| 64 | + return httpResponse.getBody(); |
| 65 | + } |
| 66 | +} |
0 commit comments