1、通过用户名和密码访问
public static WebResource getClient(String url){
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
Client client = Client.create(config);
client.addFilter(new HTTPDigestAuthFilter("Username", "Password"));//网站的用户名和密码
WebResource resource = client.resource(url);
return resource;
}
通过以上的方式获得client,然后进行api访问
2、通过api token方式
public static WebResource getClientjenkins(String url){
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
Client client = Client.create(config);
client.addFilter(new HTTPDigestAuthFilter("tokenname", "API_Token"));//api的token和其name
WebResource resource = client.resource(url);
return resource;
}
关于rest api的使用大家可以参见我的另一篇文章
http://blog.csdn.net/li575098618/article/details/47853263