HTTP request:
If you need to turn it on by yourself in Json format, you can go to N poses...
//Processing http request requestUrl is the request address requestMethod request method, the value is "GET" or "POST" public static String httpRequest(String requestUrl,String requestMethod,String outputStr){ StringBuffer buffer=null; try{ URL url=new URL(requestUrl); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod(requestMethod); connect(); //Write content to the server side that is to take the parameter that needs to be launched if(null!=outputStr){ OutputStream os=conn.getOutputStream(); os.write(outputStr.getBytes("utf-8")); os.close(); } //Read the content returned by the server side InputStream is=conn.getInputStream(); InputStreader isr=new InputStreamReader(is,"utf-8"); BufferedReader br=new BufferedReader(isr); buffer=new StringBuffer(); String line=null; while((line=br.readLine())!=null){ buffer.append(line); } }catch(Exception e){ e.printStackTrace(); } return buffer.toString(); }HTTPS request:
1. I won’t introduce the difference between https and http here. If you access the https link in Java, you need to have a corresponding SSL certificate. If you don’t, you cannot access it (using https using http will return 403), so we need to customize a trust manager first. Implement the X509TrustManger interface that comes with java, code:
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[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } @Override public X509Certificate[] getAcceptedIssuers() { // TODO Auto-generated method stub return null; } }2. Then we can use https to request:
/* * Handle https GET/POST request* Request address, request method, parameters* */ public static String httpsRequest(String requestUrl,String requestMethod,String outputStr){ StringBuffer buffer=null; try{ //Create SSLContext SSLContext sslContext=SSLContext.getInstance("SSL"); TrustManager[] tm={new MyX509TrustManager()}; //Initialize sslContext.init(null, tm, new java.security.SecureRandom());; //Get SSLSocketFactory object SSLSocketFactory ssf=sslContext.getSocketFactory(); URL url=new URL(requestUrl); HttpsURLConnection conn=(HttpsURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod(requestMethod); //Set SSLSoctetFactory used by the current instance conn.setSSLSocketFactory(ssf); connect(); //Write content to the server if(null!=outputStr){ OutputStream os=conn.getOutputStream(); os.write(outputStr.getBytes("utf-8")); os.close(); } //Read the content returned by the server InputStream is=conn.getInputStream(); InputStreamReader isr=new InputStreamReader(is,"utf-8"); BufferedReader br=new BufferedReader(isr); buffer=new StringBuffer(); String line=null; while((line=br.readLine())!=null){ buffer.append(line); } }catch(Exception e){ e.printStackTrace(); } return buffer.toString(); }The above example of Java sending http and https requests 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.