Operation, a request for another project Service Engineering Controller, HttpRequestUtil tools may be employed at this time to send the request to acquire the return value. Accordingly, a need for a tool class transmission request.
HttpRequestUtil.java
package com.alphajuns.util; import org.apache.commons.codec.binary.Base64; import org.apache.log4j.Logger; import javax.net.ssl.*; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; public class HttpRequestUtil { private static Logger mailAndLyncFailLogger = Logger.getLogger("MailAndLyncFailLogger"); /*** * 向指定URL发送POST方法的请求 * @Param URL transmission request URL * @return URL of the resource on behalf of the remote response * / public static int sendGet (String URL) { int the resultCode = 0 ; the try { URL realUrl = new new URL (URL); // open and URL the connection between the HttpURLConnection Conn = (the HttpURLConnection) realUrl.openConnection (); conn.setRequestMethod ( "the GET"); // set the post connected // set common request attribute conn.setRequestProperty ( "connection", "Keep -Alive " ); conn.setRequestProperty ( "the Charset", "UTF-. 8" ); conn.setUseCaches ( to false ); // establishing an actual connection conn.connect (); // get the response state of the resultCode = conn.getResponseCode (); } the catch ( E Exception) { e.printStackTrace (); the throw new new a RuntimeException (E); } return the resultCode; } / ** * * GET method request transmission to the specified the URL * @param URL transmission request of the URL * @return Represents the response to a remote resource URL * / public static String sendGetAndRetrieveContent (String URL) { String Result = null ; the StringBuffer ResultBuffer = new new the StringBuffer (); the try { the URL realUrl = new new the URL (URL); // open a connection and the URL the HttpURLConnection Conn = (the HttpURLConnection) realUrl.openConnection (); conn.setRequestMethod ( "the GET"); // set the post connected // set common request attribute conn.setRequestProperty ( "connection", "the Keep-Alive" ); conn.setRequestProperty("Charset", "UTF-8"); conn.setUseCaches(false); // 建立实际的连接 conn.connect(); // 获取响应状态 InputStream in = conn.getInputStream(); InputStreamReader reader = new InputStreamReader(in,"UTF-8"); BufferedReader breader = new BufferedReader(reader); String str = null; while((STR = breader.readLine ()) =! null param request parameters, the request parameter should be in the form name1 value1 name2 value2 = & =){ resultBuffer.append (STR); } breader.close (); reader.Close (); in.close (); conn.disconnect (); } the catch (Exception E) { e.printStackTrace (); the throw new new a RuntimeException (E ); } return Result = resultBuffer.toString (); } / ** * request * POST method sent to the specified the URL * @param URL transmission request of the URL * @param * @Return response URL represents the remote resource * / public static int sendPost (URL String, String param) { the PrintWriter OUT = null ; int the resultCode = 0 ; the try { URL realUrl = new new URL (URL); mailAndLyncFailLogger.info ( " realUrl: "+ realUrl); mailAndLyncFailLogger.info ( " param: "+ param); // open a connection and the URL the HttpURLConnection Conn = (the HttpURLConnection) realUrl.openConnection (); conn.setRequestMethod ( "the POST"); // set the post connected // setting request attribute common conn.setRequestProperty ( "the Content-the Type", "file application / X-WWW-form-urlencoded" ); conn.setRequestProperty ( "Connection", "the Keep -alive " ); conn.setRequestProperty ( " the Charset "," UTF-. 8 " ); // send a POST request must set the following two lines conn.setDoOutput ( to true ); conn.setDoInput ( to true ); conn.setUseCaches ( to false ) ; // Get URLConnection object corresponding to the output stream OUT = new newThe PrintWriter (conn.getOutputStream ()); // send a request parameter Out.print (param); // the flush buffered output stream out.flush (); // define BufferedReader input stream to read the URL of the response the resultCode = conn.getResponseCode (); } the catch (Exception E) { e.printStackTrace (); mailAndLyncFailLogger.error (e.getMessage ()); } the finally { the try { IF (OUT =! null ) { the out.close (); } } the catch (Exception EX) { ex.printStackTrace (); mailAndLyncFailLogger.error (ex.getMessage ()); } } return the resultCode; } / ** * request * POST method sent to the specified the URL * @param URL transmission request of the URL * @param param request parameters, the request parameter should be name1 form = value1 name2 value2 = & * @param the userName new data to be sent account name * @param passWord, new data to be sent account password * @return response URL represents the remote resource * / public static int sendPostExtend (URL String , String param, String userName, String passWord) { the PrintWriter OUT = null ; int resultCode = -1; String base64Str = null; String userInfo = null; try { URL realUrl = new URL(url); mailAndLyncFailLogger.info("realUrl:"+realUrl); mailAndLyncFailLogger.info("param:"+param+"; from userName="+userName); // 打开和URL之间的连接 HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection(); //SSLContext sslcontext = SSLContext.getInstance("SSL","SunJSSE"); SSLContext sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom()); HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() { public boolean verify(String s, SSLSession sslsession) { mailAndLyncFailLogger.info("WARNING: Hostname is not matched for cert."); return true; } }; conn.setHostnameVerifier(ignoreHostnameVerifier); conn.setSSLSocketFactory(sslcontext.getSocketFactory()); conn.setRequestMethod("POST"); //设置post方式连接 // 设置通用的请求属性 conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"); conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br"); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty ( "Upgrade-Insecure-Requests", ". 1" ); // Liuqi pass need to include header specified for temporarily authorizing the Base64 Base64 = new new the Base64 (); the userInfo = the userName + ":" + passWord,; base64Str base64.encodeToString = (userInfo.getBytes ( "UTF-. 8" )); conn.setRequestProperty ( "the Authorization", "Basic" + base64Str); // send a POST request must set the following two lines conn.setDoOutput ( to true ); Conn .setDoInput ( to true ); conn.setUseCaches ( to false ); //Does not verify // conn.setHostnameVerifier (DO_NOT_VERIFY); // Get URLConnection object corresponding to the output stream OUT = new new the PrintWriter (conn.getOutputStream ()); // send a request parameter Out.print (param); // the flush stream output buffer out.flush (); // define BufferedReader input stream to read the URL of the response conn.getResponseCode (); the resultCode = conn.getResponseCode (); } the catch (Exception E) { e.printStackTrace (); mailAndLyncFailLogger.error (e.getMessage ()); } the finally { try { if (out != null) { out.close(); } } catch (Exception ex) { ex.printStackTrace(); mailAndLyncFailLogger.error(ex.getMessage()); } } return resultCode; } }
Above file depends on MyX509TrustManager.java
package com.alphajuns.util; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; public class MyX509TrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate certificates[],String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] ax509certificate,String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { // TODO Auto-generated method stub return null; } }
Configuring maven
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency>