赞
踩
<dependency> <groupId>com.aliyun.api.gateway</groupId> <artifactId>sdk-core-java</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>com.monitorjbl</groupId> <artifactId>xlsx-streamer</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> </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(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。