尝试将我的android应用程序转换为使用SSL在我的android应用程序和Web服务器之间传输信息后,出现一些问题。 (SocketTimeOutException)在android应用程序中将http转换为https后的SocketTimeoutException
我已从证书颁发机构(CA)购买了Positive SSL证书,并配置了我的服务器以正确使用它。我已经在我的网络浏览器中测试过它,并且它的工作正常。
现在我试图修改我的android应用程序使用https而不是http,但由于这是我第一次使用https自己,我有点困惑,我需要在java代码中实现哪些步骤。
目前我正在设置我的设置页面,在该页面中我将应用程序配置为使用正确的URL。例如,我在一个活动中有2个文本字段,我在其中输入网站URL(http://www.mydomain.com)和相关页面存储在的应用程序文件夹(myfolfolder)
然后我使用以下代码连接和测试连接变量配置正确,其中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。任何想法我做错了什么?
OK -to-root-your-samsung-galaxy-ace-37385 /然后从google play上下载并使用应用主机编辑器。我能够编辑我的主机文件并添加我的网络服务器的IP和别名。 这工作很好,现在我可以在我的内部网络上使用https连接到网络服务器 – 2012-07-06 14:59:48