java:http-test
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| java:http-test [2010/04/13 12:54] – percy | java:http-test [2016/05/05 13:07] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== 说明 ====== | ||
| + | 这个测试文件, | ||
| + | |||
| + | 以及如何post数据. | ||
| + | |||
| + | |||
| + | ====== HttpTest.java ====== | ||
| + | |||
| + | |||
| + | <file java HttpTest.java> | ||
| + | |||
| + | import java.io.BufferedReader; | ||
| + | import java.io.IOException; | ||
| + | import java.io.InputStreamReader; | ||
| + | import java.io.OutputStreamWriter; | ||
| + | import java.io.PrintWriter; | ||
| + | import java.net.MalformedURLException; | ||
| + | import java.net.URL; | ||
| + | import java.net.URLConnection; | ||
| + | import java.net.URLEncoder; | ||
| + | import java.nio.charset.Charset; | ||
| + | |||
| + | import javax.net.ssl.HostnameVerifier; | ||
| + | import javax.net.ssl.HttpsURLConnection; | ||
| + | import javax.net.ssl.SSLSession; | ||
| + | |||
| + | /** | ||
| + | * A test class for how to get data from {@link # | ||
| + | * {@link # | ||
| + | | ||
| + | * @author Percy.Peng | ||
| + | * @see # | ||
| + | * @see # | ||
| + | * @see # | ||
| + | * @see #setProxy() | ||
| + | */ | ||
| + | public class HttpTest { | ||
| + | public static final String httpURL = " | ||
| + | |||
| + | public static final String httpsURL = " | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | System.out.println(" | ||
| + | setProxy(); | ||
| + | |||
| + | String httpHtml = getHttpData(httpURL); | ||
| + | String httpsHtml = getHttpsData(httpsURL); | ||
| + | |||
| + | System.out.println(httpHtml); | ||
| + | System.out.println(httpsHtml); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Set Proxy here, | ||
| + | */ | ||
| + | public static void setProxy() { | ||
| + | // Set Http Proxy | ||
| + | System.getProperties().setProperty(" | ||
| + | System.getProperties().setProperty(" | ||
| + | |||
| + | // Set Https Proxy | ||
| + | // FIXME Should import the certificates to avoid the trust problem. | ||
| + | // To avoid the certification problems, | ||
| + | SSLUtilities.trustAllHttpsCertificates(); | ||
| + | System.getProperties().setProperty(" | ||
| + | System.getProperties().setProperty(" | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Post a data to the url | ||
| + | | ||
| + | * @param urlString a URL String | ||
| + | * @param data The data to be posted | ||
| + | */ | ||
| + | private static void post(String urlString, String data) { | ||
| + | try { // Construct data | ||
| + | String encodedData = URLEncoder.encode(data, | ||
| + | |||
| + | // Send data | ||
| + | URL url = new URL(urlString); | ||
| + | URLConnection conn = url.openConnection(); | ||
| + | conn.setDoOutput(true); | ||
| + | OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); | ||
| + | wr.write(encodedData); | ||
| + | wr.flush(); | ||
| + | // Get the response | ||
| + | BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
| + | String line; | ||
| + | while ((line = rd.readLine()) != null) { | ||
| + | // Process line... | ||
| + | } | ||
| + | wr.close(); | ||
| + | rd.close(); | ||
| + | } catch (Exception e) { | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private static String LOGTAG = " | ||
| + | |||
| + | private static String UA = " | ||
| + | |||
| + | // private static String URL = " | ||
| + | |||
| + | /** | ||
| + | * Get data(Html) from a http url | ||
| + | | ||
| + | * @param url A url String | ||
| + | * @return The contents for the url or null if failed | ||
| + | */ | ||
| + | public static String getHttpData(String urlString) { | ||
| + | String result = null; | ||
| + | |||
| + | try { | ||
| + | URL url = new URL(urlString); | ||
| + | URLConnection conn = url.openConnection(); | ||
| + | |||
| + | conn.setDoOutput(true); | ||
| + | // conn.setRequestProperty(" | ||
| + | InputStreamReader isr = new InputStreamReader(conn.getInputStream()); | ||
| + | readFromInputStream(isr); | ||
| + | |||
| + | } catch (IOException e) { | ||
| + | e.printStackTrace(); | ||
| + | } catch (Exception e) { | ||
| + | // TODO: handle exception | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return result; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Get data(Html) from a https url | ||
| + | | ||
| + | * @param urlString A URL String | ||
| + | * @return The Data from the urlString or null if failed | ||
| + | */ | ||
| + | public static String getHttpsData(String urlString) { | ||
| + | String result = null; | ||
| + | URL url = null; | ||
| + | |||
| + | HostnameVerifier hv = new HostnameVerifier() { | ||
| + | public boolean verify(String arg0, SSLSession arg1) { | ||
| + | // TODO Auto-generated method stub | ||
| + | // | ||
| + | return true; | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | try { | ||
| + | url = new java.net.URL(urlString); | ||
| + | } catch (MalformedURLException e1) { | ||
| + | // TODO Auto-generated catch block | ||
| + | e1.printStackTrace(); | ||
| + | System.out.println(" | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | HttpsURLConnection.setDefaultHostnameVerifier(hv); | ||
| + | HttpsURLConnection conn; | ||
| + | |||
| + | try { | ||
| + | conn = (javax.net.ssl.HttpsURLConnection)url.openConnection(); | ||
| + | conn.setDoOutput(true); | ||
| + | InputStreamReader isr = new InputStreamReader(conn.getInputStream()); | ||
| + | result = readFromInputStream(isr); | ||
| + | } catch (IOException e) { | ||
| + | // TODO Auto-generated catch block | ||
| + | e.printStackTrace(); | ||
| + | } catch (Exception e) { | ||
| + | // TODO: handle exception | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return result; | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Read data from InputStreamReader | ||
| + | | ||
| + | * @param isr InputStreamReader | ||
| + | * @return The Data read from The InputStreamReader | ||
| + | */ | ||
| + | public static String readFromInputStream(InputStreamReader isr) { | ||
| + | String result = null; | ||
| + | |||
| + | BufferedReader rd = new BufferedReader(isr); | ||
| + | String line; | ||
| + | try { | ||
| + | while ((line = rd.readLine()) != null) { | ||
| + | result += line + ' | ||
| + | } | ||
| + | |||
| + | isr.close(); | ||
| + | rd.close(); | ||
| + | } catch (IOException e) { | ||
| + | // TODO Auto-generated catch block | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return result; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ====== SSLUtilities.java ====== | ||
| + | |||
| + | |||
| + | 如果要把上面的代码正常跑起来, | ||
| + | |||
| + | <file java SSLUtilities.java> | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | /** | ||
| + | * This class provide various static methods that relax X509 certificate and | ||
| + | * hostname verification while using the SSL over the HTTP protocol. | ||
| + | * | ||
| + | * @author | ||
| + | */ | ||
| + | | ||
| + | |||
| + | /** | ||
| + | * Hostname verifier for the Sun's deprecated API. | ||
| + | * | ||
| + | * @deprecated see {@link # | ||
| + | */ | ||
| + | | ||
| + | /** | ||
| + | * Thrust managers for the Sun's deprecated API. | ||
| + | * | ||
| + | * @deprecated see {@link # | ||
| + | */ | ||
| + | | ||
| + | /** | ||
| + | * Hostname verifier. | ||
| + | */ | ||
| + | | ||
| + | /** | ||
| + | * Thrust managers. | ||
| + | */ | ||
| + | | ||
| + | |||
| + | |||
| + | /** | ||
| + | * Set the default Hostname Verifier to an instance of a fake class that | ||
| + | * trust all hostnames. This method uses the old deprecated API from the | ||
| + | * com.sun.ssl package. | ||
| + | * | ||
| + | * @deprecated see {@link # | ||
| + | */ | ||
| + | | ||
| + | // Create a trust manager that does not validate certificate chains | ||
| + | | ||
| + | | ||
| + | } // if | ||
| + | // Install the all-trusting host name verifier | ||
| + | | ||
| + | | ||
| + | } // __trustAllHttpsCertificates | ||
| + | |||
| + | /** | ||
| + | * Set the default X509 Trust Manager to an instance of a fake class that | ||
| + | * trust all certificates, | ||
| + | * old deprecated API from the com.sun.ssl package. | ||
| + | * | ||
| + | * @deprecated see {@link # | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | |||
| + | // Create a trust manager that does not validate certificate chains | ||
| + | | ||
| + | | ||
| + | {new _FakeX509TrustManager()}; | ||
| + | } // if | ||
| + | // Install the all-trusting trust manager | ||
| + | try { | ||
| + | | ||
| + | | ||
| + | } catch(GeneralSecurityException gse) { | ||
| + | throw new IllegalStateException(gse.getMessage()); | ||
| + | } // catch | ||
| + | | ||
| + | | ||
| + | } // __trustAllHttpsCertificates | ||
| + | |||
| + | /** | ||
| + | * Return true if the protocol handler property java. | ||
| + | * protocol.handler.pkgs is set to the Sun's com.sun.net.ssl. | ||
| + | * internal.www.protocol deprecated one, false | ||
| + | * otherwise. | ||
| + | * | ||
| + | * @return | ||
| + | * property is set to the Sun's deprecated one, false | ||
| + | * otherwise. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | | ||
| + | } // isDeprecatedSSLProtocol | ||
| + | |||
| + | /** | ||
| + | * Set the default Hostname Verifier to an instance of a fake class that | ||
| + | * trust all hostnames. | ||
| + | */ | ||
| + | | ||
| + | // Create a trust manager that does not validate certificate chains | ||
| + | | ||
| + | | ||
| + | } // if | ||
| + | // Install the all-trusting host name verifier: | ||
| + | | ||
| + | } // _trustAllHttpsCertificates | ||
| + | |||
| + | /** | ||
| + | * Set the default X509 Trust Manager to an instance of a fake class that | ||
| + | * trust all certificates, | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | |||
| + | // Create a trust manager that does not validate certificate chains | ||
| + | | ||
| + | | ||
| + | } // if | ||
| + | // Install the all-trusting trust manager: | ||
| + | try { | ||
| + | | ||
| + | | ||
| + | } catch(GeneralSecurityException gse) { | ||
| + | throw new IllegalStateException(gse.getMessage()); | ||
| + | } // catch | ||
| + | | ||
| + | | ||
| + | } // _trustAllHttpsCertificates | ||
| + | |||
| + | /** | ||
| + | * Set the default Hostname Verifier to an instance of a fake class that | ||
| + | * trust all hostnames. | ||
| + | */ | ||
| + | | ||
| + | // Is the deprecated protocol setted? | ||
| + | | ||
| + | | ||
| + | } else { | ||
| + | | ||
| + | } // else | ||
| + | } // trustAllHostnames | ||
| + | |||
| + | /** | ||
| + | * Set the default X509 Trust Manager to an instance of a fake class that | ||
| + | * trust all certificates, | ||
| + | */ | ||
| + | | ||
| + | // Is the deprecated protocol setted? | ||
| + | | ||
| + | | ||
| + | } else { | ||
| + | | ||
| + | } // else | ||
| + | } // trustAllHttpsCertificates | ||
| + | |||
| + | /** | ||
| + | * This class implements a fake hostname verificator, | ||
| + | * name. This class uses the old deprecated API from the com.sun. | ||
| + | * ssl package. | ||
| + | * | ||
| + | * @author | ||
| + | * | ||
| + | * @deprecated see {@link SSLUtilities.FakeHostnameVerifier}. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | |||
| + | /** | ||
| + | * Always return true, indicating that the host name is an | ||
| + | * acceptable match with the server' | ||
| + | * | ||
| + | * @param hostname | ||
| + | * @param session | ||
| + | * host. | ||
| + | * @return | ||
| + | * indicating the host name is trusted. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | } // verify | ||
| + | } // _FakeHostnameVerifier | ||
| + | |||
| + | |||
| + | /** | ||
| + | * This class allow any X509 certificates to be used to authenticate the | ||
| + | * remote side of a secure socket, including self-signed certificates. This | ||
| + | * class uses the old deprecated API from the com.sun.ssl | ||
| + | * package. | ||
| + | * | ||
| + | * @author | ||
| + | * | ||
| + | * @deprecated see {@link SSLUtilities.FakeX509TrustManager}. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | |||
| + | /** | ||
| + | * Empty array of certificate authority certificates. | ||
| + | */ | ||
| + | | ||
| + | new X509Certificate[] {}; | ||
| + | |||
| + | |||
| + | /** | ||
| + | * Always return true, trusting for client SSL | ||
| + | * chain peer certificate chain. | ||
| + | * | ||
| + | * @param chain the peer certificate chain. | ||
| + | * @return | ||
| + | * indicating the chain is trusted. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | } // checkClientTrusted | ||
| + | |||
| + | /** | ||
| + | * Always return true, trusting for server SSL | ||
| + | * chain peer certificate chain. | ||
| + | * | ||
| + | * @param chain the peer certificate chain. | ||
| + | * @return | ||
| + | * indicating the chain is trusted. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | } // checkServerTrusted | ||
| + | |||
| + | /** | ||
| + | * Return an empty array of certificate authority certificates which | ||
| + | * are trusted for authenticating peers. | ||
| + | * | ||
| + | * @return | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | } // getAcceptedIssuers | ||
| + | } // _FakeX509TrustManager | ||
| + | |||
| + | |||
| + | /** | ||
| + | * This class implements a fake hostname verificator, | ||
| + | * name. | ||
| + | * | ||
| + | * @author | ||
| + | */ | ||
| + | | ||
| + | |||
| + | /** | ||
| + | * Always return true, indicating that the host name is | ||
| + | * an acceptable match with the server' | ||
| + | * | ||
| + | * @param hostname | ||
| + | * @param session | ||
| + | * host. | ||
| + | * @return | ||
| + | * indicating the host name is trusted. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | | ||
| + | } // verify | ||
| + | } // FakeHostnameVerifier | ||
| + | |||
| + | |||
| + | /** | ||
| + | * This class allow any X509 certificates to be used to authenticate the | ||
| + | * remote side of a secure socket, including self-signed certificates. | ||
| + | * | ||
| + | * @author | ||
| + | */ | ||
| + | | ||
| + | |||
| + | /** | ||
| + | * Empty array of certificate authority certificates. | ||
| + | */ | ||
| + | | ||
| + | new X509Certificate[] {}; | ||
| + | |||
| + | |||
| + | /** | ||
| + | * Always trust for client SSL chain peer certificate | ||
| + | * chain with any authType authentication types. | ||
| + | * | ||
| + | * @param chain the peer certificate chain. | ||
| + | * @param authType | ||
| + | * certificate. | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | } // checkClientTrusted | ||
| + | |||
| + | /** | ||
| + | * Always trust for server SSL chain peer certificate | ||
| + | * chain with any authType exchange algorithm types. | ||
| + | * | ||
| + | * @param chain the peer certificate chain. | ||
| + | * @param authType | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | } // checkServerTrusted | ||
| + | |||
| + | /** | ||
| + | * Return an empty array of certificate authority certificates which | ||
| + | * are trusted for authenticating peers. | ||
| + | * | ||
| + | * @return | ||
| + | */ | ||
| + | | ||
| + | | ||
| + | } // getAcceptedIssuers | ||
| + | } // FakeX509TrustManager | ||
| + | } // SSLUtilities | ||
| + | |||
| + | </ | ||
