Use the HTTPDNS SDK for Java to call HTTPDNS API operations from your Java application.
Get an AccessKey pair
To call HTTPDNS API operations via OpenAPI Explorer, you need an AccessKey pair — an AccessKey ID and an AccessKey secret.
For security, create a RAM user, grant the user permissions to manage HTTPDNS, and use the RAM user's AccessKey pair rather than your root account credentials.
Create a RAM user. For more information, see Create a RAM user.
Manage RAM user permissions to manage HTTPDNS.
-
Obtain the AccessKey pair of the RAM user. For more information, see Create an AccessKey.
ImportantAn AccessKey pair consists of an AccessKey ID and an AccessKey secret.
Add dependencies
Add the following Maven dependency to your project:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-httpdns</artifactId>
<version>2.1.2</version>
</dependency>
Or download the JAR packages directly:
Sample code
-
Before calling the API, set the following environment variables with your AccessKey credentials:
export CC_AK_ENV=<access_key_id> export CC_SK_ENV=<access_key_secret>Replace <access_key_id> with the AccessKey ID of your RAM user and <access_key_secret> with the AccessKey secret.
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.httpdns.model.v20160201.*;
import com.aliyuncs.profile.*;
import com.aliyuncs.*;
// Configure SDK Profile
// An Alibaba Cloud account AccessKey pair has permissions to call all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.
// We recommend that you do not save your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and all resources within your account are exposed to risks.
// In this example, the system reads the AccessKey pair from environment variables to implement authentication for API access.
public class DemoListDomains {
public static void main(String[] args) {
String accessKeyId = System.getenv("CC_AK_ENV"); /* Access Key ID */
String accessKeySecret = System.getenv("CC_SK_ENV"); /* Access Key Secret */
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
ListDomainsRequest request = new ListDomainsRequest();
try {
ListDomainsResponse httpResponse = client.getAcsResponse(request);
System.out.println(httpResponse.getRequestId());
}catch (Exception e) {
e.printStackTrace();
}
}
}