Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String jsonResult = HttpsPoster.post(
- "https://sandbox-api.100credit.cn/bankServer2/user/login.action",
- "userName=" + "1" + "&password=" + "1" +
- "&apiCode=" + "test");
- java.net.NoRouteToHostException: No route to host
- at java.net.PlainSocketImpl.socketConnect(Native Method)
- at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
- at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
- at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
- at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
- at java.net.Socket.connect(Socket.java:579)
- at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
- at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:532)
- at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:409)
- ...
- <form action="https://sandbox-api.100credit.cn/bankServer2/user/login.action"
- method="post">
- 账号: <input type="text" name="userName" /> <br/>
- 密码: <input type="text" name="password" /> <br/>
- <input type="submit" value="提交" />
- </form>
- public class HttpsPoster {
- public static KeyStore getKeyStore( String password, String keyStorePath )
- throws Exception
- {
- KeyStore ks = KeyStore.getInstance( "JKS" );
- InputStream is =
- HttpsPoster.class.getClassLoader().getResourceAsStream( keyStorePath );
- ks.load( is, password.toCharArray() );
- is.close();
- return(ks);
- }
- /*
- * / **
- * * 获取keystore
- * *
- * * @param file
- * * @param password
- * * @return
- * * @throws Exception
- * * /
- * private static KeyStore getKeyStore(File file, String password)
- * throws Exception {
- * InputStream in = new FileInputStream(file);
- * //InputStream
- * in=HttpsPoster.class.getClassLoader().getResourceAsStream("tomcat.keystore");
- * KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
- * char[] passphrase = password.toCharArray();
- * ks.load(in, passphrase);
- * in.close();
- * return ks;
- * }
- */
- public static SSLContext getSSLContext( String password, String keyStorePath,
- String trustStorePath ) throws Exception
- {
- KeyManagerFactory keyManagerFactory =
- KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
- KeyStore keyStore = getKeyStore( password, keyStorePath );
- keyManagerFactory.init( keyStore, password.toCharArray() );
- TrustManagerFactory trustManagerFactory =
- TrustManagerFactory.getInstance( TrustManagerFactory
- .getDefaultAlgorithm() );
- KeyStore trustStore = getKeyStore( password, trustStorePath );
- trustManagerFactory.init( trustStore );
- SSLContext ctx = SSLContext.getInstance( "TLS" );
- ctx.init( keyManagerFactory.getKeyManagers(),
- trustManagerFactory.getTrustManagers(), null );
- return(ctx);
- }
- public static void initHttpsURLConnection( String password,
- String keyStorePath, String trustStorePath ) throws Exception
- {
- SSLContext sslContext = null;
- HostnameVerifier hnv = new MyHostnameVerifier();
- try {
- sslContext = getSSLContext( password, keyStorePath, trustStorePath );
- } catch ( GeneralSecurityException e ) {
- e.printStackTrace();
- }
- if ( sslContext != null )
- {
- HttpsURLConnection.setDefaultSSLSocketFactory( sslContext
- .getSocketFactory() );
- }
- HttpsURLConnection.setDefaultHostnameVerifier( hnv );
- }
- public static String post( String url, String post_str ) throws Exception
- {
- /* System.out.println("data:"+post_str); */
- HttpsURLConnection urlCon = null;
- System.out.println( "=====================1=========" + url );
- @SuppressWarnings( "restriction" )
- URL url1 = new URL( null, url, new sun.net.www.protocol.https.Handler() );
- System.out.println( "=====================2=========" + url1.getPath() );
- urlCon = (HttpsURLConnection) url1.openConnection();
- urlCon.setDoInput( true );
- urlCon.setDoOutput( true );
- urlCon.setRequestMethod( "POST" );
- urlCon.setRequestProperty( "Content-Length",
- String.valueOf( post_str.getBytes().length ) );
- urlCon.setUseCaches( false );
- urlCon.getOutputStream().write( post_str.getBytes( "UTF-8" ) );
- urlCon.getOutputStream().flush();
- urlCon.getOutputStream().close();
- BufferedReader in =
- new BufferedReader( new InputStreamReader( urlCon.getInputStream(),
- "utf-8" ) );
- String line;
- StringBuffer sb = new StringBuffer();
- while ( (line = in.readLine() ) != null )
- {
- sb.append( line );
- }
- return(sb.toString() );
- }
- static class MyHostnameVerifier implements HostnameVerifier {
- public boolean verify( String hostname, SSLSession session )
- {
- /*
- * if ("localhost".equals(hostname)) {
- * return true;
- * } else {
- * return false;
- * }
- */
- return(true);
- }
- }
- public static class SavingTrustManager implements X509TrustManager {
- private final X509TrustManager tm;
- private X509Certificate[] chain;
- public SavingTrustManager( X509TrustManager tm )
- {
- this.tm = tm;
- }
- public X509TrustManager getTM()
- {
- return(tm);
- }
- public X509Certificate[] getChain()
- {
- return(chain);
- }
- public X509Certificate[] getAcceptedIssuers()
- {
- throw new UnsupportedOperationException();
- }
- public void checkClientTrusted( X509Certificate[] chain, String authType )
- throws CertificateException
- {
- throw new UnsupportedOperationException();
- }
- public void checkServerTrusted( X509Certificate[] chain, String authType )
- throws CertificateException
- {
- this.chain = chain;
- tm.checkServerTrusted( chain, authType );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement