java阿里雲動態域名解析、java阿里雲DDNS

1 創建阿里雲AccessKey,作爲訪問密鑰

 

 

 進入後點擊“創建AccessKey”。創建成功後記住,AccessKeyId和AccessKeySecret

2 獲取域名解析某條碼id

進入阿里雲域名解析界面

 

 

 F12打開開發工具,點擊網絡,再點擊垃圾桶,清空原來列表內容

 

在你要ddns的條目上,點擊修改按鈕,先隨便改一個地址。我這個是ipv6條目

 

 

 點擊確認後查看之前網絡部分新增條目

 

 

 找到一個post請求,單擊點開,再點擊請求頁面,"recordId"下面的數字就是這個條目的id

 

 

 除了剛纔的id,還需要記下,type、rr的值。value的值就是我要修改的值,到時候代碼裏面自己指定即可。

 

 

 3 java代碼編寫

需要修改代碼地方:

 

 ipv6用上圖這個,ipv4地址寫:4.ipw.cn

 

 這裏對號入座即可

 1 package org.example;
 2 
 3 import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;
 4 import com.aliyun.tea.TeaException;
 5 import com.aliyun.teaopenapi.models.Config;
 6 import com.aliyun.teautil.models.RuntimeOptions;
 7 
 8 import java.io.BufferedReader;
 9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 
14 public class TestAliDDNS {
15     /**
16      * 使用AK&SK初始化賬號Client
17      *
18      * @param accessKeyId
19      * @param accessKeySecret
20      * @return Client
21      * @throws Exception
22      */
23     public static com.aliyun.alidns20150109.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
24         Config config = new Config()
25                 // 您的 AccessKey ID
26                 .setAccessKeyId(accessKeyId)
27                 // 您的 AccessKey Secret
28                 .setAccessKeySecret(accessKeySecret);
29         // 訪問的域名
30         config.endpoint = "alidns.cn-shanghai.aliyuncs.com";
31         return new com.aliyun.alidns20150109.Client(config);
32     }
33 
34     public static String getIpv6Address() throws IOException {
35         URL url = new URL("http://6.ipw.cn/");
36         BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
37         String str;
38         if ((str = br.readLine()) != null) {
39             return str;
40         }
41         return "";
42     }
43 
44     public static void main(String[] args_) throws Exception {
45         String ipv6A = getIpv6Address();
46         if (ipv6A == null || "".equals(ipv6A)) {
47             return;
48         }
49         java.util.List<String> args = java.util.Arrays.asList(args_);
50         com.aliyun.alidns20150109.Client client = createClient("AccessKeyId", "AccessKeySecret");
51         UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest()
52                 .setRecordId("711164111172535111")
53                 .setValue(ipv6A)
54                 .setType("AAAA")
55                 .setRR("@");
56         RuntimeOptions runtime = new RuntimeOptions();
57         try {
58             // 複製代碼運行請自行打印 API 的返回值
59             client.updateDomainRecordWithOptions(updateDomainRecordRequest, runtime);
60             System.out.println("success.");
61         } catch (TeaException error) {
62             // 如有需要,請打印 error
63             com.aliyun.teautil.Common.assertAsString(error.message);
64             error.printStackTrace();
65         } catch (Exception _error) {
66             TeaException error = new TeaException(_error.getMessage(), _error);
67             // 如有需要,請打印 error
68             com.aliyun.teautil.Common.assertAsString(error.message);
69             _error.printStackTrace();
70         }
71     }
72 }

pom文件依賴

 1     <dependency>
 2       <groupId>com.aliyun</groupId>
 3       <artifactId>alidns20150109</artifactId>
 4       <version>2.0.1</version>
 5     </dependency><dependency>
 6     <groupId>com.aliyun</groupId>
 7     <artifactId>alidns20150109</artifactId>
 8     <version>2.0.1</version>
 9   </dependency>
10     <dependency>
11       <groupId>com.aliyun</groupId>
12       <artifactId>tea-openapi</artifactId>
13       <version>0.2.2</version>
14     </dependency>
15     <dependency>
16       <groupId>com.aliyun</groupId>
17       <artifactId>tea-console</artifactId>
18       <version>0.0.1</version>
19     </dependency>
20     <dependency>
21       <groupId>com.aliyun</groupId>
22       <artifactId>tea-util</artifactId>
23       <version>0.2.13</version>
24     </dependency>
25     <dependency>
26       <groupId>com.aliyun</groupId>
27       <artifactId>tea</artifactId>
28       <version>1.1.14</version>
29     </dependency>

4 設置開機自動定時重複運行

後面博客有發

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章