Examples are as follows:
package com.common.util;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.ConnectException;import java.net.HttpURLConnection;import java.net.URL;import java.util.Date;import javax.net.ssl.HttpsURLConnection;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSocketFactory;import javax.net.ssl.TrustManager;import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.common.weixin.access.util.MyX509TrustManager;/** * Public Platform General Interface Tool Class* * @author * @date 2013-08-09 */public class HttpConnectUtil {private static Logger log = LoggerFactory.getLogger(HttpConnectUtil.class);/*** Initiate https request and get the result* * @param requestUrl Request address* @param requestMethod Request method (GET, POST)* @param outputStr Submitted data* @return JSONObject(get(get the attribute value of the json object through JSONObject.get(key))*/public static String httpRequest(String requestUrl, String requestMethod, String outputStr) {String result = null;StringBuffer buffer = new StringBuffer();try {URL url = new URL(requestUrl);HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();// Set the general request attribute httpUrlConn.setRequestProperty("accept", "*/*");httpUrlConn.setRequestProperty("connection", "Keep-Alive");httpUrlConn.setRequestProperty("Charset", "utf-8"); httpUrlConn.setDoOutput(true);httpUrlConn.setDoInput(true);httpUrlConn.setUseCaches(false);// Set request method (GET/POST) httpUrlConn.setRequestMethod(requestMethod);if ("GET".equalsIgnoreCase(requestMethod))httpUrlConn.connect();// When there is data that needs to be submitted if (null != outputStr) {OutputStream outputStream = httpUrlConn.getOutputStream();// Pay attention to the encoding format to prevent Chinese garbled outputStream.write(outputStr.getBytes("UTF-8"));outputStream.close();}// Convert the returned input stream into a string 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();// Release the resource inputStream.close();inputStream = null;httpUrlConn.disconnect();result = buffer.toString();//jsonObject = JSONObject.fromObject(buffer.toString());} catch (ConnectException ce) {log.error("Weixin server connection timed out.");} catch (Exception e) {log.error("https request error:{}", e);}return result;}/*** Initiate https request and get the result* * @param requestUrl Request address* @param requestMethod Request method (GET, POST)* @param outputStr Data submitted* @return Result String*/public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) throws Exception{String result = null;StringBuffer buffer = new StringBuffer();try { // Create an SSLContext object and initialize TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); // Get the SSLSocketFactory object from the above SSLContext object SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl); HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection(); httpUrlConn.setSSLSocketFactory(ssf); // Set the general request attribute httpUrlConn.setRequestProperty("accept", "*/*");httpUrlConn.setRequestProperty("connection", "Keep-Alive");httpUrlConn.setRequestProperty("Charset", "utf-8"); httpUrlConn.setDoOutput(true);httpUrlConn.setDoInput(true);httpUrlConn.setUseCaches(false);// Set request method (GET/POST) httpUrlConn.setRequestMethod(requestMethod);if ("GET".equalsIgnoreCase(requestMethod))httpUrlConn.connect();// When there is data that needs to be submitted if (null != outputStr) {OutputStream outputStream = httpUrlConn.getOutputStream();// Pay attention to the encoding format to prevent Chinese garbled outputStream.write(outputStr.getBytes("UTF-8"));outputStream.close();}// Convert the returned input stream to a string 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();// release resource inputStream.close();inputStream = null;httpUrlConn.disconnect();result = buffer.toString();//jsonObject = JSONObject.fromObject(buffer.toString());} catch (ConnectException ce) {log.error(requestUrl + " server connection timed out.");throw new Exception("Connect Server Timed out");} catch (Exception e) {log.error(requestUrl +" https request error:{}", e);throw new Exception("HTTPS request error");} return result;} public static String getIpAddr(HttpServletRequest request) {String ip = request.getHeader("x-forwarded-for");if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}return ip;} }The above example code of the HttpConnectUtil of Java public platform universal interface tool is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.