如何在Android上通过基本的SSL认证连接到Http服务器?
我知道以前有人问过这个问题,但我发现没有一个答案对我有帮助。我正在尝试使用基本Http身份验证通过SSL连接到一个简单的登录服务。该服务的主机位于。当我从浏览器(在windows、OSX和Android上)点击该服务时,它工作正常,我输入用户名和密码,并正确验证。然而,当我试图通过Xoom上的代码来实现这一点时,我得到了一个ClientProtocolException,它表示“服务器未能使用有效的HTTP响应进行响应”。有人能帮我往正确的方向推吗?这是我的密码如何在Android上通过基本的SSL认证连接到Http服务器?,android,ssl,basic-authentication,Android,Ssl,Basic Authentication,我知道以前有人问过这个问题,但我发现没有一个答案对我有帮助。我正在尝试使用基本Http身份验证通过SSL连接到一个简单的登录服务。该服务的主机位于。当我从浏览器(在windows、OSX和Android上)点击该服务时,它工作正常,我输入用户名和密码,并正确验证。然而,当我试图通过Xoom上的代码来实现这一点时,我得到了一个ClientProtocolException,它表示“服务器未能使用有效的HTTP响应进行响应”。有人能帮我往正确的方向推吗?这是我的密码 String r
String result = null;
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
DefaultHttpClient client = new DefaultHttpClient(conMgr, params);
HttpGet get = new HttpGet(preferences.getString(getString(R.string.Security_Login_URL),
"https://localhost:80/login"));
String credentials = Base64.encodeToString((username + ":" + password).getBytes(), Base64.URL_SAFE);
if(credentials!=null){
get.addHeader("Authorization","Basic "+credentials);
ResponseHandler responseHandler=new BasicResponseHandler();
try {
result = client.execute(get, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
两个常见问题可能会导致这种情况: