嘗試將我的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