class Program
{
private static string regionId = "cn-hangzhou";
private static string accessKeyId = "Ydjakdfdsadfd";//换成自己的
private static string accessKeySecret = "wfdg89ds8fdkfasdfyaiusfdasf";//换成自己的
private static string recordId = "4685613245468132";//blog.yxxrui.cn的唯一记录ID
//或者设置为空,然后下边有从阿里云获取的方法,只是不建议每次都去阿里云获取。
private static string domain = "yxxrui.cn";
private static string rr = "blog";//子域名
private static string filename = "C:/yxxrui/ip.dat";//缓存IP保存的位置
private static readonly IClientProfile ClientProfile =
DefaultProfile.GetProfile(regionId, accessKeyId,accessKeySecret);
private static readonly IAcsClient Client = new DefaultAcsClient(ClientProfile);
static void Main(string[] args)
{
//为了加快速度,此处获取到recordId之后就保存起来,以后不再请求阿里云接口
/*recordId = GetRecordId(domain, rr);
if (recordId == "") return;*/
//获取成功,并且有变化时
string lastIp = Read();
string curIp = GetIp();
if (!curIp.Equals("") && !lastIp.Equals(curIp))
{
var request = new UpdateDomainRecordRequest
{
RecordId = recordId,
RR = rr,
Type = "A",
Value = curIp,
TTL = 600,//我的是免费版,最小为10分钟,600秒
Priority = 10
};
Client.DoAction(request);
Write(curIp);//更新成功后保存IP地址到本地
}
}
private static string GetRecordId(string domain,string rr)
{
DescribeDomainRecordsRequest reqq = new DescribeDomainRecordsRequest
{
DomainName = domain,
RRKeyWord = rr
};
var rss = Client.GetAcsResponse(reqq);
foreach (var record in rss.DomainRecords)
{
if (record.DomainName.Equals(domain) && record.RR.Equals(rr))
{
return record.RecordId;
}
}
return "";
}
public static string Read()
{
FileInfo file = new FileInfo(filename);
if (!file.Exists)
{
Write("尚未获取");
}
StreamReader sr = new StreamReader(filename, Encoding.Default);
string res = "";
String line;
while ((line = sr.ReadLine()) != null)
{
res += line;
}
sr.Close();
return res;
}
public static void Write(string ip)
{
FileStream fs = new FileStream(filename, FileMode.Create);
//获得字节数组
byte[] data = Encoding.Default.GetBytes(ip);
//开始写入
fs.Write(data, 0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
private static string GetIp()
{
string ip = "";
try
{
WebClient myWebClient = new WebClient {
Credentials = CredentialCache.DefaultCredentials
};
//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = myWebClient.DownloadData(
"http://www.yxxrui.cn/yxxrui_cabangs_api/myip.ashx");
string pageHtml = Encoding.Default.GetString(pageData);
//如果获取网站页面采用的是GB2312,则使用这句
//string pageHtml = Encoding.UTF8.GetString(pageData);
//如果获取网站页面采用的是UTF-8,则使用这句
return pageHtml;
}
catch (WebException webEx)
{
}
return ip;
}
}
using System;
using System.Configuration;
using System.Net;
using System.Text;
using Aliyun.Acs.Alidns.Model.V20150109;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
namespace AliyunDomain
{
class Program
{
private static readonly string RegionId=GetConfig("regionId");
private static readonly string AccessKeyId=GetConfig("accessKeyId");
private static readonly string AccessKeySecret=GetConfig("accessKeySecret");
private static string _recordId = GetConfig("recordId");
private static readonly string Domain = GetConfig("domain");
private static readonly string Rr = GetConfig("rr");
private static readonly IClientProfile ClientProfile =
DefaultProfile.GetProfile(RegionId, AccessKeyId,AccessKeySecret);
private static readonly IAcsClient Client = new DefaultAcsClient(ClientProfile);
static void Main(string[] args)
{
if (string.IsNullOrWhiteSpace(_recordId))
{
//如果没有获取过,那么获取一下,然后保存起来
_recordId = GetRecordId(Domain, Rr);
SaveConfig("recordId",_recordId);
}
if (_recordId == "") return;
//获取成功,并且有变化时
string lastIp = GetConfig("lastIP");
string curIp = GetIp();
if (!curIp.Equals("") && !lastIp.Equals(curIp))
{
var request = new UpdateDomainRecordRequest
{
RecordId = _recordId,
RR = Rr,
Type = "A",
Value = curIp,
TTL = 600,
Priority = 10
};
Client.DoAction(request);
SaveConfig("lastIP", curIp);
}
}
#region 保存配置信息
///
/// 保存配置信息
///
///
///
private static void SaveConfig(string name,string value)
{
var cfa = ReadAppConfig();
cfa.AppSettings.Settings[name].Value = value;
cfa.Save();
}
#endregion
#region 获取配置项
///
/// 获取配置项
///
///
///
private static string GetConfig(string name)
{
var cfa = ReadAppConfig();
return cfa.AppSettings.Settings[name].Value;
}
#endregion
#region 调用阿里云接口获取域名的唯一记录ID
///
/// 调用阿里云接口获取域名的唯一记录ID
///
/// 域名
/// 子域名
///
private static string GetRecordId(string domain,string rr)
{
DescribeDomainRecordsRequest reqq = new DescribeDomainRecordsRequest
{
DomainName = domain,
RRKeyWord = rr
};
var rss = Client.GetAcsResponse(reqq);
foreach (var record in rss.DomainRecords)
{
if (record.DomainName.Equals(domain) && record.RR.Equals(rr))
{
return record.RecordId;
}
}
return "";
}
#endregion
#region 获取公网IP
///
/// 获取当前电脑的真实外网IP地址,可以自行修改成稳定的
///
///
private static string GetIp()
{
string ip = "";
try
{
//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
WebClient myWebClient = new WebClient {
Credentials = CredentialCache.DefaultCredentials };
//从指定网站下载数据
Byte[] pageData = myWebClient.DownloadData(
"http://www.yxxrui.cn/yxxrui_cabangs_api/myip.ashx");
//如果获取网站页面采用的是GB2312,则使用这句
string pageHtml = Encoding.Default.GetString(pageData);
//如果获取网站页面采用的是UTF-8,则使用这句
//string pageHtml = Encoding.UTF8.GetString(pageData);
return pageHtml;
}
catch (WebException webEx)
{
}
return ip;
}
#endregion
#region 获取配置文件
private static Configuration _config;
private static Configuration ReadAppConfig()
{
if (_config == null)
{
string configPath = @"app.config";
_config = ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap()
{
ExeConfigFilename = configPath
}, ConfigurationUserLevel.None);
}
//MessageBox.Show(config.AppSettings.Settings["serverIp"].Value);
return _config;
}
#endregion
}
}
app.config文件内容如下:
为了更好的获取IP地
这些都是调用了现成的SDK的,不能直接抄,只能先把API文档小蜜蜂论坛回帖机看懂,再一步步的实现,挺花时间的。
代码可以作为借鉴,也可以改成其它代码格式,慢慢研究