| | |
| | | package com.ycl.utils.http; |
| | | |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.apache.http.client.HttpClient; |
| | | import org.apache.http.client.config.RequestConfig; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.Collections; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @author xp |
| | |
| | | @RequiredArgsConstructor |
| | | public class SelfHttpUtil { |
| | | |
| | | private final RestTemplate httpClient; |
| | | private final RestTemplate restTemplate; |
| | | |
| | | |
| | | /** |
| | | * post |
| | |
| | | * @return |
| | | */ |
| | | public Object post(String url, @Nullable Object data, @Nullable MultiValueMap header) { |
| | | ResponseEntity<Object> response = httpClient.exchange( |
| | | ResponseEntity<Object> response = restTemplate.exchange( |
| | | url, |
| | | HttpMethod.POST, |
| | | getHttpEntity(data, header), |
| | |
| | | * @return |
| | | */ |
| | | public Object post(String url, String token, @Nullable Object data) { |
| | | ResponseEntity<Object> response = httpClient.exchange( |
| | | ResponseEntity<Object> response = restTemplate.exchange( |
| | | url, |
| | | HttpMethod.POST, |
| | | getHttpEntity(data, token), |
| | |
| | | * @param params |
| | | * @return |
| | | */ |
| | | public ResponseEntity<Object> get(String url, @Nullable MultiValueMap header, @Nullable Object... params) { |
| | | ResponseEntity<Object> response = httpClient.exchange( |
| | | public ResponseEntity<String> get(String url, @Nullable MultiValueMap header, @Nullable Map<String, Object> params) { |
| | | if (params == null) { |
| | | params = Collections.emptyMap(); |
| | | } |
| | | ResponseEntity<String> response = restTemplate.exchange( |
| | | url, |
| | | HttpMethod.GET, |
| | | getHttpEntity(null, header), |
| | | Object.class, |
| | | String.class, |
| | | params |
| | | ); |
| | | return response; |