HttpUtils.java 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
package cn.ibizlab.util.helper;

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

/**
 * 请求工具类
 */
public class HttpUtils {

    private final static String DEFAULT_ENCODING = "UTF-8";
    /**
     * Post请求
     *
     * @return
     */
    public static String post(String url) {
        return post(url,null,null);
    }

    /**
     *Post请求
     * @param
     * @return
     */
    public static String post(String url,JSONObject headerMap,JSONObject paramMap) {
        return doRestRequest(url,HttpMethod.POST,headerMap,paramMap);
    }

    /**
     *put请求
     * @param
     * @return
     */
    public static String put(String url, JSONObject headerMap,JSONObject paramMap) {
        return doRestRequest(url,HttpMethod.PUT,headerMap,paramMap);
    }

    /**
     * GET请求
     *
     * @param
     * @return
     */
    public static String get(String url) throws UnsupportedEncodingException {
       return get(url,null,null);
    }

    /**
     * GET请求
     *
     * @param
     * @param
     * @return
     */
    public static String get(String url, JSONObject headerMap,JSONObject paramMap) throws UnsupportedEncodingException {
        if(paramMap !=null){
            boolean flag = true;
            for(String key : paramMap.keySet()){
                if(flag){
                    url += "?";
                }else{
                    url += "&";
                }
                flag = false;
                url = url + key + "=" + URLEncoder.encode(paramMap.getString(key),DEFAULT_ENCODING);
            }
        }
        return doRestRequest(url,HttpMethod.GET,headerMap,paramMap);
    }

    /**
     * DELETE请求
     *
     * @param
     * @return
     */
    public static String delete(String url) {
       return delete(url,null,null);
    }

    /**
     * DELETE请求
     *
     * @param
     * @param
     * @return
     */
    public static String delete(String url, JSONObject headerMap,JSONObject paramMap) {
      return doRestRequest(url,HttpMethod.DELETE,headerMap,paramMap);
    }

    public static String doRestRequest(String url, HttpMethod method, JSONObject headerMap, JSONObject paramMap){
        RestTemplate restTemplate = getRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        if(headerMap!=null){
            for(String key : headerMap.keySet()){
                headers.add(key,headerMap.getString(key));
            }
        }
        MultiValueMap<String,String> params = new LinkedMultiValueMap<String,String>();
        HttpEntity<String> entity;
        if(paramMap!=null){
            entity = new HttpEntity<>(paramMap.toString(), headers);
        }else{
            entity = new HttpEntity<>(null, headers);
        }
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, entity, String.class);
        return responseEntity.getBody();
    }

    public static InputStream requestInputStream(String url, JSONObject headerMap, JSONObject paramMap) throws IOException {
        RestTemplate restTemplate = getRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        if(headerMap!=null){
            for(String key : headerMap.keySet()){
                headers.add(key,headerMap.getString(key));
            }
        }
        MultiValueMap<String,String> params = new LinkedMultiValueMap<String,String>();
        HttpEntity<String> entity;
        if(paramMap!=null){
            entity = new HttpEntity<>(paramMap.toString(), headers);
        }else{
            entity = new HttpEntity<>(null, headers);
        }
        ResponseEntity<Resource> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, Resource.class);
        return responseEntity.getBody().getInputStream();
    }

    public static RestTemplate getRestTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        for (HttpMessageConverter<?> httpMessageConverter : restTemplate.getMessageConverters()) {
            if (httpMessageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8"));
                break;
            }
        }
        return restTemplate;
    }
}