我想将我的Android应用程序使用SSL来我的Android应用程序和Web服务器之间传输的信息后,得到了一些问题。 (SocketTimeOutException)
我已经买了从证书颁发机构(CA)一个积极的SSL证书和配置我的服务器与它正常工作。 我在Web浏览器中测试它和它的正常工作。
现在我想修改我的Android应用使用https而非http,但因为这是我第一次使用HTTPS我自己,我以什么步骤,我需要在Java代码来实现有点困惑。
目前,我的工作我的设置页面,配置我的应用程序来使用正确的URL链接。 比如我有,我进入网站的URL的活动(http://www.mydomain.com)和应用程序文件夹中的相关网页存储在两个文本字段(myappfolder)
然后我用下面的代码来连接和测试连接变量配置正确,其中validateurl.aspx是,如果页面存在,它返回一个JSON字符串的网页:
protected boolean validateConnectionSettings() {
String result = "";
boolean validated = false;
try {
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx");
URL url = new URL(urlBuilder.toString());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
URLConnection urlConn = url.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null){
result += inputLine;
}
in.close();
if(result.equals("exists")) {
validated = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
return validated;
}
现在,当我尝试转换(http://www.mydomain.com)变量使用https我得到上述java.net.SocketTimeoutException。
我用Google搜索这个问题,并看到了,我要在上面的代码来实现HttpsURLConnection的,而不是URLConnection的所以我相应的修改我的代码如下:
protected boolean validateConnectionSettings() {
String result = "";
boolean validated = false;
try {
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx");
URL url = new URL(urlBuilder.toString());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
// URLConnection urlConn = url.openConnection();
HttpsURLConnection urlConn = (HttpsURLConnection)url.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null){
result += inputLine;
}
in.close();
if(result.equals("exists")) {
validated = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
return validated;
}
不过,我还是收到了java.net.SocketTimeoutException。 任何想法什么林做错了什么?