| | |
| | | |
| | | import java.io.OutputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.SocketTimeoutException; |
| | | import java.net.URL; |
| | | import java.util.Iterator; |
| | | import java.util.Map; |
| | | |
| | | public class HttpHzUtil { |
| | | |
| | | /**请求方法*/ |
| | | public static final String METHOD_POST = "POST"; |
| | | /**编码*/ |
| | | public static final String CHARSET = "UTF-8"; |
| | | /** |
| | | * 请求方法 |
| | | */ |
| | | public static final String METHOD_POST = "POST"; |
| | | /** |
| | | * 编码 |
| | | */ |
| | | public static final String CHARSET = "UTF-8"; |
| | | |
| | | |
| | | /**请求 |
| | | * @param url 请求链接 |
| | | * @param bodyData 请求body数据 |
| | | * @param keyValues 连接携带参数 |
| | | * @param header 表头携带参数 |
| | | /** |
| | | * 请求 |
| | | * |
| | | * @param url 请求链接 |
| | | * @param bodyData 请求body数据 |
| | | * @param keyValues 连接携带参数 |
| | | * @param header 表头携带参数 |
| | | * @param requestMethod 请求类型,默认:POST |
| | | * @param outTime 超时时间(毫秒),默认:300000 |
| | | * @param outTime 超时时间(毫秒),默认:300000 |
| | | * @return |
| | | */ |
| | | public static HttpHzResponse HttpURLUtilJson(String url, String bodyData, Map<String, Object> keyValues, Map<String, String> header, String requestMethod, Integer outTime) { |
| | |
| | | if (keyValues != null && !keyValues.isEmpty()) { |
| | | var9 = keyValues.entrySet().iterator(); |
| | | |
| | | while(var9.hasNext()) { |
| | | entry = (Map.Entry)var9.next(); |
| | | dataP.append((String)entry.getKey()).append("="); |
| | | while (var9.hasNext()) { |
| | | entry = (Map.Entry) var9.next(); |
| | | dataP.append((String) entry.getKey()).append("="); |
| | | dataP.append(entry.getValue()); |
| | | dataP.append("&"); |
| | | } |
| | |
| | | requestMethod = METHOD_POST; |
| | | } |
| | | |
| | | if(outTime == null || outTime < 0){ |
| | | if (outTime == null || outTime < 0) { |
| | | outTime = 300000; |
| | | } |
| | | |
| | | URL httpUrl = new URL(url); |
| | | con = (HttpURLConnection)httpUrl.openConnection(); |
| | | con = (HttpURLConnection) httpUrl.openConnection(); |
| | | con.setRequestMethod(requestMethod); |
| | | con.setDoOutput(true); |
| | | con.setDoInput(true); |
| | |
| | | if (header != null) { |
| | | var9 = header.entrySet().iterator(); |
| | | |
| | | while(var9.hasNext()) { |
| | | entry = (Map.Entry)var9.next(); |
| | | con.setRequestProperty((String)entry.getKey(), (String)entry.getValue()); |
| | | while (var9.hasNext()) { |
| | | entry = (Map.Entry) var9.next(); |
| | | con.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); |
| | | } |
| | | } |
| | | |
| | |
| | | os.write(bodyData.getBytes(CHARSET)); |
| | | } |
| | | |
| | | httpHzResponse.setCode(con.getResponseCode()+""); |
| | | if(con.getErrorStream() != null){ |
| | | httpHzResponse.setCode(con.getResponseCode() + ""); |
| | | if (con.getErrorStream() != null) { |
| | | httpHzResponse.setMsg(IOUtils.toString(con.getErrorStream(), CHARSET)); |
| | | } |
| | | if(con.getInputStream() != null){ |
| | | if (HttpURLConnection.HTTP_OK == con.getResponseCode() || HttpURLConnection.HTTP_CREATED == con.getResponseCode()) { |
| | | httpHzResponse.setData(IOUtils.toString(con.getInputStream(), CHARSET)); |
| | | } |
| | | |
| | | }catch (SocketTimeoutException var15) { |
| | | httpHzResponse.setMsg(var15.getMessage()); |
| | | httpHzResponse.setCode(HttpHzResponse.CODE_TIME_OUT); |
| | | var15.printStackTrace(); |
| | | } catch (Exception var14) { |
| | | httpHzResponse.setMsg(var14.getMessage()); |
| | | var14.printStackTrace(); |
| | | } finally { |
| | | if (con != null) { |