阿里云API网关文档
错误代码表
如何获取错误信息
<dependency>
<groupId>com.aliyun.api.gatewaygroupId>
<artifactId>sdk-core-javaartifactId>
<version>1.1.0version>
dependency>
<dependency>
<groupId>com.monitorjblgroupId>
<artifactId>xlsx-streamerartifactId>
<version>1.2.0version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.28version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.12version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.1.2version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.1.3version>
dependency>
import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.enums.Scheme;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
public class HttpApiClient extends ApacheHttpClient{
/**
* 初始化请求参数
* @param isHttps 是否为https请求
* @param host
* @param appKey
* @param appSecret
*/
public HttpApiClient(boolean isHttps, String host, String appKey, String appSecret){
// HTTP/HTTPS Client init
HttpClientBuilderParams httpParam = new HttpClientBuilderParams();
httpParam.setHost(host);
httpParam.setAppKey(appKey);
httpParam.setAppSecret(appSecret);
if(isHttps){
initHttpsClient(httpParam);
} else {
initHttpClient(httpParam);
}
super.init(httpParam);
}
/**
* 初始化HTTP请求参数
* @param httpParam
*/
private void initHttpClient(HttpClientBuilderParams httpParam){
httpParam.setScheme(Scheme.HTTP);
}
/**
* 初始化HTTPS请求参数
* @param httpsParam
*/
private void initHttpsClient(HttpClientBuilderParams httpsParam){
httpsParam.setScheme(Scheme.HTTPS);
/**
* HTTPS request use DO_NOT_VERIFY mode only for demo
* Suggest verify for security
*/
X509TrustManager xtm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
X509Certificate[] x509Certificates = new X509Certificate[0];
return x509Certificates;
}
};
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{xtm}, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
httpsParam.setSslSocketFactory(sslContext.getSocketFactory());
httpsParam.setX509TrustManager(xtm);
httpsParam.setHostnameVerifier(DO_NOT_VERIFY);
}
/**
* 同步接口
* @param body
* @return
*/
public ApiResponse send(byte[] body) {
String path = "/test/send.json";
ApiRequest request = new ApiRequest(HttpMethod.POST_BODY , path, body);
return sendSyncRequest(request);
}
/**
* 异步接口
* @param body
* @param apiCallback
*/
public void sendAnsy(byte[] body, ApiCallback apiCallback) {
String path = "/test/send.json";
ApiRequest request = new ApiRequest(HttpMethod.POST_BODY , path, body);
sendAsyncRequest(request, apiCallback);
}
}
import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestApiService {
private static Logger log = LoggerFactory.getLogger(TestApiService.class);
private final HttpApiClient httpApiClient;
/**
* 初始化请求参数
* @param isHttps 是否为https请求
* @param host
* @param appKey
* @param appSecret
*/
public TestApiService(boolean isHttps, String host, String appKey, String appSecret){
this.httpApiClient = new HttpApiClient(isHttps, host, appKey, appSecret);
}
/**
* 同步接口
* @return
*/
public JSONObject send(){
String body = "{}";
ApiResponse response = this.httpApiClient.send(body.getBytes(SdkConstant.CLOUDAPI_ENCODING));
JSONObject result = JSONObject.parseObject(new String(response.getBody()));
return result;
}
/**
* 异步接口
*/
public void sendAnsy(){
String body = "{}";
this.httpApiClient.sendAnsy(body.getBytes(SdkConstant.CLOUDAPI_ENCODING), new ApiCallback() {
@Override
public void onFailure(ApiRequest request, Exception e) {
log.error(e.getMessage(),e);
}
@Override
public void onResponse(ApiRequest request, ApiResponse response) {
JSONObject result = JSONObject.parseObject(new String(response.getBody()));
log.info(result.toJSONString());
}
});
}
public static void main(String[] args) {
boolean isHttps = false;
String host = "";
String appKey = "";
String appSecret = "";
TestApiService service = new TestApiService(isHttps, host, appKey, appSecret);
service.sendAnsy();
}
}