Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. String jsonResult = HttpsPoster.post(
  2. "https://sandbox-api.100credit.cn/bankServer2/user/login.action",
  3. "userName=" + "1" + "&password=" + "1" +
  4. "&apiCode=" + "test");
  5.  
  6. java.net.NoRouteToHostException: No route to host
  7. at java.net.PlainSocketImpl.socketConnect(Native Method)
  8. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
  9. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
  10. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
  11. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  12. at java.net.Socket.connect(Socket.java:579)
  13. at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
  14. at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:532)
  15. at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:409)
  16. ...
  17.  
  18. <form action="https://sandbox-api.100credit.cn/bankServer2/user/login.action"
  19. method="post">
  20. 账号: <input type="text" name="userName" /> <br/>
  21. 密码: <input type="text" name="password" /> <br/>
  22. <input type="submit" value="提交" />
  23. </form>
  24.  
  25. public class HttpsPoster {
  26. public static KeyStore getKeyStore( String password, String keyStorePath )
  27. throws Exception
  28. {
  29. KeyStore ks = KeyStore.getInstance( "JKS" );
  30. InputStream is =
  31. HttpsPoster.class.getClassLoader().getResourceAsStream( keyStorePath );
  32. ks.load( is, password.toCharArray() );
  33. is.close();
  34. return(ks);
  35. }
  36.  
  37.  
  38. /*
  39. * / **
  40. * * 获取keystore
  41. * *
  42. * * @param file
  43. * * @param password
  44. * * @return
  45. * * @throws Exception
  46. * * /
  47. * private static KeyStore getKeyStore(File file, String password)
  48. * throws Exception {
  49. * InputStream in = new FileInputStream(file);
  50. * //InputStream
  51. * in=HttpsPoster.class.getClassLoader().getResourceAsStream("tomcat.keystore");
  52. * KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
  53. * char[] passphrase = password.toCharArray();
  54. * ks.load(in, passphrase);
  55. * in.close();
  56. * return ks;
  57. * }
  58. */
  59. public static SSLContext getSSLContext( String password, String keyStorePath,
  60. String trustStorePath ) throws Exception
  61. {
  62. KeyManagerFactory keyManagerFactory =
  63. KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
  64. KeyStore keyStore = getKeyStore( password, keyStorePath );
  65. keyManagerFactory.init( keyStore, password.toCharArray() );
  66. TrustManagerFactory trustManagerFactory =
  67. TrustManagerFactory.getInstance( TrustManagerFactory
  68. .getDefaultAlgorithm() );
  69. KeyStore trustStore = getKeyStore( password, trustStorePath );
  70. trustManagerFactory.init( trustStore );
  71. SSLContext ctx = SSLContext.getInstance( "TLS" );
  72. ctx.init( keyManagerFactory.getKeyManagers(),
  73. trustManagerFactory.getTrustManagers(), null );
  74. return(ctx);
  75. }
  76.  
  77.  
  78. public static void initHttpsURLConnection( String password,
  79. String keyStorePath, String trustStorePath ) throws Exception
  80. {
  81. SSLContext sslContext = null;
  82. HostnameVerifier hnv = new MyHostnameVerifier();
  83. try {
  84. sslContext = getSSLContext( password, keyStorePath, trustStorePath );
  85. } catch ( GeneralSecurityException e ) {
  86. e.printStackTrace();
  87. }
  88. if ( sslContext != null )
  89. {
  90. HttpsURLConnection.setDefaultSSLSocketFactory( sslContext
  91. .getSocketFactory() );
  92. }
  93. HttpsURLConnection.setDefaultHostnameVerifier( hnv );
  94. }
  95.  
  96.  
  97. public static String post( String url, String post_str ) throws Exception
  98. {
  99. /* System.out.println("data:"+post_str); */
  100. HttpsURLConnection urlCon = null;
  101. System.out.println( "=====================1=========" + url );
  102. @SuppressWarnings( "restriction" )
  103. URL url1 = new URL( null, url, new sun.net.www.protocol.https.Handler() );
  104. System.out.println( "=====================2=========" + url1.getPath() );
  105. urlCon = (HttpsURLConnection) url1.openConnection();
  106. urlCon.setDoInput( true );
  107. urlCon.setDoOutput( true );
  108. urlCon.setRequestMethod( "POST" );
  109. urlCon.setRequestProperty( "Content-Length",
  110. String.valueOf( post_str.getBytes().length ) );
  111. urlCon.setUseCaches( false );
  112. urlCon.getOutputStream().write( post_str.getBytes( "UTF-8" ) );
  113. urlCon.getOutputStream().flush();
  114. urlCon.getOutputStream().close();
  115. BufferedReader in =
  116. new BufferedReader( new InputStreamReader( urlCon.getInputStream(),
  117. "utf-8" ) );
  118. String line;
  119. StringBuffer sb = new StringBuffer();
  120. while ( (line = in.readLine() ) != null )
  121. {
  122. sb.append( line );
  123. }
  124. return(sb.toString() );
  125. }
  126.  
  127.  
  128. static class MyHostnameVerifier implements HostnameVerifier {
  129. public boolean verify( String hostname, SSLSession session )
  130. {
  131. /*
  132. * if ("localhost".equals(hostname)) {
  133. * return true;
  134. * } else {
  135. * return false;
  136. * }
  137. */
  138. return(true);
  139. }
  140. }
  141. public static class SavingTrustManager implements X509TrustManager {
  142. private final X509TrustManager tm;
  143. private X509Certificate[] chain;
  144. public SavingTrustManager( X509TrustManager tm )
  145. {
  146. this.tm = tm;
  147. }
  148.  
  149.  
  150. public X509TrustManager getTM()
  151. {
  152. return(tm);
  153. }
  154.  
  155.  
  156. public X509Certificate[] getChain()
  157. {
  158. return(chain);
  159. }
  160.  
  161.  
  162. public X509Certificate[] getAcceptedIssuers()
  163. {
  164. throw new UnsupportedOperationException();
  165. }
  166.  
  167.  
  168. public void checkClientTrusted( X509Certificate[] chain, String authType )
  169. throws CertificateException
  170. {
  171. throw new UnsupportedOperationException();
  172. }
  173.  
  174.  
  175. public void checkServerTrusted( X509Certificate[] chain, String authType )
  176. throws CertificateException
  177. {
  178. this.chain = chain;
  179. tm.checkServerTrusted( chain, authType );
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement