JSP/SERVLET
2018.03.31 / 16:22
Apache HttpClient¸¦ »ç¿ëÇÏ¿© Https ¿äûÇϱâ
º½ÀÌ
Ãßõ ¼ö 290
ÀÌ ±ÛÀº HttpClient 4.2.5 ¹öÀüÀ» ±âÁØÀ¸·Î ÀÛ¼ºµÇ¾úÀ¸¸ç, 4.3.x ¹öÀü¿¡¼´Â SSLSocketFactory°¡ deprecated µÇ¾ú°í, ´ë½Å SSLConnectionSocketFactory¸¦ »ç¿ëÇϱ⸦ ±Ç°íÇÏ°í ÀÖ´Ù.
GCM(Google Cloud Messaging)À» »ç¿ëÇÏ·Á´Ù º¸´Ï, ¿äûÀ» https·Î ó¸®ÇØ¾ß Çؼ Á¤¸®¸¦ ÇÏ°Ô µÇ¾ú´Ù. ¿©±â¼´Â ÀÎÁõ¼¸¦ °ËÁõ(verify)ÇÏÁö ¾Ê°í ¸ðµç ÀÎÁõ¼¸¦ Çã¿ëÇÏ´Â ·ÎÁ÷À» »ç¿ëÇÑ´Ù.
SSLContext sslContext = SSLContext.getInstance("TLS");
// ¸ðµç ÀÎÁõ¼¸¦ Çã¿ëÇÑ´Ù
TrustManager tm = new X509TrustManager()
{
@Override
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] cetificates, String authType) throws CertificateException
{
}
@Override
public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException
{
}
};
sslContext.init(null, new TrustManager[]{ tm }, new SecureRandom());
SSLSocketFactory sf = new SSLSocketFactory(sslContext);
// https schemeÀ» µî·ÏÇϸé¼, À§¿¡¼ »ý¼ºÇÑ SSLSocketFactory¸¦ °´Ã¼¸¦ »ç¿ëÇÑ´Ù
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", 443, sf));
// GCM¿¡ POST ¿äûÀ» º¸³½´Ù
ClientConnectionManager cm = new BasicClientConnectionManager();
HttpClient httpClient = new DefaultHttpClient(cm);
HttpPost post = new HttpPost("https://android.googleapis.com/gcm/send");
post.addHeader("Content-Type", "application/json");
post.addHeader("Authorization", "key=A");
post.setEntity(new StringEntity("{ \"registration_ids\": [ \"42\" ] }"));
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();