【问题标题】:Reload IP on noip.com in C#用 C# 在 noip.com 上重新加载 IP
【发布时间】:2014-11-26 18:49:02
【问题描述】:

无法将 POST 请求发送到正确的站点:noip.com。 Instructions contained here。 尝试向 C# 发送请求:

private void DDD()
        {
            string Host = "";
            string Authorization = "";
            string Authorization_Base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(Authorization));

            string Zapros = "GET /nic/update?hostname=" + Host + "&myip=1.2.3.4 HTTP/1.0" + "\n"
                + "Host: dynupdate.no-ip.com" + "\n"
                + "Authorization: " + Authorization_Base64 + "\n"
                + "User-Agent: MSA/1.0 msa.com";

            System.Net.HttpWebRequest reqPOST = (HttpWebRequest)WebRequest.Create("http://dynupdate.no-ip.com/nic/update");
            reqPOST.Method = "POST"; 
            reqPOST.Timeout = 120000; 

            byte[] sentData = Encoding.UTF8.GetBytes(Zapros);
            reqPOST.ContentLength = sentData.Length;
            System.IO.Stream sendStream = reqPOST.GetRequestStream();
            sendStream.Write(sentData, 0, sentData.Length);
            sendStream.Close();

            System.Net.WebResponse result = reqPOST.GetResponse();
            System.IO.Stream stream = result.GetResponseStream();
            System.IO.StreamReader sr = new System.IO.StreamReader(stream);
            string s = sr.ReadToEnd();
            MessageBox.Show(s);
        }

【问题讨论】:

  • 您要同时发出 POST 和 GET 请求?您的“Zapros”变量的内容实际上是您的请求的内容和设置,而不是您的请求正文中应该包含的内容。如果您将 request.Method 更改为“GET”,那么请求对象应该具有您可以分配的主机和授权部分。 (以及用户代理)。

标签: c# post web request ip


【解决方案1】:

您可以在 .NET 4.5 中使用 HttpClient:

    public static async void DynamicUpdate(string hostname, string myip, string username, string password) {
        try {
            string noipuri = "http://dynupdate.no-ip.com/nic/update?hostname=" + hostname + "&myip=" + myip;

            using (var client = new HttpClient(new HttpClientHandler { Credentials = new NetworkCredential(username, password) }))
            using (var response = await client.GetAsync(noipuri))
            using (var content = response.Content) {
                await content.ReadAsStringAsync();
            }
        }
        catch {

        }
    }

【讨论】:

  • 我在 RPI 上使用 Windows IOT Core;我已经花了大约 ½ 天的时间寻找可行的解决方案,并且您的 sln 是完美的。感谢分享!
【解决方案2】:

你可以试试这个:

    public static void DynamicUpdate(string hostname, string myip, string username, string password)
    {
        try
        {
            HttpWebRequest req =
                (HttpWebRequest)
                    HttpWebRequest.Create("http://dynupdate.no-ip.com/nic/update?hostname=" + hostname + "&myip=" + myip);
            req.Host = "dynupdate.no-ip.com";
            req.Credentials = new NetworkCredential(username, password);
            req.UserAgent = "My awesome update Client/1.0 contact@email.com";
            req.Method = "GET";
            using (var res = (HttpWebResponse)req.GetResponse())
            {
                // Do something with the response accordingly to
                // http://www.noip.com/integrate/response
            }
        }
        catch
        {
        }
    }

:)

【讨论】:

    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2012-09-03
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多