
*.tencentcloudapi.com,产品的调用地址有一定区别,例如,云服务器的调用地址为cvm.tencentcloudapi.com。具体调用地址可参考对应产品的 API 文档。<dependencies>标签,在里面添加以下依赖项即可。<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. --><!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 --><version>3.1.217</version></dependency>
<repositories><repository><id>nexus-tencentyun</id><name>Nexus tencentyun</name><url>https://mirrors.tencent.com/nexus/repository/maven-public/</url></repository></repositories>
import com.tencentcloudapi.common.Credential;import com.tencentcloudapi.common.exception.TencentCloudSDKException;import com.tencentcloudapi.cvm.v20170312.CvmClient;import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest;import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;public class DescribeInstances {public static void main(String[] args) {try {Credential cred = new Credential("secretId", "secretKey");CvmClient client = new CvmClient(cred, "ap-shanghai");DescribeInstancesRequest req = new DescribeInstancesRequest();DescribeInstancesResponse resp = client.DescribeInstances(req);System.out.println(DescribeInstancesResponse.toJsonString(resp));} catch (TencentCloudSDKException e) {System.out.println(e.toString());}}}
import com.tencentcloudapi.common.Credential;import com.tencentcloudapi.common.exception.TencentCloudSDKException;// 导入对应产品模块的clientimport com.tencentcloudapi.cvm.v20170312.CvmClient;// 导入要请求接口对应的request response类import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest;import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;import com.tencentcloudapi.cvm.v20170312.models.Filter;//导入可选配置类import com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.HttpProfile;import com.tencentcloudapi.common.profile.Language;public class DescribeInstances {public static void main(String[] args) {try {// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密Credential cred = new Credential("secretId", "secretKey");// 实例化一个http选项,可选的,没有特殊需求可以跳过HttpProfile httpProfile = new HttpProfile();// 从3.1.16版本开始, 单独设置 HTTP 代理// httpProfile.setProxyHost("真实代理ip");// httpProfile.setProxyPort(真实代理端口);httpProfile.setReqMethod("GET"); // get请求(默认为post请求)httpProfile.setProtocol("https://"); // 在外网互通的网络环境下支持http协议(默认是https协议),请选择(https:// or http://)httpProfile.setConnTimeout(30); // 请求连接超时时间,单位为秒(默认60秒)httpProfile.setWriteTimeout(30); // 设置写入超时时间,单位为秒(默认0秒)httpProfile.setReadTimeout(30); // 设置读取超时时间,单位为秒(默认0秒)httpProfile.setEndpoint("cvm.ap-shanghai.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)// 实例化一个client选项,可选的,没有特殊需求可以跳过ClientProfile clientProfile = new ClientProfile();clientProfile.setSignMethod("HmacSHA256"); // 指定签名算法(默认为HmacSHA256)// 自3.1.80版本开始,SDK 支持打印日志。clientProfile.setHttpProfile(httpProfile);clientProfile.setDebug(true);// 从3.1.16版本开始,支持设置公共参数 Language, 默认不传,选择(ZH_CN or EN_US)clientProfile.setLanguage(Language.EN_US);// 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的CvmClient client = new CvmClient(cred, "ap-shanghai", clientProfile);// 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。DescribeInstancesRequest req = new DescribeInstancesRequest();// 填充请求参数,这里request对象的成员变量即对应接口的入参// 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义Filter respFilter = new Filter(); // 创建Filter对象, 以zone的维度来查询cvm实例respFilter.setName("zone");respFilter.setValues(new String[] { "ap-shanghai-1", "ap-shanghai-2" });req.setFilters(new Filter[] { respFilter }); // Filters 是成员为Filter对象的列表// 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的// 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应DescribeInstancesResponse resp = client.DescribeInstances(req);// 输出json格式的字符串回包System.out.println(DescribeInstancesResponse.toJsonString(resp));// 也可以取出单个值。// 你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义System.out.println(resp.getTotalCount());} catch (TencentCloudSDKException e) {System.out.println(e.toString());}}}
HttpProfile httpProfile = new HttpProfile();httpProfile.setProxyHost("真实代理ip");httpProfile.setProxyPort(真实代理端口);
import com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.Language;...ClientProfile clientProfile = new ClientProfile();clientProfile.setLanguage(Language.ZH_CN);
HttpProfile httpProfile = new HttpProfile();httpProfile.setProtocol("http://"); //http 协议httpProfile.setProtocol("https://"); //https 协议
ClientProfile clientProfile = new ClientProfile();clientProfile.setDebug(true);
九月 10, 2020 5:14:30 下午 com.tencentcloudapi.cvm.v20170312.CvmClient info信息: send request, request url: https://cvm.ap-shanghai.tencentcloudapi.com/?Nonce=367595572&Action=DescribeInstances&Filters.0.Values.1=ap-shanghai-2&Version=2017-03-12&Filters.0.Values.0=ap-shanghai-1&SecretId=******************&Filters.0.Name=zone&RequestClient=SDK_JAVA_3.1.129&Region=ap-shanghai&SignatureMethod=HmacSHA256&Timestamp=1599729270&Signature=DcGRPdquMZZRPj1NFXP5bsOGnRlaT2KXy7aegNhZa00%3D. request headers information:九月 10, 2020 5:14:32 下午 com.tencentcloudapi.cvm.v20170312.CvmClient info信息: recieve response, response url: https://cvm.ap-shanghai.tencentcloudapi.com/?Nonce=367595572&Action=DescribeInstances&Filters.0.Values.1=ap-shanghai-2&Version=2017-03-12&Filters.0.Values.0=ap-shanghai-1&SecretId=******************&Filters.0.Name=zone&RequestClient=SDK_JAVA_3.1.129&Region=ap-shanghai&SignatureMethod=HmacSHA256&Timestamp=1599729270&Signature=DcGRPdquMZZRPj1NFXP5bsOGnRlaT2KXy7aegNhZa00%3D, response headers: Server: nginx;Date: Thu, 10 Sep 2020 09:14:32 GMT;Content-Type: application/json;Content-Length: 103;Connection: keep-alive;OkHttp-Selected-Protocol: http/1.1;OkHttp-Sent-Millis: 1599729271230;OkHttp-Received-Millis: 1599729272020;,response body information: com.squareup.okhttp.internal.http.RealResponseBody@8646db9
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");Log logger = LogFactory.getLog("TestLog");logger.info("hello world");
<dependency><groupId>com.qcloud</groupId><artifactId>qcloud-java-sdk</artifactId><version>2.0.6</version></dependency>
[TencentCloudSDKException]message:java.net.ConnectException-Connection timed out: connect requestId:这里需要排查:是否本机配置了代理,而未在代码中加入代理,代理的加入可参考上文的 代理配置。Exception in thread "main" java.lang.NoSuchMethodError: okio.BufferedSource.rangeEquals(JLokio/ByteString;)Z。<dependency><groupId>com.qcloud</groupId><artifactId>cmq-http-client</artifactId><version>1.0.7</version></dependency><dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>3.1.59</version></dependency>
文档反馈