HTTPS的GET、POST、PUT、DELETE请求

 HTTPS的POST、GET、DELETE、PUT请求

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;






import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;


import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;


/**
* @author: zhaochunhui
* @Date: 2021/1/21 18:04
* @Description:
*/
public class SSLClient  extends DefaultHttpClient {
    public SSLClient() throws Exception {
        super();
        //传输协议需要根据自己的判断
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }


            @Override
            public void checkServerTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }


            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }


    public String doPost(String url, String map, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            //设置参数
            httpPost.addHeader("Accept", "application/json");
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity stringEntity = new StringEntity(map);
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");
            httpPost.setEntity(stringEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public String doPut(String url, String map, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpPut httpPut = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpPut = new HttpPut(url);
            //设置参数
            httpPut.addHeader("Accept", "application/json");
            httpPut.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpPut.addHeader("iBaseToken", "3285EA7E1D79955A1C832F57AFE0ABC7EDEBD7B1A4D5CFE14A1887C27160EFA6");
            httpPut.addHeader(new BasicHeader("Cookie", "session=ismsession=1708491774373951553490536926597163443211973631094874730003"));
            StringEntity stringEntity = new StringEntity(map);
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");
            httpPut.setEntity(stringEntity);
            HttpResponse response = httpClient.execute(httpPut);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public String doGet(String url,  String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpGet httpGet = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpGet = new HttpGet(url);
            //设置参数
            httpGet.addHeader("Accept", "application/json");
            httpGet.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpGet.addHeader("iBaseToken", "DD6452F8C28C8B6C4DA09F10DBBD8F0AEB679C8C0527F1C130A3611CF7C788EE");
            httpGet.addHeader(new BasicHeader("Cookie", "session=ismsession=1211398665627844242134562708694152040389047758107797599492"));
            HttpResponse response = httpClient.execute(httpGet);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public String doDelete(String url, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpDelete httpDelete = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpDelete = new HttpDelete(url);
            //设置参数
            httpDelete.addHeader("Accept", "application/json");
            httpDelete.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpDelete.addHeader("iBaseToken", "8C2D9B310A6039D6382A0F675B9B0A09680975E0E99FC3783A41AAF9D8862A1F");
            httpDelete.addHeader(new BasicHeader("Cookie", "session=ismsession=476387060735095844496830122778384587051871883735873158400"));
            HttpResponse response = httpClient.execute(httpDelete);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }




}
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.junit.Test;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
* @author: zhaochunhui
* @Date: 2021/1/21 18:06
* @Description:
*/
public class test {




    private String url = "https://25.11.3.23:8088/deviceManager/rest/xxx/sessions";
    private String charset = "utf-8";
    private SSLClient httpClientUtil; {
        try {
            httpClientUtil = new SSLClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    };






    public static String md5(String text) {
        String result="";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(text.getBytes("UTF-8"));
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
//       System.out.println("result: " + buf.toString());// 32位的加密
//       System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
        }
        return result;
    }
    @Test
    public void HttpsPostTest() throws Exception {
        String ver = "1.0";
        String msgId = "91b024e3-06ca-4a79-9993-1472d0fdb973";
        String appId = "300011853779";
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String appKey = "A0702827F21C9CC7DDC93AEF24B6B16B";
        String sign = md5(ver + appId + msgId + timestamp + appKey).toUpperCase();
        JSONObject result = new JSONObject();
        result.put("username", "CMPuser");
        result.put("password", "Ceb@12345");
        result.put("scope", "0");


        String encryptStr = result.toString();
        System.out.println("encryptStr:" + encryptStr);
        String httpOrgCreateTestRtn = httpClientUtil.doPost(url, encryptStr, charset);
        System.out.println("result:" + httpOrgCreateTestRtn);


    }
    @Test
    public void HttpsPutTest() throws Exception {
        JSONObject result = new JSONObject();
        result.put("username", "CMPuser");
        result.put("password", "Ceb@12345");
        result.put("scope", "0");


        String encryptStr = result.toString();
        System.out.println("encryptStr:" + encryptStr);
        String httpOrgCreateTestRtn = httpClientUtil.doPut("https://25.11.3.23:8088/deviceManager/rest/2102351PXS9WKA800003/offline_user/CMPuser", encryptStr, charset);
        System.out.println("result:" + httpOrgCreateTestRtn);


    }


    @Test
    public void HttpGetTest() throws Exception{
        String httpOrgCreateTestRtn = httpClientUtil.doGet("https://25.11.3.23:8088/deviceManager/rest/2102351PXS9WKA800003/filesystem", charset);
        System.out.println(httpOrgCreateTestRtn);
    }
    @Test
    public void HttpDeleteTest() throws Exception{
        String httpOrgCreateTestRtn = httpClientUtil.doDelete("https://25.11.3.23:8088/deviceManager/rest/2102351PXS9WKA800003/sessions", charset);
        System.out.println(httpOrgCreateTestRtn);
    }
}
@Date: 2021/1/21 18:04
* @Description:
*/
public class SSLClient  extends DefaultHttpClient {
    public SSLClient() throws Exception {
        super();
        //传输协议需要根据自己的判断
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }


            @Override
            public void checkServerTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }


            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }


    public String doPost(String url, String map, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            //设置参数
            httpPost.addHeader("Accept", "application/json");
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity stringEntity = new StringEntity(map);
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");
            httpPost.setEntity(stringEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public String doPut(String url, String map, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpPut httpPut = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpPut = new HttpPut(url);
            //设置参数
            httpPut.addHeader("Accept", "application/json");
            httpPut.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpPut.addHeader("iBaseToken", "3285EA7E1D79955A1C832F57AFE0ABC7EDEBD7B1A4D5CFE14A1887C27160EFA6");
            httpPut.addHeader(new BasicHeader("Cookie", "session=ismsession=1708491774373951553490536926597163443211973631094874730003"));
            StringEntity stringEntity = new StringEntity(map);
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");
            httpPut.setEntity(stringEntity);
            HttpResponse response = httpClient.execute(httpPut);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public String doGet(String url,  String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpGet httpGet = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpGet = new HttpGet(url);
            //设置参数
            httpGet.addHeader("Accept", "application/json");
            httpGet.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpGet.addHeader("iBaseToken", "DD6452F8C28C8B6C4DA09F10DBBD8F0AEB679C8C0527F1C130A3611CF7C788EE");
            httpGet.addHeader(new BasicHeader("Cookie", "session=ismsession=1211398665627844242134562708694152040389047758107797599492"));
            HttpResponse response = httpClient.execute(httpGet);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
    public String doDelete(String url, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpDelete httpDelete = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpDelete = new HttpDelete(url);
            //设置参数
            httpDelete.addHeader("Accept", "application/json");
            httpDelete.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpDelete.addHeader("iBaseToken", "8C2D9B310A6039D6382A0F675B9B0A09680975E0E99FC3783A41AAF9D8862A1F");
            httpDelete.addHeader(new BasicHeader("Cookie", "session=ismsession=476387060735095844496830122778384587051871883735873158400"));
            HttpResponse response = httpClient.execute(httpDelete);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }




}
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.junit.Test;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
* @author: zhaochunhui
* @Date: 2021/1/21 18:06
* @Description:
*/
public class test {




    private String url = "https://25.11.3.23:8088/deviceManager/rest/xxx/sessions";
    private String charset = "utf-8";
    private SSLClient httpClientUtil; {
        try {
            httpClientUtil = new SSLClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    };






    public static String md5(String text) {
        String result="";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(text.getBytes("UTF-8"));
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
//       System.out.println("result: " + buf.toString());// 32位的加密
//       System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
        }
        return result;
    }
    @Test
    public void HttpsPostTest() throws Exception {
        String ver = "1.0";
        String msgId = "91b024e3-06ca-4a79-9993-1472d0fdb973";
        String appId = "300011853779";
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String appKey = "A0702827F21C9CC7DDC93AEF24B6B16B";
        String sign = md5(ver + appId + msgId + timestamp + appKey).toUpperCase();
        JSONObject result = new JSONObject();
        result.put("username", "CMPuser");
        result.put("password", "Ceb@12345");
        result.put("scope", "0");


        String encryptStr = result.toString();
        System.out.println("encryptStr:" + encryptStr);
        String httpOrgCreateTestRtn = httpClientUtil.doPost(url, encryptStr, charset);
        System.out.println("result:" + httpOrgCreateTestRtn);


    }
    @Test
    public void HttpsPutTest() throws Exception {
        JSONObject result = new JSONObject();
        result.put("username", "CMPuser");
        result.put("password", "Ceb@12345");
        result.put("scope", "0");


        String encryptStr = result.toString();
        System.out.println("encryptStr:" + encryptStr);
        String httpOrgCreateTestRtn = httpClientUtil.doPut("https://25.11.3.23:8088/deviceManager/rest/2102351PXS9WKA800003/offline_user/CMPuser", encryptStr, charset);
        System.out.println("result:" + httpOrgCreateTestRtn);


    }


    @Test
    public void HttpGetTest() throws Exception{
        String httpOrgCreateTestRtn = httpClientUtil.doGet("https://25.11.3.23:8088/deviceManager/rest/2102351PXS9WKA800003/filesystem", charset);
        System.out.println(httpOrgCreateTestRtn);
    }
    @Test
    public void HttpDeleteTest() throws Exception{
        String httpOrgCreateTestRtn = httpClientUtil.doDelete("https://25.11.3.23:8088/deviceManager/rest
    }
}

 

HTTPS的GET、POST、PUT、DELETE请求

上一篇:web端滑动页面查找元素


下一篇:spring之配置C3P0实例