Java HttpsUnits 도구 클래스 Https 구현

머리말
이 글 은 자바 의 HttpsUnits 도구 류 를 간략하게 기록 합 니 다.
자바 에서 Http 액세스 네트워크 를 실현 하 는 것 은 간단 하지만 Https 를 실현 하 는 것 은 상대 적 으로 복잡 하 다.
HttpsUnits.java
  • 코드 구름: HttpsUnits. java.
  • 개술:
  • getFile()getFileByte()POST 요청 을 통 해 네트워크 파일 을 읽 고 기록 하 는 것 만 실 현 했 을 뿐 실현 되 지 않 았 다 GET.
  • import ......
    
    /**
     * HttpsUnits    
     */
    public class HttpsUnits {
        private static final int TIMEOUT = 45000;
        public static final String ENCODING = "UTF-8";
    
        /**
         *    http   
         *    https   ,     https   ,   https   SSL   
         *
         * @param url                
         * @param method             
         * @param headerParameters    
         * @param body                 
         * @return HttpURLConnection httpConnection
         * @throws Exception     
         */
        private static HttpURLConnection createConnection(String url, String method, Map headerParameters, String body) throws Exception {}
    
        /**
         * POST  
         *
         * @param address              
         * @param headerParameters   
         * @param body
         * @return proxyHttpRequest()  headerParameters, String body) throws Exception {}
    
        /**
         * GET  
         *
         * @param address          URL  
         * @param headerParameters
         * @param body
         * @return proxyHttpRequest()  headerParameters, String body) throws Exception {}
    
        /**
         *            
         *
         * @param address          URL  
         * @param headerParameters //     * @param body
         * @param file             File  
         * @return readInputStream()  headerParameters, File file) throws Exception {}
    
        /**
         *   Byte            
         *
         * @param address String
         * @param headerParameters ap
         * @return byte[]
         * @throws Exception     
         */
        public static byte[] getFileByte(String address, Map headerParameters, File file) throws Exception {}
    
        /**
         *           
         *
         * @param in InputStream  
         * @return "success"
         * @throws Exception     
         */
        public static String readInputStream(InputStream in, File file) throws Exception {}
    
        /**
         *   Byte           
         *
         * @param in InputStream
         * @return byte[]
         * @throws Exception     
         */
        public static byte[] readInputStreamToByte(InputStream in, File file) throws Exception {}
    
        /**
         * HTTP   
         *
         * @param address            
         * @param method             
         * @param headerParameters    
         * @param body                 
         * @return inputStreamToString()  headerParameters, String body) throws Exception {}
    
        /**
         *       body,   getRequestBody(Map params, boolean urlEncode)
         *
         * @param params Map  
         * @return getRequestBody()  params) {}
    
        /**
         *       body   
         *
         * @param params    Map  
         * @param urlEncode boolean urlEncode
         * @return StringBuilder.toString()
         */
        public static String getRequestBody(Map params, boolean urlEncode) {}
    
        /**
         *    InputStream   string
         *
         * @param input    InputStream  
         * @param encoding String encoding
         * @return StringBuilder.toString()
         * @throws IOException     
         */
        private static String inputStreamToString(InputStream input, String encoding) throws IOException {}
    
        /**
         *    https   ,     https   ,   https   SSL   
         *
         * @throws Exception     
         */
        private static void trustAllHttpsCertificates() throws Exception {}
    
    
        /**
         *    https     
         */
        static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {}
    }
    
    

    테스트
  • 예시:
    import java.io.File;
    import java.util.HashMap;
    import java.util.Map;
    
    public class main {
        public static void main(String[] args) {
            try {
                // get   
                //    (         )
                System.out.println("Get:
    "); String address_get = "https://v1.hitokoto.cn/"; // Map params_get = new HashMap<>(); params_get.put("c", "d");// params_get.put("encode", "text");// // get String res = HttpsUnits.get(address_get, params_get, null); System.out.println(res);// System.out.println("

    Post:
    "); // ( ) String address_post = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm"; // Map params_post = new HashMap<>(); params_post.put("tel", "13777777777");// // post String res_post = HttpsUnits.post(address_post, params_post, null); System.out.println(res_post);// System.out.println("

    File Post Write...
    "); File file_post = new File("src/file/file_post.txt"); HttpsUnits.getFile(address_post, params_post, file_post); System.out.println("

    File Post Write...
    "); File file_post_byte = new File("src/file/file_post_byte.txt"); HttpsUnits.getFileByte(address_post, params_post, file_post_byte); } catch (Exception e) { // TODO e.printStackTrace(); } } }
  • 표시:
    Get:
        ,        。
    
    Post:
    __GetZoneResult_ = {    mts:'1377777',    province:'  ',    catName:'    ',    telString:'13777777777',	areaVid:'30510',	ispVid:'3236139',	carrier:'    '}
    
    File Post Write...
    File Post Write...
    
  • 좋은 웹페이지 즐겨찾기