How to execute an https request through an HTTP client?

I am trying to make a GET request through an https endpoint, I'm not sure if there is any special treatment that is needed, but below is my code:

String foursquareURL = "https://api.foursquare.com/v2/venues/search?ll=" + latitude + "," + longitude + "&client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET; System.out.println("Foursquare URL is " + foursquareURL); try { Log.v("HttpClient", "Preparing to create a request " + foursquareURL); URI foursquareURI = new URI(foursquareURL); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(foursquareURI)); content = response.getEntity().getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(content)); String strLine; String result = ""; while ((strLine = br.readLine()) != null) { result += strLine; } //editTextShowLocation.setText(result); Log.v("result of the parser is", result); } catch (Exception e) { Log.v("Exception", e.getLocalizedMessage()); } 
+4
source share
2 answers

I'm not sure this approach will work on Android, but we saw this problem in server-side Java using HttpClient with HTTPS URLs. Here's how we solved the problem:

First, we copied / adapted the implementation of the EasySSLProtocolSocketFactory class to our own code base. You can find the source here:

http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java?view=markup

Using this class, we will create new HttpClient instances using

 HttpClient httpClient = new HttpClient(); mHttpClient = httpClient; Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", easyhttps); 

Using EasySSLProtocolSocketsfactory will allow your HttpClient to ignore any certificate failures / outcomes when executing the request.

0
source

Take a look at AndroidHttpClient . This is essentially an alternative to DefaultHttpClient , which will register some commonly used schemes (including HTTPS) for you backstage when you create it.

You can then execute HttpGet using this instance of this client, and it will handle SSL for you if your URL specifies the "https" scheme. You do not need to bother registering your own SSL protocols / schemes, etc.

0
source

Source: https://habr.com/ru/post/1380938/


All Articles