Quick BI中如何使用原始的HttpClient调用产品提供的API接口
更新时间:
概述
本文介绍主要如何使用Apache原始的HttpClient调用Quick BI产品提供的API接口。
详细信息
Quick BI提供了SDK包调用产品开放的API接口。具体代码实现时需要同时安装Quick BI SDK和阿里云核心库。其中独立部署版本生成客户端需要引用Quick BI本身封装好的类:com.alibaba.quickbi.openapi.client.IOpenAPIClient,且需要客户的JDK版本在1.7及其以上。
但是实际应用中,有些客户由于其他原因,不能使用Quick BI库本身封装的客户端代码,而必须使用原始的HttpClient。这种情况下,可以参考如下代码实现调用:
package com.alibaba.quickbi.openapi;
import com.alibaba.quickbi.openapi.client.FormatType;
import com.alibaba.quickbi.openapi.client.ParameterUtil;
import com.alibaba.quickbi.openapi.common.signature.SignatureUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class CloseableHttpClientUtil {
// 签名的ak/sk
final static String ak = "XXX";
final static String sk = "XXX";
// host 域名
final static String host = "http://XXXXX.yunbi.biz.aliyun.test";
// 默认的client类。
private static HttpClient client = new DefaultHttpClient();
private static final String ENCODING_TYPE = "UTF-8";
private static final String POST = "POST";
private static final String GET = "GET";
public static void doPost(String uri,HashMap params, FormatType formatType)
throws ClientProtocolException, IOException {
// 设置为get取连接的方式.
HttpPost request = new HttpPost(host + uri);
// 请求类型
request.addHeader("Content-type", formatType.format);
// header
Map<String, String> signHeaders = new HashMap();
signHeaders.put("X-Gw-AccessId", ak);
signHeaders.put("X-Gw-Nonce", UUID.randomUUID().toString());
signHeaders.put("X-Gw-Timestamp", String.valueOf(System.currentTimeMillis()));
Map<String, String> signParameters = new HashMap<>();
if (formatType == FormatType.FORM) {
// parameter json提交时不参与拼接
signParameters = ParameterUtil.getFormMap(params);
}
// 待签名字符串
String stringToSign = SignatureUtil.buildStringToSign(uri, POST, signParameters, signHeaders);
// 生产签名
String sign = SignatureUtil.sign(stringToSign, sk);
signHeaders.put("X-Gw-Signature", sign);
// 是否开启调试模式,仅用于测试调试
request.addHeader("X-Gw-Debug", "true");
// 插入请求头
for (String key : signHeaders.keySet()) {
request.addHeader(key, signHeaders.get(key));
}
// 插入参数
String formData = ParameterUtil.getFormData(params);
request.setEntity(new StringEntity(formData, ENCODING_TYPE));
// 得到返回的response.
HttpResponse response = client.execute(request);
// 得到返回的client里面的实体对象信息.
HttpEntity rsEntity = response.getEntity();
if (rsEntity != null) {
System.out.println("内容编码是:" + rsEntity.getContentEncoding());
System.out.println("内容类型是:" + rsEntity.getContentType());
// 得到返回的主体内容.
InputStream instream = rsEntity.getContent();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream, ENCODING_TYPE));
System.out.println(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
} finally {
instream.close();
}
}
// 关闭连接.
client.getConnectionManager().shutdown();
}
/**
* 测试
*/
public static void test() throws IOException {
String apiUri = "/openapi/v2/embed/ticket/create";
HashMap<Object, Object> params = new HashMap<>();
params.put("worksId","6b6e4622-32b0-422b-9647-f040179cdd07");
doPost(apiUri, params, FormatType.FORM);
}
public static void main(String[] args) throws Exception {
test();
}
}
适用于
- Quick BI独立部署版本
文档内容是否对您有帮助?