Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. protected HttpClient constructClient() {
  2. HttpParams params = new BasicHttpParams();
  3. params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
  4.  
  5. // use the debug proxy to view internet traffic on the host computer
  6. if (ABCApplication.isDebuggingProxy()) params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(ABCConstants.DEBUG_PROXY_HOST, ABCConstants.DEBUG_PROXY_PORT, "http"));
  7.  
  8. // ignore ssl certification (due to signed authority not appearing on android list of permitted authorities)
  9. // see: http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/
  10. SchemeRegistry registry = new SchemeRegistry();
  11. registry.register(new Scheme("http", new PlainSocketFactory(), 80));
  12. registry.register(new Scheme("https", new FakeSocketFactory(), 443));
  13. ClientConnectionManager cm = new SingleClientConnManager(params, registry);
  14.  
  15. return new DefaultHttpClient(cm, params);
  16. }
  17.  
  18. HttpURLConnection http = null;
  19. URL url;
  20. try {
  21. url = new URL("https:your domian");
  22.  
  23. if (url.getProtocol().toLowerCase().equals("https")) {
  24. trustAllHosts();
  25. HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
  26. https.setHostnameVerifier(DO_NOT_VERIFY);
  27. http = https;
  28. System.out.println("TEST:::"+convertStreamToString(http.getInputStream()));
  29. } else {
  30. http = (HttpURLConnection) url.openConnection();
  31. System.out.println("TEST:::"+convertStreamToString(http.getInputStream()));
  32. }
  33. } catch (MalformedURLException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40.  
  41. public void checkClientTrusted(X509Certificate[] chain,
  42. String authType) throws CertificateException {
  43. }
  44.  
  45. public void checkServerTrusted(X509Certificate[] chain,
  46. String authType) throws CertificateException {
  47. }
  48. } };
  49.  
  50. // Install the all-trusting trust manager
  51. try {
  52. SSLContext sc = SSLContext.getInstance("TLS");
  53. sc.init(null, trustAllCerts, new java.security.SecureRandom());
  54. HttpsURLConnection
  55. .setDefaultSSLSocketFactory(sc.getSocketFactory());
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement