| | |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | |
| | | return headers; |
| | | } |
| | | |
| | | private static HttpHeaders get() { |
| | | private static HttpHeaders getMsg() { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.set("accept", "*/*"); |
| | | headers.set("connection", "Keep-Alive"); |
| | | headers.set("user-agent", |
| | | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| | | List<MediaType> mediaTypes = new ArrayList<>(); |
| | | mediaTypes.add(MediaType.ALL); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // 表单格式 |
| | | headers.setAccept(mediaTypes); |
| | | headers.setConnection("Keep-Alive"); |
| | | headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| | | return headers; |
| | | } |
| | | |
| | | /** |
| | | * 短信 |
| | | * http返回String xml |
| | | */ |
| | | public static String exchangeMsg(HttpMethod httpMethod, String url, MultiValueMap<String, String> map) { |
| | | HttpHeaders headers = getMsg(); |
| | | HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers); |
| | | RestTemplate restTemplate = new RestTemplate(getFactory()); |
| | | return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody(); |
| | | } |
| | | |
| | | /** |
| | | * http返回String |
| | | */ |
| | | public static String exchangeString(HttpMethod httpMethod, String url, Map<String, Object> map) { |
| | | HttpHeaders headers = get(); |
| | | HttpHeaders headers = getHeaders(); |
| | | HttpEntity<Object> httpEntity = new HttpEntity<>(map, headers); |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | return restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody(); |