| | |
| | | return httpClient; |
| | | } |
| | | |
| | | public static String postJsonRequest(String url, String request) throws HttpRequestException { |
| | | /** |
| | | * POST JSON,只校验 HTTP 状态码,允许响应体为空。 |
| | | * <p> |
| | | * 用于 /hangup 这类"成功只回 200、没有响应体"的接口: |
| | | * {@link #postJsonRequest(String, String)} 在读响应体时如果为空会抛 RESPONSE_NULL_ERROR_CODE(999994), |
| | | * 会把一次成功的挂断误判成失败(还会触发无意义的重试)。 |
| | | * <p> |
| | | * 非 2xx 时抛出的 HttpRequestException 里带上状态码和响应体,方便排查。 |
| | | * |
| | | * @param url 请求地址 |
| | | * @param request json 请求体 |
| | | */ |
| | | public static void postJsonRequestAllowEmptyResponse(String url, String request) throws HttpRequestException { |
| | | Assert.hasLength(url, "请求url不能为空字符串。"); |
| | | EntityEnclosingMethod httpMethod = new PostMethod(url); |
| | | httpMethod.addRequestHeader("Content-Type", "application/json; charset=UTF-8"); |
| | | setHeaderRequestId(httpMethod); |
| | | try { |
| | | RequestEntity entity = new StringRequestEntity(request, "application/json", "utf-8"); |
| | | httpMethod.setRequestEntity(entity); |
| | | int resultCode = httpClient.executeMethod(httpMethod); |
| | | if (resultCode >= 200 && resultCode < 300) { |
| | | return; |
| | | } |
| | | String respJson = ""; |
| | | try { |
| | | respJson = httpMethod.getResponseBodyAsString(); |
| | | } catch (Exception ignore) { |
| | | // 读不到响应体不影响判断,状态码已经能说明问题 |
| | | } |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static Boolean postJsonRequest(String url, String request) throws HttpRequestException { |
| | | Assert.hasLength(url, "请求url不能为空字符串。"); |
| | | EntityEnclosingMethod httpMethod = new PostMethod(url); |
| | | |
| | |
| | | if (inputStream == null) { |
| | | throw new HttpRequestException(RESPONSE_NULL_ERROR_CODE, "响应为null"); |
| | | } |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,charset)); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); |
| | | StringBuffer stringBuffer = new StringBuffer(); |
| | | String str = ""; |
| | | while ((str = reader.readLine()) != null) { |
| | |
| | | String respJson = stringBuffer.toString(); |
| | | // String respJson = httpMethod.getResponseBodyAsString(); |
| | | if (resultCode == OK) { |
| | | return respJson; |
| | | return true; |
| | | } else { |
| | | throw new HttpRequestException(resultCode, respJson); |
| | | } |
| | |
| | | * json的post请求 |
| | | * |
| | | * @param url 请求url |
| | | * @param reqEntity 请求头与请求体的封装 |
| | | * @param respBodyClass 响应体类型 |
| | | * @return 返回的响应结果 |
| | | */ |
| | | public static String postXmlRequest(String url, String request) throws HttpRequestException { |
| | | return postXmlRequest(url, request, EMPTY_HEADERS); |
| | | } |
| | | |
| | | public static String postXmlRequest(String url, String request, Map<String, String> headers) throws HttpRequestException { |
| | | Assert.hasLength(url, "璇锋眰url涓嶈兘涓虹┖瀛楃涓层€?"); |
| | | Assert.notNull(request, "XML request must not be null"); |
| | | EntityEnclosingMethod httpMethod = new PostMethod(url); |
| | | |
| | | httpMethod.addRequestHeader(CONTENT_TYPE, TEXT_XML_UTF8); |
| | | setHeaderRequestId(httpMethod); |
| | | if (headers != null && headers != EMPTY_HEADERS) { |
| | | setReqHeaders(headers, httpMethod); |
| | | } |
| | | |
| | | try { |
| | | RequestEntity entity = new StringRequestEntity(request, TEXT_XML, StandardCharsets.UTF_8.name()); |
| | | httpMethod.setRequestEntity(entity); |
| | | |
| | | int resultCode = httpClient.executeMethod(httpMethod); |
| | | String contentType = httpMethod.getResponseHeader(CONTENT_TYPE) == null |
| | | ? null : httpMethod.getResponseHeader(CONTENT_TYPE).getValue(); |
| | | String charset = StandardCharsets.UTF_8.name(); |
| | | 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, "鍝嶅簲涓簄ull"); |
| | | } |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | String str; |
| | | while ((str = reader.readLine()) != null) { |
| | | stringBuilder.append(str); |
| | | } |
| | | reader.close(); |
| | | String respXml = stringBuilder.toString(); |
| | | if (resultCode == OK) { |
| | | return respXml; |
| | | } else { |
| | | throw new HttpRequestException(resultCode, respXml); |
| | | } |
| | | } 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(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static <RESPBODY> HttpEntity<RESPBODY> postJsonRequestV2(String url, HttpEntity<?> reqEntity, Class<RESPBODY> respBodyClass) { |
| | | Assert.hasLength(url, "请求url不能为空字符串。"); |