(android)用ksoap2连接webservice

祝灼光
2023-12-01

private void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] {
        new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }
            public void checkClientTrusted(X509Certificate[] chain,
                String authType) throws CertificateException {
            }
            public void checkServerTrusted(X509Certificate[] chain,
                String authType) throws CertificateException {
            }
        }
    };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

String inputStream2String(InputStream is) {
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    StringBuffer buffer = new StringBuffer();
    String line = "";
    try {
        while ((line = in .readLine()) != null) {
            buffer.append(line);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return buffer.toString();
}

private String getDataFromHTTPSUrl(String url) {
    String data = "";
    InputStream is = null;
    HttpURLConnection http = null;
    HttpsURLConnection https = null;
    try {
        URL obj = new URL(url);
        if (obj.getProtocol().toLowerCase().equals("https")) {
            trustAllHosts();
            https = (HttpsURLConnection) obj.openConnection();
            https.setHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            http = https;
        } else {
            http = (HttpURLConnection) obj.openConnection();
        }

        http.setRequestMethod("GET");
        //	 http.setRequestProperty("Content-Type",
        //	 "application/x-www-form-urlencoded");
        http.setRequestProperty("Content-Length", " + Integer.toString(url.getBytes().length));
        http.setRequestProperty("Content-Language", "en-US");
        //	 http.setRequestProperty("Connection", "close");
        http.setUseCaches(false);
        http.setDoOutput(true);
        //	 http.setDoInput(true);
        http.setConnectTimeout(100000);
        http.setReadTimeout(100000);
        is = http.getInputStream();
        data = inputStream2String(is);
    } catch (Exception e) {
        // TODO: handle exception
        data = null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //	 http.disconnect();
        //	 https.disconnect();
    }
    return data;
}


 类似资料: