| | |
| | | package com.panzhihua.common.utlis; |
| | | |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.MalformedURLException; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | |
| | | import static com.panzhihua.common.utlis.wx.WXPayConstants.USER_AGENT; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | |
| | | |
| | | |
| | | /** |
| | | * http请求工具类,get请求 |
| | | * |
| | | * @param url |
| | | * @param params |
| | | * @param resonseCharSet |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String httpGet(String url, Map<String, Object> params,Integer code, String... resonseCharSet) throws Exception { |
| | | DefaultHttpClient defaultHttpClient = null; |
| | | BufferedReader bufferedReader = null; |
| | | try { |
| | | defaultHttpClient = new DefaultHttpClient(); |
| | | if (params != null) { |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | Iterator<String> iterator = params.keySet().iterator(); |
| | | String key; |
| | | while (iterator.hasNext()) { |
| | | key = iterator.next(); |
| | | Object val = params.get(key); |
| | | if (val instanceof List) { |
| | | List v = (List)val; |
| | | for (Object o : v) { |
| | | stringBuilder.append(key).append("=").append(o.toString()).append("&"); |
| | | } |
| | | } else { |
| | | stringBuilder.append(key).append("=").append(val.toString()).append("&"); |
| | | } |
| | | } |
| | | stringBuilder.deleteCharAt(stringBuilder.length() - 1); |
| | | url = url + "?" + stringBuilder.toString(); |
| | | log.info("url:{}", url); |
| | | } |
| | | HttpGet httpGet = new HttpGet(url); |
| | | httpGet.setHeader("Content-Type", "application/json;charset=ut-8"); |
| | | HttpResponse httpResponse = defaultHttpClient.execute(httpGet); |
| | | if (httpResponse.getStatusLine().getStatusCode() != code) { |
| | | String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode(); |
| | | log.info("errorLog:{}"+errorLog); |
| | | throw new Exception(url + errorLog); |
| | | } |
| | | // 读取返回信息 |
| | | String charSet = "utf-8"; |
| | | if (resonseCharSet != null && resonseCharSet.length > 0) |
| | | charSet = resonseCharSet[0]; |
| | | String output; |
| | | bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), charSet)); |
| | | |
| | | StringBuilder dataBuilder = new StringBuilder(); |
| | | while ((output = bufferedReader.readLine()) != null) { |
| | | dataBuilder.append(output); |
| | | } |
| | | log.info("InFoLog:{}"+dataBuilder.toString()); |
| | | return dataBuilder.toString(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } finally { |
| | | if (defaultHttpClient != null) |
| | | defaultHttpClient.getConnectionManager().shutdown(); |
| | | if (bufferedReader != null) |
| | | bufferedReader.close(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * http请求工具类,post请求 |
| | | * |
| | | * @param url |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | // HTTP GET请求 |
| | | public static String sendGet(String url) throws Exception { |
| | | URL obj = new URL(url); |
| | | HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
| | | //默认值GET |
| | | con.setRequestMethod("GET"); |
| | | //添加请求头 |
| | | con.setRequestProperty("User-Agent", USER_AGENT); |
| | | int responseCode = con.getResponseCode(); |
| | | log.info("发送 'GET' 请求到 URL:{}" + url); |
| | | log.info("Response Code:{}" + responseCode); |
| | | BufferedReader in = new BufferedReader( |
| | | new InputStreamReader(con.getInputStream())); |
| | | String inputLine; |
| | | StringBuffer response = new StringBuffer(); |
| | | while ((inputLine = in.readLine()) != null) { |
| | | response.append(inputLine); |
| | | } |
| | | in.close(); |
| | | //打印结果 |
| | | log.info("uu洗车返回:{}"+response.toString()); |
| | | return response.toString(); |
| | | } |
| | | |
| | | /** |
| | | * http请求工具类,post请求 |
| | | * |