提供一个wink restclient用get访问restapi的例子(java语言)
来源:互联网 发布:健身会所软件 编辑:程序博客网 时间:2024/06/08 11:15
public void getMethod() {
JSONObject result;
String url = "https://api.bmob.cn/1/classes/xxx";
// 详细的类库请使用Apache wink,地址:http://wink.apache.org
RestClient restClient = new RestClient(getClientConfig());
Resource resource = restClient.resource(url);
resource.header("X-Bmob-Application-Id", "xxx");
resource.header("X-Bmob-REST-API-Key", "xxx");
ClientResponse response = resource.get();
if(response.getStatusCode() == 200){
result = new JSONObject(response.getEntity(String.class));
}
}
private ClientConfig getClientConfig() throws ServletException {
ClientConfig myConfig = new ClientConfig();
SSLContext sc;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, getTrustManager(), new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
throw new ServletException(e.getMessage());
} catch (KeyManagementException e) {
throw new ServletException(e.getMessage());
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
myConfig.setBypassHostnameVerification(true);
myConfig.connectTimeout(100000);
myConfig.readTimeout(100000);
myConfig.followRedirects(false);
return myConfig;
}
private TrustManager[] getTrustManager() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
} };
return trustAllCerts;
}
JSONObject result;
String url = "https://api.bmob.cn/1/classes/xxx";
// 详细的类库请使用Apache wink,地址:http://wink.apache.org
RestClient restClient = new RestClient(getClientConfig());
Resource resource = restClient.resource(url);
resource.header("X-Bmob-Application-Id", "xxx");
resource.header("X-Bmob-REST-API-Key", "xxx");
ClientResponse response = resource.get();
if(response.getStatusCode() == 200){
result = new JSONObject(response.getEntity(String.class));
}
}
private ClientConfig getClientConfig() throws ServletException {
ClientConfig myConfig = new ClientConfig();
SSLContext sc;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, getTrustManager(), new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
throw new ServletException(e.getMessage());
} catch (KeyManagementException e) {
throw new ServletException(e.getMessage());
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
myConfig.setBypassHostnameVerification(true);
myConfig.connectTimeout(100000);
myConfig.readTimeout(100000);
myConfig.followRedirects(false);
return myConfig;
}
private TrustManager[] getTrustManager() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
} };
return trustAllCerts;
}
0 0
- 提供一个wink restclient用get访问restapi的例子(java语言)
- 通过RestClient访问restAPI接口
- RESTClient请求的例子
- 推荐一个好用的录像工具-Wink
- groovy RESTClient的POST、GET、DELETE 用法
- 用maven创建基于wink的rest服务(二)-携带参数的get请求
- 用maven创建基于wink的rest服务(一)-get请求
- yii2 restapi例子
- wink,一个免费的可以在linux下用的教学视频制作软件
- Wink - 好用的屏幕录制工具
- restclient post 例子
- 一个使用Java语言描述的矩阵旋转的例子
- OpenDayLight 氢版本 RestAPI 调用实例(1)-拓扑的获取(Java)
- 一个java和golang(go语言)通信的例子。
- 一个例子说明C语言和Java的区别
- 一个javafx初学者实现国际象棋简单方法(很粗暴)棋子实现不再提供 没有使用java编程思想用的很基础的c语言思想
- 使用Python访问restAPI接口
- ajax 用js直接访问服务器的一个例子
- 感谢网络,硬生生让我的砖机获得重生
- POJ 1915 Knight Moves
- 深入讲解WEBVIEW——上
- SQL collate
- MyEclipse 的Web项目移植到Eclipse中无法添加到Tomcat
- 提供一个wink restclient用get访问restapi的例子(java语言)
- [iOS_Dev] 谁 retain 了 object ?
- cocos2d-js 3.0 屏幕适配方案 分辨率适应
- 读写锁
- 深入讲解WEBVIEW——下
- http status code 状态码
- 关于后台数据中包含的html代码在前台显示的问题
- Unity5.0 解密
- 将已有的分区表全局索引改为本地索引