2011-11-12 182 views
4

我试图让通过HTTPS端点的GET请求,我不知道是否有任何需要的特殊治疗,但下面是我的代码:如何通过http客户端执行https获取请求?

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

什么是错误? –

+0

它总是遇到异常,说api.foursquare.com,它说地址家族不支持的协议 – adit

+0

没有什么特别的与HTTPS(尽管服务器证书是由Android接受器,这是另一回事)。特定于HTTPS的问题?你有没有使用过HTTPS的另一个wibe网站上的另一个网址? – Thierry

回答

0

我不知道,如果这方法将在Android上运行,但我们在服务器端Java中使用HttpClient和HTTPS URL看到了同样的问题。下面是我们如何解决问题的方法:首先,我们将EasySSLProtocolSocketFactory类的实现复制/修改为我们自己的代码库。您可以在这里找到来源:

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

有了这个类,然后我们用创造我们的新HttpClient的实例:

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

使用该EasySSLProtocolSocketsfactory将让你的HttpClient忽略任何证书失败/在提出请求时出现。

0

看看AndroidHttpClient。它实质上是DefaultHttpClient的替代品,它将在您创建后为您在幕后注册一些常用方案(包括HTTPS)。

然后,您应该可以使用此客户端的一个实例执行HttpGet,并且如果您的URL指示“https”方案,它将为您处理SSL。您不应该需要注册自己的SSL协议/方案等。