| | |
| | | import org.apache.commons.httpclient.methods.RequestEntity; |
| | | import org.apache.commons.httpclient.methods.StringRequestEntity; |
| | | import org.apache.commons.httpclient.params.HttpClientParams; |
| | | import org.apache.http.NameValuePair; |
| | | import org.apache.http.client.ClientProtocolException; |
| | | import org.apache.http.client.config.RequestConfig; |
| | | import org.apache.http.client.entity.UrlEncodedFormEntity; |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.client.utils.URIBuilder; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.message.BasicNameValuePair; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import javax.net.ssl.SSLSocketFactory; |
| | | import javax.net.ssl.TrustManager; |
| | | import javax.net.ssl.X509TrustManager; |
| | | 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.security.NoSuchAlgorithmException; |
| | | import java.security.cert.X509Certificate; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.HashMap; |
| | | import java.util.Locale; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | public class HttpUtil { |
| | |
| | | } |
| | | } |
| | | |
| | | 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) { |
| | | //得到响应头 |