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 设置开机自动定时重复运行
后面博客有发