首页 通用示例代码 HttpUtil.java示例代码[Java]
    • HttpUtil.java示例代码

      [Java]
    • Java HTTP请求通用函数
    • 作者:Jonathan 阅读数:1737 上传时间:2020-11-21
	            package com.liupai.test; 
                import java.util.HashMap;
                import java.util.Iterator;
                import java.util.Map;
                import java.util.Set;
                
                import org.springframework.http.HttpEntity;
                import org.springframework.http.HttpHeaders;
                import org.springframework.http.HttpMethod;
                import org.springframework.http.MediaType;
                import org.springframework.http.ResponseEntity;
                import org.springframework.http.client.SimpleClientHttpRequestFactory;
                import org.springframework.util.LinkedMultiValueMap;
                import org.springframework.util.MultiValueMap;
                import org.springframework.web.client.RestTemplate;
                
                import com.alibaba.fastjson.JSONObject;
                
                public class TEST{
                       /**
                       * 请求接口
                       * @param method 请求方法
                       * @param url 请求地址
                       * @param headers 请求头部
                       * @param params 请求参数
                       * @return
                       */
                        public static String proxyToDesURL(String method, String url, Map<String, String> headers,
                            Map<String, String> params) {
                        // TODO Auto-generated method stub
                        try {
                            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
                            RestTemplate restTemplate = new RestTemplate(requestFactory);
                            //处理请求头部
                            HttpHeaders requestHeaders = new HttpHeaders();
                            if (headers != null && !headers.isEmpty()) {
                                Set&lt;String&gt; set = headers.keySet();
                                for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
                                    String key = iterator.next();
                                    String value = headers.get(key);
                                    requestHeaders.add(key, value);
                                }
                            }
                            //处理请求参数
                            MultiValueMap<String, String> paramList = new LinkedMultiValueMap<String, String>();
                            if (params != null && !params.isEmpty()) {
                                if(method.equalsIgnoreCase("GET"))
                                {
                                    url += "?";
                                    Set<String> set = params.keySet();
                                    for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
                                        String key = iterator.next();
                                        String value = params.get(key);
                                        url += key+"="+value+"&amp;";
                                    }
                                    url = url.substring(0, url.length() - 1);
                                }
                                else
                                {
                                    Set<String> set = params.keySet();
                                    for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
                                        String key = iterator.next();
                                        String value = params.get(key);
                                        paramList.add(key, value);
                                    }
                                }
                            }
                            requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
                            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                                    paramList, requestHeaders);
                            //处理请求方法
                            HttpMethod requestType = HttpMethod.GET;
                            method = method.toUpperCase();
                            switch (method) {
                            case "GET":
                                requestType = HttpMethod.GET;
                                break;
                            case "POST":
                                requestType = HttpMethod.POST;
                                break;
                            case "PUT":
                                requestType = HttpMethod.PUT;
                                break;
                            case "DELETE":
                                requestType = HttpMethod.DELETE;
                                break;
                            case "HEAD":
                                requestType = HttpMethod.HEAD;
                                break;
                            case "OPTIONS":
                                requestType = HttpMethod.OPTIONS;
                                break;
                            default:
                                requestType = HttpMethod.GET;
                                break;
                            }
                            ResponseEntity<String> responseEntity = restTemplate.exchange(url, requestType, requestEntity,
                                    String.class, params);
                            //获取返回结果
                            return responseEntity.getBody();
                        } catch (Exception e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }
                        return null;
                     
                }