package cn.com.lzt.common.util; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Vector; import com.alibaba.fastjson.JSONObject; public class HttpUtil { /** * 发起https请求并获取结果 * * @param requestUrl * 请求地址 * @param requestMethod * 请求方式(GET、POST) * @param outputStr * 提交的数据 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值) */ public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject = null; StringBuffer buffer = new StringBuffer(); HttpURLConnection httpUrlConn = null; try { // 创建SSLContext对象,并使用我们指定的信任管理器初始化 URL url = new URL(requestUrl); httpUrlConn = (HttpURLConnection) url.openConnection(); httpUrlConn.setDoOutput(true); httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false); httpUrlConn.setRequestProperty("Content-Type", "text/plain;charset=UTF-8"); // httpUrlConn.setRequestProperty("content-type", "text/html"); // 设置请求方式(GET/POST) httpUrlConn.setRequestMethod(requestMethod); if ("GET".equalsIgnoreCase(requestMethod)) httpUrlConn.connect(); // 当有数据需要提交时 if (null != outputStr) { OutputStream outputStream = httpUrlConn.getOutputStream(); // 注意编码格式,防止中文乱码 outputStream.write(outputStr.getBytes("UTF-8")); outputStream.close(); } // 将返回的输入流转换成字符串 InputStream inputStream = httpUrlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader( inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader( inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); // 释放资源 inputStream.close(); inputStream = null; httpUrlConn.disconnect(); jsonObject = JSONObject.parseObject(buffer.toString()); // jsonObject = JSONObject.fromObject(buffer.toString()); } catch (ConnectException ce) { org.jeecgframework.core.util.LogUtil .info("Weixin server connection timed out."); } catch (Exception e) { //e.printStackTrace(); org.jeecgframework.core.util.LogUtil.info("https request error:{}" + e.getMessage()); }finally{ try { httpUrlConn.disconnect(); }catch (Exception e) { e.printStackTrace(); org.jeecgframework.core.util.LogUtil.info("http close error:{}"+ e.getMessage()); } } return jsonObject; } public static HttpRespons post(String strURL,String params){ try { HttpRespons httpResponser = new HttpRespons(); URL url = new URL(strURL);// 创建连接 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestMethod("POST"); // 设置请求方式 connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式 connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式 connection.connect(); OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream(), "UTF-8"); // utf-8编码 out.append(params); out.flush(); out.close(); // 读取响应 InputStream is = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(is)); httpResponser.contentCollection = new Vector(); StringBuffer temp = new StringBuffer(); String line = bufferedReader.readLine(); while (line != null) { httpResponser.contentCollection.add(line); temp.append(line).append("\r\n"); line = bufferedReader.readLine(); } bufferedReader.close(); httpResponser.urlString = strURL; httpResponser.defaultPort = connection.getURL().getDefaultPort(); httpResponser.file = connection.getURL().getFile(); httpResponser.host = connection.getURL().getHost(); httpResponser.path = connection.getURL().getPath(); httpResponser.port = connection.getURL().getPort(); httpResponser.protocol = connection.getURL().getProtocol(); httpResponser.query = connection.getURL().getQuery(); httpResponser.ref = connection.getURL().getRef(); httpResponser.userInfo = connection.getURL().getUserInfo(); httpResponser.content = new String(temp.toString().getBytes(), "UTF-8"); httpResponser.contentEncoding = "UTF-8"; httpResponser.code = connection.getResponseCode(); httpResponser.message = connection.getResponseMessage(); httpResponser.contentType = connection.getContentType(); httpResponser.method = connection.getRequestMethod(); httpResponser.connectTimeout = connection.getConnectTimeout(); httpResponser.readTimeout = connection.getReadTimeout(); return httpResponser; } catch (Exception e) { e.printStackTrace(); return null; } } public static void main(String args[]){ String basePath = "http://localhost:8080/hglserver/"; // 修改密码 /*basePath = basePath + "sys/changePwd.do"; SysDefaultReq req = new SysDefaultReq(); req.setUserId("8a8ab0b246dc81120146dc8181a60055"); req.setPwd("a324509dc1a3089a"); req.setNewpwd("aaaaaaaaa");*/ // --------------------------------------------------- // System.out.println("请求:" + JSON.toJSONString(req)); // System.out.println("响应:" + post(basePath, JSON.toJSONString(req)).getContent()); } }