【备份】Java实现忽略SSL认证请求https

下面的代码均是测试OK的,商用请注意。

Android

看代码

    public static String getHttpsContent(String url) {
        String str2 = "";
        try {
            HttpClient a = getHttpClient();
            HttpUriRequest httpGet = new HttpGet(url.replace(" ", ""));
//            httpGet.setHeader("User-Agent", "");
            HttpResponse execute = a.execute(httpGet);
            if (execute.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(execute.getEntity());
            }
            return "error";
        } catch (Exception e) {
            e.printStackTrace();
            return str2;
        }
    }

    public static HttpClient getHttpClient() {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 8443));
        HttpParams basicHttpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(basicHttpParams, u);
        HttpConnectionParams.setSoTimeout(basicHttpParams, u);
        return new DefaultHttpClient(new ThreadSafeClientConnManager(basicHttpParams, schemeRegistry), basicHttpParams);
    }

gradle中需要设置

useLibrary 'org.apache.http.legacy'

Java SE

下面这个代码是抄的别人的,只是用于实验。

    public static String sendXMLDataByGet(String url, String xml) {
        // 创建HttpClient实例     
        if (client == null) {
            // Create HttpClient Object
            client = new DefaultHttpClient();
            enableSSL(client);
        }
        StringBuilder urlString = new StringBuilder();
        urlString.append(url);
        if(xml != null){
            urlString.append("?");
            System.out.println("getUTF8XMLString(xml):" + getUTF8XMLString(xml));
            try {
                urlString.append(URLEncoder.encode(getUTF8XMLString(xml), "UTF-8"));
            } catch (UnsupportedEncodingException e2) {            
                e2.printStackTrace();
            }
        }

        String urlReq = urlString.toString();
        // 创建Get方法实例     
        HttpGet httpsgets = new HttpGet(urlReq);
       // httpsgets.addHeader("Cookie", "");
        String strRep = "";
        try {
            HttpResponse response = client.execute(httpsgets);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                strRep = EntityUtils.toString(response.getEntity());
                // Do not need the rest    
                httpsgets.abort();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strRep;
    }
    /**
     * 访问https的网站
     *
     * @param httpclient
     */
    private static void enableSSL(DefaultHttpClient httpclient) {
        //调用ssl  
        try {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[]{truseAllManager}, null);
            SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme https = new Scheme("https", sf, 443);
            httpclient.getConnectionManager().getSchemeRegistry().register(https);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // test url
    public static void main(String[] args) {
        String urlString = "https://www.xxx.com:8443/Q?Info=All";
        String output = new String(sendXMLDataByGet(urlString.replace(" ", ""),null));
        System.out.println(output);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值