0

我需要使用应用程序更新我们的 dyndns 区域。

他们的 api 文档位于此处

他们说我需要像这样提出一个获取请求:

GET /nic/update?    hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG HTTP/1.0
Host: members.dyndns.org
Authorization: Basic base-64-authorization
User-Agent: Company - Device - Version Number

我将如何在 c# 中执行此操作?

我试过这个:

String request = "/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG HTTP/1.0";
WebRequest webRequest = WebRequest.Create(request);
WebResponse webResp = webRequest.GetResponse();
Console.WriteLine(webResp.ToString()

但是我该如何做主机名和所有这些呢?

4

2 回答 2

2

我只是想发布我最终正确的代码,以防其他人有一天可能需要帮助!

为简单起见,我将其分解为一些子功能。不要让它吓到你。

/// <summary>
/// Call this from another class to update a zone.
/// </summary>
/// <param name="host">The full name of the host</param>
/// <returns></returns>
public string Update(String host)
{
    string url = BuildUrl(host, Ip);
    return PerformUpdate(url);
}

这是构建 url 的函数

 /// <summary>
 /// //Constructs the url to send the get request to.
 /// </summary>
 /// <param name="hostname">the hostname </param>
 /// <param name="ip">the ipaddress</param>
 /// <returns>The complete String</returns>
 private string BuildUrl(String hostname, String ip)
 {
    return BaseUrl + "hostname=" + hostname + "&myip=" + ip;
 }

这是执行更新的函数:

/// <summary>
/// Performs the actual request to the dyndns server to update the entity
/// </summary>
/// <param name="url">url to post</param>
private String PerformUpdate(String url)
{
   HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
   NetworkCredential creds = new NetworkCredential(Username, Password);
   request.UserAgent = Username + " - " + Password + " - " + "0.01";
   request.Credentials = creds;
   request.Method = "GET";
   HttpWebResponse response = request.GetResponse() as HttpWebResponse;
   Stream reply = response.GetResponseStream();
   StreamReader readReply = new StreamReader(reply);
   return readReply.ReadToEnd();
}
于 2014-02-13T08:06:56.930 回答
0
// External/Public IP
string externalIP = string.Empty;
externalIP = GetUserIPAddress();
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
            .Matches(externalIP)[0].ToString();

希望这能解决您的问题。:)

于 2018-09-28T08:23:21.207 回答