最近为客户做了一个程序使用httpclient调用Quickr for Domino的REST API获取Quickr中的文档列表,本来是一个简单的事情,可是这个客户的环境有一些麻烦。第一,他们启用了SSL,但是证书过期了。(状况就是每次访问Quickr时,浏览器都要报证书过期的警告)。第二,Quickr页面被做了定制,无法找到REST API的URL。经过两天的研究,终于解决。特以博客方式记录下来。
如何找到特定页面的REST API URL呢?经过艰难的研究,发现其实很简单,REST API URL就是页面URL后加一个“&Form=hAtom”后缀即可,例如页面的URL是 https://quickr.abc.cn/LotusQuickr/place/PageLibrary482576D300250AF3.nsf /h_Toc/EDC922351AF87015482576D300278C21/?OpenDocument,那么它REST URL就是 https://quickr.abc.cn/LotusQuickr/place/PageLibrary482576D300250AF3.nsf /h_Toc/EDC922351AF87015482576D300278C21/?OpenDocument&Form=hAtom,很简单吧。
第二个问题,如何解决启用SSL证书过期,通过httpclient访问的问题呢,这个比较麻烦了,直接上代码吧
Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", easyhttps); HttpClient httpClient = new HttpClient(); httpClient.getParams().setContentCharset("UTF-8"); HostConfiguration hc = new HostConfiguration(); hc.setHost("quickr.abc.cn", 80, easyhttps); PostMethod httpMethod = new PostMethod("https://quickr.abc.cn/LotusQuickr/place/PageLibrary482576D300250AF3.nsf?Login"); httpClient.executeMethod(hc,httpMethod); httpMethod.releaseConnection();
这里面涉及到一个自定义类EasySSLProtocolSocketFactory,直接参考我附件中两个java文件的实现吧,实话说我也没看懂。不过好用,:-)
在开发过程中还遇到一个问题,就是httpclient不支持url自动的跳转,因此本需要一次访问的就需要做两次调用。完整的代码如下:
package com.abc.quickr; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.abdera.Abdera; import org.apache.abdera.model.Document; import org.apache.abdera.model.Entry; import org.apache.abdera.model.Feed; import org.apache.abdera.parser.Parser; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.protocol.Protocol; public class PlacesHelper { public List<Entry> getPlaces() { try { Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", easyhttps); HttpClient httpClient = new HttpClient(); httpClient.getParams().setContentCharset("UTF-8"); HostConfiguration hc = new HostConfiguration(); hc.setHost("qucikr.abc.cn", 80, easyhttps); PostMethod httpMethod = new PostMethod("https://qucikr.abc.cn/LotusQuickr/place/PageLibrary482576D300250AF3.nsf?Login"); NameValuePair username = new NameValuePair("username", "admin"); NameValuePair password = new NameValuePair("password", "password"); httpMethod.setRequestBody(new NameValuePair[]{username,password}); httpClient.executeMethod(hc,httpMethod); httpMethod.releaseConnection(); GetMethod redirect = new GetMethod("https://qucikr.abc.cn/LotusQuickr/place/PageLibrary482576D300250AF3.nsf/h_Toc/EDC922351AF87015482576D300278C21/?OpenDocument&Form=hAtom"); httpClient.executeMethod(hc,redirect); InputStream in = redirect.getResponseBodyAsStream(); Parser parser = Abdera.getNewParser(); Document<Feed> doc = parser.parse(in); Feed feed = (Feed) doc.getRoot(); return feed.getEntries(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }