自签名SSL连接 - Android

时间:2011-10-27 05:43:11

标签: android ssl

我按照here所述添加了SSLFactory和TrustManager类。

我正在建立如下连接:

//does not work
String url = "https://waprd.uark.edu/web-apps/regr/scheduleofclasses/Main?strm=1123";
  //http://bektemirov.com/a/android.php?t=amurica   //simple http - works

HttpClient client = new DefaultHttpClient(clientConnectionManager, params);
HttpGet get = new HttpGet(url);
try {
  HttpResponse response = client.execute(get, context);
  HttpEntity entity = response.getEntity();

  String responseText = EntityUtils.toString(entity);
  Body.setText(responseText);

} catch (ClientProtocolException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

此特定链接的每次连接都失败。请指点我正确的方向?

1 个答案:

答案 0 :(得分:0)

初始化SSLContext时,有一个参数可以传递TrustManager数组,您可以在其中定义自己的TrustManager以接受所有。下面列出了一些关键步骤:

  1. Subclass SSLSocketFactory

  2. 自定义TrustManager以接受SSLSocketFactory子类中的所有证书。

  3. 使用SSLSocketFactory的子类中的自定义TrustManager初始化SSLContext。

  4. 使用HttpParams和ClientConnectionManager创建参数化的DefaultHttpClient,它使用SchemeRegistry,您可以在其中使用SSLSocketFactory的子类注册“https”。

  5. 因此,所有https响应都将由您自定义的TrustManager类处理。

相关问题