我只是想发布我最终正确的代码,以防其他人有一天可能需要帮助!
为简单起见,我将其分解为一些子功能。不要让它吓到你。
/// <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();
}