1

我正在尝试使用 DynDNS API 来验证用户提供的 DynDNS 凭据,并且文档“badauth”响应正是为此而生的。
这个想法是我可以使用用户凭据自动更新并查找 badauth 响应并使用它来检测不正确的凭据,这听起来很完美。
问题是我得到的唯一回应是 badauth。当我发送应该很好的凭据时(因为它们当前正在用于登录和所有),我得到一个 badauth 响应。这是我的代码:

   try
   {
     string target = string.Format( "http://{0}:{1}@members.dyndns.org:8245/nic/update?hostname={2}&myip={3}",
      HttpUtility.UrlEncode( DynDnsUserName ), HttpUtility.UrlEncode( DynDnsPassword ), HttpUtility.UrlEncode( DynDnsURL ), ip );
     HttpWebRequest request = WebRequest.Create( target ) as HttpWebRequest;
     request.Method = "GET";
     request.UserAgent = company + " - " + model + " - " + firmware;
     HttpWebResponse response = request.GetResponse() as HttpWebResponse;
     Stream reply = response.GetResponseStream();
     StreamReader readReply = new StreamReader( reply );
     string returnCode = readReply.ReadToEnd();
     Trace.WriteLine( "We've validated the provided DynDNS credentials, response received: " + returnCode );
     return !returnCode.Contains( "badauth" );
   }
   catch (WebException webEx)
   {
     Trace.WriteLine( "WebException thrown: " + webEx.Message );
     if (webEx.Response != null)
     {
      Stream reply = webEx.Response.GetResponseStream();
      StreamReader readReply = new StreamReader( reply );
      Trace.WriteLine( "Actual response: " + readReply.ReadToEnd() );
     }
   }

无论我使用什么凭据,我都会收到一个抛出的 WebException “远程服务器返回错误:(401) 未授权”,响应正文为 badauth。有任何想法吗?

4

1 回答 1

2

{0}:{1}@... 不会处理 webrequest。

您的目标只是“http://members.dynd....”,而您执行类似的操作

NetworkCredential myCredentials = new NetworkCredential(username,passwd);

request.Credentials = myCredentials;
于 2011-12-09T01:11:11.107 回答