Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. final Context context;
  2.  
  3. public MyHttpsClient(Context context) {
  4. this.context = context;
  5. }
  6.  
  7. @Override
  8. protected ClientConnectionManager createClientConnectionManager() {
  9. SchemeRegistry registry = new SchemeRegistry();
  10. registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  11. // Register for port 443 our SSLSocketFactory with our keystore
  12. // to the ConnectionManager
  13. registry.register(new Scheme("https", newSslSocketFactory(), 443));
  14. return new SingleClientConnManager(getParams(), registry);
  15. }
  16.  
  17. private SSLSocketFactory newSslSocketFactory() {
  18. try {
  19. // Get an instance of the Bouncy Castle KeyStore format
  20. KeyStore trusted = KeyStore.getInstance("BKS");
  21. // Get the raw resource, which contains the keystore with
  22. // your trusted certificates (root and any intermediate certs)
  23. InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
  24. try {
  25. // Initialize the keystore with the provided trusted certificates
  26. // Also provide the password of the keystore
  27. trusted.load(in, "testpassword".toCharArray());
  28. } finally {
  29. in.close();
  30. }
  31. // Pass the keystore to the SSLSocketFactory. The factory is responsible
  32. // for the verification of the server certificate.
  33. SSLSocketFactory sf = new SSLSocketFactory(trusted);
  34. // Hostname verification from certificate
  35. // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
  36. sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
  37. return sf;
  38. } catch (Exception e) {
  39. throw new AssertionError(e);
  40. }
  41. }
  42.  
  43. DefaultHttpClient httpClient = new MyHttpsClient(context);
  44. HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 30000);
  45. HttpContext localContext = new BasicHttpContext();
  46. HttpPost httpPost = new HttpPost(url);
  47. httpPost.setHeader("Accept", "application/json");
  48. httpPost.setHeader("Content-type", "application/json");
  49. httpPost.setEntity(new StringEntity(jsonString));
  50.  
  51. response = httpClient.execute(httpPost, localContext);
  52. HttpEntity entity = response.getEntity();
  53. httpresponse = getResponse(entity);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement