Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. HttpClient httpclient = getNewHttpClient();
  2.  
  3. HttpPost request = new HttpPost(uri);
  4. try {
  5. request.addHeader("Authorization", "autorisation string");
  6. request.addHeader("Content-Type", "application/json; charset=utf-8");
  7. request.setEntity(new ByteArrayEntity(Query.getBytes("utf-8")));
  8.  
  9. HttpHost hh = new HttpHost(uri.getHost());
  10.  
  11. Log.w("myApp", "Port: " + Integer.toString(hh.getPort()));
  12. HttpResponse response = httpclient.execute(new HttpHost(uri.getHost()),request);
  13. InputStream inputStream = response.getEntity().getContent();
  14. final char[] buffer = new char[0x10000];
  15. responseString = new StringBuilder();
  16. Reader in = new InputStreamReader(inputStream, "utf-8");
  17. int read;
  18. do {
  19. read = in.read(buffer, 0, buffer.length);
  20. if (read > 0) {
  21. responseString.append(buffer, 0, read);
  22. }
  23. } while (read >= 0);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27.  
  28. public HttpClient getNewHttpClient() {
  29. HttpClient hc = null;
  30. try {
  31. KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
  32. trustStore.load(null, null);
  33.  
  34. SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
  35. sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  36.  
  37. HttpParams params = new BasicHttpParams();
  38. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  39. HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  40.  
  41. SchemeRegistry registry = new SchemeRegistry();
  42. registry.register(new Scheme("https", sf, 443));
  43. registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  44.  
  45.  
  46. ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
  47. hc = new DefaultHttpClient(ccm, params);
  48.  
  49. } catch (Exception e) {
  50. Log.w("myApp",e.toString());
  51. hc = new DefaultHttpClient();
  52. }
  53. return hc;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement