简体   繁体   English

如何通过http客户端执行https get请求?

[英]how to perform an https get request via http client?

I am trying to make a GET request via an https end point, I am not sure if there are any special treatment that is needed, but below is my code: 我试图通过https终点发出GET请求,我不确定是否需要任何特殊处理,但下面是我的代码:

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());
              }

I'm not sure if this approach will work on Android, but we saw this same problem in server-side Java using HttpClient with HTTPS URLs. 我不确定这种方法是否适用于Android,但我们在服务器端Java中使用带有HTTPS URL的HttpClient看到了同样的问题。 Here is how we solved the problem: 以下是我们解决问题的方法:

First, we copied/adapted an implementation of the class EasySSLProtocolSocketFactory into our own code base. 首先,我们将类EasySSLProtocolSocketFactory的实现复制/调整到我们自己的代码库中。 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 http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java?view=markup

With that class in place, we then create our new HttpClient instances with: 有了这个类,我们就可以创建新的HttpClient实例:

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

The use of the EasySSLProtocolSocketsfactory will allow your HttpClient to ignore any certificate failures/isses when making the request. 使用EasySSLProtocolSocketsfactory将允许您的HttpClient在发出请求时忽略任何证书失败/缺陷。

Take a look at the AndroidHttpClient . 看看AndroidHttpClient It's essentially an alternative to DefaultHttpClient which will register some commonly used schemes (including HTTPS) for you behind the scenes when you create it. 它本质上是DefaultHttpClient的替代品,它会在您创建它时为您在幕后注册一些常用的方案(包括HTTPS)。

You should then be able to execute HttpGet s using this an instance of this client and it will handle the SSL for you if your URL indicates an 'https' scheme. 然后,您应该能够使用此客户端的实例执行HttpGet ,如果您的URL指示“https”方案,它将为您处理SSL。 You shouldn't need to mess around with registering your own SSL protocols / schemes etc. 您不应该需要注意自己的SSL协议/方案等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM