| | |
| | | return httpClient; |
| | | } |
| | | |
| | | public static String postJsonRequest(String url, String request) throws HttpRequestException { |
| | | 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 { |
| | |
| | | |
| | | // 获取响应码 |
| | | int responseCode = connection.getResponseCode(); |
| | | log.info("postFormRequest 响应码:{},url:{}", responseCode, fullUrl); |
| | | |
| | | // 读取响应 |
| | | // 读取响应(非200时errorStream可能为null,避免NPE) |
| | | StringBuilder response = new StringBuilder(); |
| | | if (responseCode == HttpURLConnection.HTTP_OK) { |
| | | reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | } else { |
| | | reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); |
| | | InputStream respStream = responseCode == HttpURLConnection.HTTP_OK |
| | | ? connection.getInputStream() |
| | | : connection.getErrorStream(); |
| | | if (respStream == null) { |
| | | log.warn("postFormRequest 响应体为空,响应码:{},url:{}", responseCode, fullUrl); |
| | | return ""; |
| | | } |
| | | reader = new BufferedReader(new InputStreamReader(respStream)); |
| | | |
| | | String line; |
| | | while ((line = reader.readLine()) != null) { |