1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.List;
import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.security.cert.X509Certificate;
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class HttpsTest {
// public static void main(String[] args) throws Exception { @RequestMapping("/tachyontest.do") public void test() throws NoSuchAlgorithmException, KeyManagementException, ClientProtocolException, IOException{ HttpClient httpclient = new DefaultHttpClient();
TrustManager easyTrustManager = new X509TrustManager() { @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { // TODO Auto-generated method stub return null; } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } @Override public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { // TODO Auto-generated method stub } }; //. HttpClientÀÇ °æ¿ì X509TrustManager¸¦ ÀÌ¿ëÇØ SSLContext¸¦ »ý¼ºÇÏ°í //ClientConnectionManager¿Í SchemeRegistry¿¡ SSLSocketFactory¸¦ µî·ÏÇØ ÁØ´Ù. try { SSLContext sslcontext = SSLContext.getInstance("TLS");//SSLContext ÁöÁ¤µÈ ½ÃÅ¥¾î ¼ÒÄÏ ÇÁ·ÎÅäÄÝ ±¸Çö sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext,SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
// SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext, // SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // ¸¸¾à º»ÀÎ ÀÎÁõ ¹æ½Ä (Self-Signed Certificate)ÀÏ °æ¿ì´Â ¡°SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER¡±¼±¾ðµÈ Äڵ带 // ÁÖ¼®ÇØÁ¦ ÇÏ½Ã¸é µË´Ï´Ù. ¹°·Ð ¹Ù·Î À§ÀÇ ¡± SSLSocketFactory.STRICT_HOSTNAME_VERIFIER¡±ÄÚµå´Â ÁÖ¼® ó¸® Scheme sch = new Scheme("https", 443, socketFactory);//SSL±âº»Æ÷Æ® : 443 httpclient.getConnectionManager().getSchemeRegistry().register(sch);
// HttpGet httpget = new HttpGet("https://msp.f-secure.com/web-test/common/test.html"); // System.out.println("executing request" + httpget.getRequestLine()); // HttpResponse response = httpclient.execute(httpget);
//HttpClient timeout ¼ÂÆà httpclient.getParams().setParameter("http.protocol.expect-continue", false);//HttpClient POST ¿äû½Ã Expect Çì´õÁ¤º¸ »ç¿ë x httpclient.getParams().setParameter("http.connection.timeout", 3 * 1000);// ¿ø°Ý È£½ºÆ®¿Í ¿¬°áÀ» ¼³Á¤ÇÏ´Â ½Ã°£ httpclient.getParams().setParameter("http.socket.timeout", 3 * 1000);//µ¥ÀÌÅ͸¦ ±â´Ù¸®´Â ½Ã°£ httpclient.getParams().setParameter("http.connection-manager.timeout", 3 * 1000);// ¿¬°á ¹× ¼ÒÄÏ ½Ã°£ ÃÊ°ú httpclient.getParams().setParameter("http.protocol.head-body-timeout", 3 * 1000); String url = "ÇØ´ç¼¹ö url"; //server url (https·Î µÇ¾îÀÖ´Â) HttpPost httppost = new HttpPost(url); System.out.println("executing request" + httppost.getRequestLine());
String sampleJson = "[" + "{" + "\"filesize\": \"1398259\"," + "\"crc32_1\": \"0x00001226\"," + "\"crc32_2\": \"0x2A49AB1A\"," + "\"crc32_3\": \"0x3B642DBE\"," + "\"crc32_4\": \"0x00000C91\"," + "\"ukey\": \"053F\"" + "}," + "{" + "\"filesize\": \"880114\"," + "\"crc32_1\": \"0x0000144C\"," + "\"crc32_2\": \"0x67D8725E\"," + "\"crc32_3\": \"0xE212B08C\"," + "\"crc32_4\": \"0x00005F5C\"," + "\"ukey\": \"407F\"" + "}" + "]";
List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("json",sampleJson)); UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(qparams,"UTF-8"); httppost.setEntity(entity1); HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(response.getEntity(),"UTF-8");
System.out.println(responseBody);
System.out.println("————————————————————"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: "+ entity.getContentLength()); } EntityUtils.consume(entity);
} finally { //ApacheÀÇ HttpClient ¶óÀ̹ö·¯¸®¿¡¼´Â À¥ ¼¹ö¸¦ ¿¬°áÀ» ÇÏ°í ³ÈÄ ¸í½ÃÀûÀ¸·Î ²÷´Â ºÎºÐÀÌ ¾øÀ» °æ¿ì ´ë±â »óÅ·Π³Ñ¾î°¡¸ç, //ÀÌ·± ´ë±â°¡ ¿©·¯°³ ¹ß»ýÇÏ°Ô µÇ¸é À¥¼¹öÀÇ ³×Æ®¿öÅ© ¸®¼Ò½º¿¡ ´©¼ö°¡ ¹ß»ýÇÒ ¼ö°¡ ÀÖ´Ù. httpclient.getConnectionManager().shutdown();//<-httpclientÀÇ Á¢¼Ó ²÷±â. }
}
}
|