Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpClient and HTTPS

I'm new to implementing HTTPS connections in Android. Essentially, I'm trying to connect to a server using the org.apache.http.client.HttpClient. I believe, at some point, I'll need to access the application's keystore in order to authorize my client with a private key. But, for the moment, I'm just trying to connect and see what happens; I keep getting an HTTP/1.1 400 Bad Request error.

I can't seem to make heads or tails of this despite many examples (none of them seem to work for me). My code looks like this (the BODY constant is XmlRPC):

 private void connect() throws IOException, URISyntaxException{      HttpPost post     = new HttpPost(new URI(PROD_URL));     HttpClient client = new DefaultHttpClient();      post.setEntity(new StringEntity(BODY));     HttpResponse result = client.execute(post);      Log.d("MainActivity", result.getStatusLine().toString());  } 

So, pretty simple. Let me know if anyone out there has any advice. Thanks!

like image 664
harrisonlee Avatar asked Apr 08 '10 21:04

harrisonlee


People also ask

What is the use of httpclient in Android Studio?

Android Studio is used to create the sample. HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class.

How to use httpclient in httppost?

HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data.

How to make get and POST requests in Android HTTP client?

When developing Android HTTP client, GET and POST requests are the base blocks in HTTP protocol. To make this kind of requests we need first to open a connection toward the remote server: In the first line we get the HttpURLConnection, while in line 2, we set the method and at the end we connect to the server.

Should I use httpclienthandler or httpclient?

You need TLS 1.2+ support for HttpClient. You don't need TLS 1.2+ support for WebClient. HttpClientHandler is a good choice if you need TLS 1.2+ support but must support versions of Android earlier than Android 4.1. It is also a good choice if you need TLS 1.2+ support for WebClient.


1 Answers

This should get you started. I'm using basically the same, except with ThreadSafeClientConnManager.

SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https",              SSLSocketFactory.getSocketFactory(), 443));  HttpParams params = new BasicHttpParams();  SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);  HttpClient client = new DefaultHttpClient(mgr, params); 
like image 85
synic Avatar answered Oct 13 '22 19:10

synic