| | |
| | | connection.setRequestProperty("accept", "*/*"); |
| | | connection.setRequestProperty("connection", "Keep-Alive"); |
| | | connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| | | connection.setConnectTimeout(5000); |
| | | connection.setReadTimeout(10000); |
| | | connection.connect(); |
| | | in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType)); |
| | | String line; |
| | |
| | | * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 |
| | | * @return 所代表远程资源的响应结果 |
| | | */ |
| | | public static String sendPost(String url, String param) { |
| | | public static Boolean sendPost(String url, String param) { |
| | | PrintWriter out = null; |
| | | BufferedReader in = null; |
| | | StringBuilder result = new StringBuilder(); |
| | | boolean success = false; |
| | | try { |
| | | log.info("sendPost - {}", url); |
| | | URL realUrl = new URL(url); |
| | |
| | | conn.setRequestProperty("Accept-Charset", "utf-8"); |
| | | conn.setRequestProperty("Charsert", "UTF-8"); |
| | | conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); |
| | | conn.setConnectTimeout(5000); |
| | | conn.setReadTimeout(10000); |
| | | conn.setDoOutput(true); |
| | | conn.setDoInput(true); |
| | | out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8)); |
| | |
| | | result.append(line); |
| | | } |
| | | log.info("recv - {}", result); |
| | | success = true; |
| | | } catch (ConnectException e) { |
| | | log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e); |
| | | } catch (SocketTimeoutException e) { |
| | |
| | | log.error("调用in.close Exception, url=" + url + ",param=" + param, ex); |
| | | } |
| | | } |
| | | return result.toString(); |
| | | return success; |
| | | } |
| | | |
| | | public static String sendPostByHeaderOld(String url, String param, Map<String, String> headers) { |
| | |
| | | conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| | | conn.setRequestProperty("Accept-Charset", "utf-8"); |
| | | conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); |
| | | conn.setConnectTimeout(5000); |
| | | conn.setReadTimeout(10000); |
| | | conn.setDoOutput(true); |
| | | conn.setDoInput(true); |
| | | if (!Objects.isNull(headers)) { |
| | |
| | | return "调用失败"; |
| | | } |
| | | |
| | | /** |
| | | * 对URL参数进行编码,处理中文乱码问题 |
| | | * |
| | | * @param params 参数字符串,格式:name1=value1&name2=value2 |
| | | * @param charset 编码格式 |
| | | * @return 编码后的参数字符串 |
| | | */ |
| | | private static String encodeParams(String params, String charset) { |
| | | if (StringUtils.isBlank(params)) { |
| | | return params; |
| | | } |
| | | |
| | | try { |
| | | StringBuilder encodedParams = new StringBuilder(); |
| | | String[] pairs = params.split("&"); |
| | | |
| | | for (int i = 0; i < pairs.length; i++) { |
| | | String pair = pairs[i]; |
| | | if (StringUtils.isNotBlank(pair)) { |
| | | int idx = pair.indexOf("="); |
| | | if (idx > 0) { |
| | | String key = pair.substring(0, idx); |
| | | String value = pair.substring(idx + 1); |
| | | // 对key和value都进行URL编码 |
| | | encodedParams.append(URLEncoder.encode(key, charset)) |
| | | .append("=") |
| | | .append(URLEncoder.encode(value, charset)); |
| | | } else { |
| | | // 没有等号,整个作为key |
| | | encodedParams.append(URLEncoder.encode(pair, charset)); |
| | | } |
| | | |
| | | if (i < pairs.length - 1) { |
| | | encodedParams.append("&"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return encodedParams.toString(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | log.error("URL参数编码失败", e); |
| | | return params; |
| | | } |
| | | } |
| | | |
| | | } |