使用java发送https的请求详解编程语言

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.security.cert.CertificateException; 
import java.util.Map; 
 
import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLContext; 
import javax.net.ssl.SSLSession; 
import javax.net.ssl.TrustManager; 
import javax.net.ssl.X509TrustManager; 
import java.security.cert.X509Certificate; 
 
public class HttpsGetData { 
    private static class TrustAnyTrustManager implements X509TrustManager { 
         
        public void checkClientTrusted(X509Certificate[] chain, String authType) 
                throws CertificateException { 
        } 
  
        public void checkServerTrusted(X509Certificate[] chain, String authType) 
                throws CertificateException { 
        } 
  
        public X509Certificate[] getAcceptedIssuers() { 
            return new X509Certificate[] {}; 
        } 
    } 
  
    private static class TrustAnyHostnameVerifier implements HostnameVerifier { 
        public boolean verify(String hostname, SSLSession session) { 
            return true; 
        } 
    } 
    String _url=""; 
     
    Map<String,String> _params; 
     
    public HttpsGetData(String url,Map<String,String> keyValueParams) 
    { 
        this._url=url; 
        this._params=keyValueParams; 
    } 
    public String Do() throws Exception 
    { 
         String result = ""; 
         BufferedReader in = null; 
            try { 
                 
                String urlStr = this._url + "&" + getParamStr(); 
                System.out.println("GET请求的URL为:"+urlStr); 
                SSLContext sc = SSLContext.getInstance("SSL"); 
                sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, 
                         new java.security.SecureRandom()); 
                URL realUrl = new URL(urlStr); 
                // 打开和URL之间的连接 
                HttpsURLConnection connection = (HttpsURLConnection) realUrl.openConnection(); 
                //设置https相关属性 
                connection.setSSLSocketFactory(sc.getSocketFactory()); 
                connection.setHostnameVerifier(new TrustAnyHostnameVerifier()); 
                connection.setDoOutput(true); 
                 
                // 设置通用的请求属性 
                connection.setRequestProperty("accept", "*/*"); 
                connection.setRequestProperty("connection", "Keep-Alive"); 
                connection.setRequestProperty("user-agent", 
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 
                // 建立实际的连接 
                connection.connect(); 
                 
                // 定义 BufferedReader输入流来读取URL的响应 
                in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); 
                String line; 
                while ((line = in.readLine()) != null) { 
                    result += line; 
                } 
                System.out.println("获取的结果为:"+result); 
            } catch (Exception e) { 
                System.out.println("发送GET请求出现异常!" + e); 
                //e.printStackTrace(); 
                throw e; 
            } 
            // 使用finally块来关闭输入流 
            finally { 
                try { 
                    if (in != null) { 
                        in.close(); 
                    } 
                } catch (Exception e2) { 
                    //e2.printStackTrace(); 
                    throw e2; 
                } 
            } 
            return result; 
     
    } 
 
    private String getParamStr() 
    { 
        String paramStr=""; 
        // 获取所有响应头字段 
        Map<String, String> params = this._params; 
        // 获取参数列表组成参数字符串 
        for (String key : params.keySet()) { 
            paramStr+=key+"="+params.get(key)+"&"; 
        } 
        //去除最后一个"&" 
        paramStr=paramStr.substring(0, paramStr.length()-1); 
         
        return paramStr; 
    } 
} 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10155.html

(0)
上一篇 2021年7月19日 10:00
下一篇 2021年7月19日 10:00

相关推荐

发表回复

登录后才能评论

WordPress 数据库错误: [Duplicate entry '81-7bc5f87d83893fa5db53603967a72d35' for key 'task_id_source_url_key']
insert into wp_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(81,'http://www.52im.net/forum.php?mod=collection&action=view&ctid=16&page=3','7bc5f87d83893fa5db53603967a72d35',1735268186,0,0)