gpt4 book ai didi

android - 如何使用httpclient从android调用https安全休息服务

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:44 26 4
gpt4 key购买 nike

我已经在 Android 中使用 http 成功调用了 rest 服务。

但是现在我想在android中调用HTTPS restful服务我正在使用 apache 的 HTTPmime API 来调用 rest 服务。

请任何人可以帮助我调用 android 中的安全休息服务

最佳答案

这是您可以用来忽略 SSL 证书错误的示例代码,创建一个名为 SimpleSSLSocketFactory 的新 java 类

  public class SimpleSSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory {
private SSLSocketFactory sslFactory = HttpsURLConnection.getDefaultSSLSocketFactory();

public SimpleSSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException,
UnrecoverableKeyException {
super(null);

try {
SSLContext context = SSLContext.getInstance("TLS");

// Create a trust manager that does not validate certificate chains and simply
// accept all type of certificates
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 {
}
} };

// Initialize the socket factory
context.init(null, trustAllCerts, new SecureRandom());
sslFactory = context.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslFactory.createSocket(socket, host, port, autoClose);
}

@Override
public Socket createSocket() throws IOException {
return sslFactory.createSocket();
}
}

并使用以下 httpclient 而不是默认客户端

// Setup a custom SSL Factory object which simply ignore the certificates
// validation and accept all type of self signed certificates
SSLSocketFactory sslFactory = new SimpleSSLSocketFactory(null);
sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

// Enable HTTP parameters
HttpParams paramsSecure = new BasicHttpParams();
HttpProtocolParams.setVersion(paramsSecure, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(paramsSecure, HTTP.UTF_8);

// Register the HTTP and HTTPS Protocols. For HTTPS, register our custom SSL Factory object.
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sslFactory, 443));

// Create a new connection manager using the newly created registry and then create a new HTTP client
// using this connection manager
ClientConnectionManager ccm = new ThreadSafeClientConnManager(paramsSecure, registry);
HttpClient httpclient = new DefaultHttpClient(ccm, paramsSecure);

干杯!

关于android - 如何使用httpclient从android调用https安全休息服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15805079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com