import org.apache.commons.lang3.StringUtils; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class GetAndPostUtil { /** * get方式发送请求 * * @param url * @param params * @return * @throws Exception */ public static String sendByGet(String url, String params) throws Exception { String retStr = ""; // 创建url String tmpUrl = url; if (params != null) { tmpUrl += "?" + params; } URL urlObj = new URL(tmpUrl); // 打开http连接 HttpURLConnection httpConn = (HttpURLConnection)(urlObj.openConnection()); // 设置http连接属性 httpConn.setConnectTimeout(30000); httpConn.setRequestMethod("GET"); //设置跟踪Key httpConn.setRequestProperty("Track-Key", Thread.currentThread().getName()); // 发送请求 httpConn.connect(); // 接受响应数据 InputStream isr = httpConn.getInputStream(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); int b; while ((b = isr.read()) != -1) { bao.write(b); } isr.close(); // 关闭http连接 httpConn.disconnect(); // 返回响应数据,并设置为utf-8编码 retStr = new String(bao.toByteArray(), "ISO-8859-1"); retStr = new String(retStr.getBytes("ISO-8859-1"), "UTF-8"); // retStr=new String(bao.toByteArray(), "UTF-8"); return retStr; } /** * post方式发送请求 * * @param url * @param params * @return * @throws Exception */ public static String sendByPost(String url, String params) throws Exception { String retStr = ""; if (StringUtils.isNotEmpty(url) && params != null) { // 创建url URL urlObj = new URL(url); // 打开http连接 HttpURLConnection httpConn = (HttpURLConnection)(urlObj.openConnection()); // 设置http连接属性 httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置跟踪Key httpConn.setRequestProperty("Track-Key", Thread.currentThread().getName()); httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); // 发送请求数据,并设置为utf-8编码 OutputStream out = httpConn.getOutputStream(); out.write(params.getBytes("UTF-8")); out.close(); // 接受响应数据 InputStream isr = httpConn.getInputStream(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); int b; while ((b = isr.read()) != -1) { bao.write(b); } isr.close(); // 关闭http连接 httpConn.disconnect(); // 返回响应数据,并设置为utf-8编码 retStr = new String(bao.toByteArray(), "ISO-8859-1"); retStr = new String(retStr.getBytes("ISO-8859-1"), "UTF-8"); // retStr=new String(bao.toByteArray(), "UTF-8"); } return retStr; } /** * post方式发送请求 * * @param url * @param params * @return * @throws Exception */ public static String sendByPost(String url, String params,String contentType) throws Exception { String retStr = ""; if (StringUtils.isNotEmpty(url) && params != null) { // 创建url URL urlObj = new URL(url); // 打开http连接 HttpURLConnection httpConn = (HttpURLConnection)(urlObj.openConnection()); // 设置http连接属性 httpConn.setRequestProperty("Content-Type", contentType); //设置跟踪Key httpConn.setRequestProperty("Track-Key", Thread.currentThread().getName()); httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); // 发送请求数据,并设置为utf-8编码 OutputStream out = httpConn.getOutputStream(); out.write(params.getBytes("UTF-8")); out.close(); // 接受响应数据 InputStream isr = httpConn.getInputStream(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); int b; while ((b = isr.read()) != -1) { bao.write(b); } isr.close(); // 关闭http连接 httpConn.disconnect(); // 返回响应数据,并设置为utf-8编码 retStr = new String(bao.toByteArray(), "ISO-8859-1"); retStr = new String(retStr.getBytes("ISO-8859-1"), "UTF-8"); // retStr=new String(bao.toByteArray(), "UTF-8"); } return retStr; } }