package com.base.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import org.slf4j.Logger; import com.amarsoft.are.ARE; import com.amarsoft.are.lang.StringX; import com.amarsoft.are.log.Log; public class HttpUtil { private static final Log LOGGER = ARE.getLog(); private static final String HTTP_CONNECTION_TIMEOUT = "3000"; private static final String HTTP_READ_TIMEOUT = "3000"; private static final String HTTP_METHOD_GET = "GET"; private static final String HTTP_METHOD_POST = "POST"; private static String DoProcess(String strUrl, String strContent, String httpMethod) throws Exception { return DoProcess(strUrl,strContent,httpMethod,null); } private static String DoProcess(String strUrl, String strContent, String httpMethod,String timeOut) throws Exception { URL postUrl = new URL(strUrl); HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); System.setProperty("sun.net.client.defaultConnectTimeout", HTTP_CONNECTION_TIMEOUT); System.setProperty("sun.net.client.defaultReadTimeout", HTTP_READ_TIMEOUT); if(StringX.isEmpty(timeOut)){ connection.setConnectTimeout(Integer.parseInt(HTTP_CONNECTION_TIMEOUT)); }else{ connection.setConnectTimeout(Integer.parseInt(timeOut)); } if(StringX.isEmpty(timeOut)){ connection.setReadTimeout(Integer.parseInt(HTTP_READ_TIMEOUT)); }else{ connection.setReadTimeout(Integer.parseInt(timeOut)); } connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod(httpMethod); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type","application/json;charset=UTF-8"); connection.connect(); if(strContent != null && !"".equals(strContent)) { OutputStream out = connection.getOutputStream(); // out.write(strContent.getBytes("utf-8")); out.write(strContent.getBytes("UTF-8")); out.close(); } BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); String line=""; StringBuilder strBuf=new StringBuilder(); // LOGGER.info("{} URL=>{}", new Object[]{httpMethod,strUrl}); LOGGER.info("=============== "+httpMethod+":"+strUrl); while ((line = reader.readLine()) != null){ strBuf.append(line); } reader.close(); // LOGGER.debug("responseCode => {} | responseMessage => {}", new Object[]{connection.getResponseCode(),connection.getResponseMessage()}); connection.disconnect(); return strBuf.toString(); } public static String PostMsg(String strPostUrl, String strContent) throws Exception { return DoProcess(strPostUrl, strContent, HTTP_METHOD_POST); } public static String PostLongMsg(String strPostUrl, String strContent) throws Exception { return DoProcess(strPostUrl, strContent, HTTP_METHOD_POST,"3600000"); } public static String GetMsg(String strGetUrl) throws Exception { return DoProcess(strGetUrl, null, HTTP_METHOD_GET); } public static void main(String[] args) throws Exception { String sServer = "http://localhost:8080/cebfl/webapi/resf"; String sServiceImpl = "/logon/manager/logon"; String sParam = "un=admin&pwd=07C193B5FFC84B6F5A903A00A4D3DFF07ED10BEC39024D00&dev_reg_id=111&keytype=common"; // String strGetUrl = sServer+sServiceImpl+"?"+sParam; String strGetUrl = "http://localhost:9090/getConfiguration"; // try { // String jsonStr = GetMsg(strGetUrl); //// jsonStr = new String(jsonStr.getBytes("iso-8859-1"),"GBK"); //// jsonStr = URLDecoder.decode(jsonStr, "GBK"); // System.out.println(jsonStr); // Map map = (Map) JsonUtil.str2Map(jsonStr); // System.out.println(map); // System.out.println( // ((Map)(map.get("header"))).get("code") // ); // Map map1 = // (Map) ((Map)(map.get("body"))).get("outline"); // System.out.println(map1.get("code")); // System.out.println(map1.get("message")); // } catch (Exception e) { // System.out.println(e); // } System.out.println(GetMsg(strGetUrl)); } }