【问题标题】:Dyndns updater with http get带有 http get 的 Dyndns 更新程序
【发布时间】:2014-02-11 10:02:41
【问题描述】:

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

他们的api文档位于here

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

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()

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

【问题讨论】:

  • 你是什么意思,'我如何做主机名'?
  • 在示例中,他们有主机 members.dynds.org 。在我的请求字符串中我没有那个?
  • 不,但你有一些其他主机名指向你的 IP
  • 是的,但这是他们需要的参数。仍然需要将请求发送到的服务器的主机名。
  • 我不确定您必须使用路径 /nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG 向 members.dyndns.org 发送 HTTP GET 有什么困惑,其中主机名是您要更新的用户名/主机名

标签: c# http dyndns


【解决方案1】:

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

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

/// <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();
}

【讨论】:

  • 使用MyWebRequest content = new MyWebRequest("http://checkip.dyndns.org/");,您还可以获得您的IP。当然你得把内容切到ip
【解决方案2】:
// 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();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2020-11-21
    • 1970-01-01
    • 2014-06-01
    • 2015-07-02
    相关资源
    最近更新 更多