|  |  | 
 |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
 |  |  |  | 
 |  |  | import java.io.*; | 
 |  |  | import java.net.HttpURLConnection; | 
 |  |  | import java.net.URL; | 
 |  |  | import java.net.URLEncoder; | 
 |  |  | import java.nio.charset.Charset; | 
 |  |  | import java.nio.charset.StandardCharsets; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | 
 |  |  |             httpMethod.setRequestEntity(entity); | 
 |  |  |  | 
 |  |  |             int resultCode = httpClient.executeMethod(httpMethod); | 
 |  |  |             // 获取响应头的字符编码 | 
 |  |  |             String contentType = httpMethod.getResponseHeader("Content-Type") == null ? | 
 |  |  |                     null : httpMethod.getResponseHeader("Content-Type").getValue(); | 
 |  |  |             String charset = "UTF-8"; // 默认使用UTF-8 | 
 |  |  |  | 
 |  |  |             if (contentType != null && contentType.contains("charset=")) { | 
 |  |  |                 String[] parts = contentType.split("charset="); | 
 |  |  |                 if (parts.length > 1) { | 
 |  |  |                     charset = parts[1].split(";")[0].trim(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             InputStream inputStream = httpMethod.getResponseBodyAsStream(); | 
 |  |  |             if (inputStream == null) { | 
 |  |  |                 throw new HttpRequestException(RESPONSE_NULL_ERROR_CODE, "响应为null"); | 
 |  |  |             } | 
 |  |  |             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | 
 |  |  |             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,charset)); | 
 |  |  |             StringBuffer stringBuffer = new StringBuffer(); | 
 |  |  |             String str = ""; | 
 |  |  |             while ((str = reader.readLine()) != null) { | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static String postFormRequest(String url, Map<String, String> params) throws HttpRequestException { | 
 |  |  |         Assert.hasLength(url, "请求url不能为空字符串。"); | 
 |  |  |         Assert.notNull(params, "请求params不能为空。"); | 
 |  |  |  | 
 |  |  |         PostMethod httpMethod = new PostMethod(url); | 
 |  |  |  | 
 |  |  |         httpMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); | 
 |  |  |     public static String postFormRequest(String baseUrl, Map<String, String> params, Map<String, String> headers, String body) { | 
 |  |  |         HttpURLConnection connection = null; | 
 |  |  |         BufferedReader reader = null; | 
 |  |  |  | 
 |  |  |         try { | 
 |  |  |             // 发送请求参数 | 
 |  |  |             StringBuilder param = new StringBuilder(); | 
 |  |  |             for (Map.Entry<String, String> entry : params.entrySet()) { | 
 |  |  |                 if (param.length() > 0) { | 
 |  |  |                     param.append("&"); | 
 |  |  |             // 构建完整的URL(包含查询参数) | 
 |  |  |             String fullUrl = buildUrlWithParams(baseUrl, params); | 
 |  |  |             URL requestUrl = new URL(fullUrl); | 
 |  |  |             connection = (HttpURLConnection) requestUrl.openConnection(); | 
 |  |  |  | 
 |  |  |             // 设置请求方法 | 
 |  |  |             connection.setRequestMethod("POST"); | 
 |  |  |             connection.setDoOutput(true); | 
 |  |  |             connection.setDoInput(true); | 
 |  |  |             connection.setUseCaches(false); | 
 |  |  |  | 
 |  |  |             // 设置请求头 | 
 |  |  |             if (headers != null) { | 
 |  |  |                 for (Map.Entry<String, String> entry : headers.entrySet()) { | 
 |  |  |                     connection.setRequestProperty(entry.getKey(), entry.getValue()); | 
 |  |  |                 } | 
 |  |  |                 param.append(entry.getKey()); | 
 |  |  |                 param.append("="); | 
 |  |  |                 param.append(entry.getValue()); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             RequestEntity entity = new StringRequestEntity(param.toString(), "application/json", "utf-8"); | 
 |  |  |             httpMethod.setRequestEntity(entity); | 
 |  |  |             // 执行请求并接收响应码 | 
 |  |  |             int resultCode = httpClient.executeMethod(httpMethod); | 
 |  |  |             // 设置默认的Content-Type | 
 |  |  |             if (!connection.getRequestProperties().containsKey("Content-Type")) { | 
 |  |  |                 connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             String respJson = httpMethod.getResponseBodyAsString(); | 
 |  |  |             if (resultCode == OK) { | 
 |  |  |                 return respJson; | 
 |  |  |             // 如果有请求体,写入数据 | 
 |  |  |             if (body != null && !body.isEmpty()) { | 
 |  |  |                 try (OutputStream os = connection.getOutputStream()) { | 
 |  |  |                     byte[] input = body.getBytes(StandardCharsets.UTF_8); | 
 |  |  |                     os.write(input, 0, input.length); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             // 获取响应码 | 
 |  |  |             int responseCode = connection.getResponseCode(); | 
 |  |  |  | 
 |  |  |             // 读取响应 | 
 |  |  |             StringBuilder response = new StringBuilder(); | 
 |  |  |             if (responseCode == HttpURLConnection.HTTP_OK) { | 
 |  |  |                 reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | 
 |  |  |             } else { | 
 |  |  |                 throw new HttpRequestException(resultCode, respJson); | 
 |  |  |                 reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); | 
 |  |  |             } | 
 |  |  |         } catch (UnsupportedEncodingException e) { | 
 |  |  |             throw new HttpRequestException(ENCODING_ERROR_CODE, e); | 
 |  |  |         } catch (HttpException e) { | 
 |  |  |             throw new HttpRequestException(HTTP_ERROR_CODE, e); | 
 |  |  |         } catch (IOException e) { | 
 |  |  |             throw new HttpRequestException(IO_ERROR_CODE, e); | 
 |  |  |  | 
 |  |  |             String line; | 
 |  |  |             while ((line = reader.readLine()) != null) { | 
 |  |  |                 response.append(line); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             return response.toString(); | 
 |  |  |  | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             throw new RuntimeException("POST请求失败: " + e.getMessage(), e); | 
 |  |  |         } finally { | 
 |  |  |             if (httpMethod != null) { | 
 |  |  |                 httpMethod.releaseConnection(); | 
 |  |  |             // 关闭连接 | 
 |  |  |             if (reader != null) { | 
 |  |  |                 try { | 
 |  |  |                     reader.close(); | 
 |  |  |                 } catch (Exception e) { /* ignore */ } | 
 |  |  |             } | 
 |  |  |             if (connection != null) { | 
 |  |  |                 connection.disconnect(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private static String buildUrlWithParams(String baseUrl, Map<String, String> params) { | 
 |  |  |         if (params == null || params.isEmpty()) { | 
 |  |  |             return baseUrl; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         StringBuilder urlBuilder = new StringBuilder(baseUrl); | 
 |  |  |         boolean firstParam = true; | 
 |  |  |  | 
 |  |  |         for (Map.Entry<String, String> entry : params.entrySet()) { | 
 |  |  |             if (firstParam) { | 
 |  |  |                 urlBuilder.append("?"); | 
 |  |  |                 firstParam = false; | 
 |  |  |             } else { | 
 |  |  |                 urlBuilder.append("&"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             try { | 
 |  |  |                 urlBuilder.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString())).append("=").append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString())); | 
 |  |  |             } catch (UnsupportedEncodingException e) { | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return urlBuilder.toString(); | 
 |  |  |     } | 
 |  |  | //    public static String postFormRequest(String url, Map<String, String> params,Map<String, String> headers) throws HttpRequestException { | 
 |  |  | //        Assert.hasLength(url, "请求url不能为空字符串。"); | 
 |  |  | //        Assert.notNull(params, "请求params不能为空。"); | 
 |  |  | // | 
 |  |  | //        PostMethod httpMethod = new PostMethod(url); | 
 |  |  | // | 
 |  |  | //        httpMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); | 
 |  |  | // | 
 |  |  | //        if(!ObjectUtils.isEmpty(headers)) { | 
 |  |  | //            httpMethod.addRequestHeader(headers.get(), "application/json"); | 
 |  |  | //        } | 
 |  |  | // | 
 |  |  | //        try { | 
 |  |  | //            // 发送请求参数 | 
 |  |  | //            StringBuilder param = new StringBuilder(); | 
 |  |  | //            for (Map.Entry<String, String> entry : params.entrySet()) { | 
 |  |  | //                if (param.length() > 0) { | 
 |  |  | //                    param.append("&"); | 
 |  |  | //                } | 
 |  |  | //                param.append(entry.getKey()); | 
 |  |  | //                param.append("="); | 
 |  |  | //                param.append(entry.getValue()); | 
 |  |  | //            } | 
 |  |  | // | 
 |  |  | //            RequestEntity entity = new StringRequestEntity(param.toString(), "application/json", "utf-8"); | 
 |  |  | //            httpMethod.setRequestEntity(entity); | 
 |  |  | //            // 执行请求并接收响应码 | 
 |  |  | //            int resultCode = httpClient.executeMethod(httpMethod); | 
 |  |  | // | 
 |  |  | //            String respJson = httpMethod.getResponseBodyAsString(); | 
 |  |  | //            if (resultCode == OK) { | 
 |  |  | //                return respJson; | 
 |  |  | //            } else { | 
 |  |  | //                throw new HttpRequestException(resultCode, respJson); | 
 |  |  | //            } | 
 |  |  | //        } catch (UnsupportedEncodingException e) { | 
 |  |  | //            throw new HttpRequestException(ENCODING_ERROR_CODE, e); | 
 |  |  | //        } catch (HttpException e) { | 
 |  |  | //            throw new HttpRequestException(HTTP_ERROR_CODE, e); | 
 |  |  | //        } catch (IOException e) { | 
 |  |  | //            throw new HttpRequestException(IO_ERROR_CODE, e); | 
 |  |  | //        } finally { | 
 |  |  | //            if (httpMethod != null) { | 
 |  |  | //                httpMethod.releaseConnection(); | 
 |  |  | //            } | 
 |  |  | //        } | 
 |  |  | //    } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private static Map<String, String> getRespHeaders(HttpMethodBase httpMethod) { | 
 |  |  |         //得到响应头 |