HTTPS url을 그냥 전송하면 단말기에 따라서는 전송이 실패할 수 있다. 귀찮더라도 확실하게 하려면 아래처럼...
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; // . . . // always verify the host - dont check for certificate final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; /** * Trust every server - don't check for any certificate */ private static void trustAllHosts() { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[] {}; } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }}; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } } public static HttpsURLConnection postHttps(String url, int connTimeout, int readTimeout) { trustAllHosts(); HttpsURLConnection https = null; try { https = (HttpsURLConnection) new URL(url).openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); https.setConnectTimeout(connTimeout); https.setReadTimeout(readTimeout); } catch (MalformedURLException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } return https; }
출처 - http://sizuha.wikispaces.com/Android+HTTPS+post
정적함수인 DownloadHtml 가 스트링형태로 html문서내용을 리턴해줍니다. 페이지에따라 시간이 오래 걸릴수 있으니 쓰레드를 활용 하셔야 합니다.
Thread의 run 함수에 넣고 사용 하시면 됩니다.
package ukg.network.http;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import android.util.Log;
/*
작성자 : 도플광어
설명 : 안드로이드 용 html 다운로더 모듈 (https 도 지원함)
*/
public class CHttpUtil {
public static String DownloadHtml(String addr) {
StringBuilder html = new StringBuilder();
try {
URL url = new URL(addr);
//HttpURLConnection conn = (HttpURLConnection)url.openConnection();
HttpURLConnection conn = null;
if (url.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
conn = https;
} else {
conn = (HttpURLConnection) url.openConnection();
}
if (conn != null) {
conn.setConnectTimeout(10000);
conn.setUseCaches(false);
int resultcode = conn.getResponseCode();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
for (;;) {
String line = br.readLine();
if (line == null) break;
html.append(line + '\n');
}
br.close();
}
conn.disconnect();
}
}
catch (Exception ex) {
Log.i("error",ex.getMessage());
return ex.getMessage();
//System.out.println(ex.getMessage());
}
return html.toString();
}
private static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
// TODO Auto-generated method stub
return true;
}
};
}
출처 - http://cafe.naver.com/jzsdn/21091
'Android > Common' 카테고리의 다른 글
android - 자신 전화번호 확인 (0) | 2013.01.21 |
---|---|
Why do variable names often start with the letter 'm'? (0) | 2013.01.19 |
Javascript에서 Android 함수 호출하기 (1) | 2013.01.02 |
android - WebView 사용 하기 (1) | 2012.11.03 |
android - app 버전 관리 (0) | 2012.10.07 |