首页 > 解决方案 > 如何使用 c# 从 cudy lte n300 4g 路由器发送短信

问题描述

我正在尝试使用 c# 中的 HttpClient 从路由器发送短信。我成功登录并获取了发送功能的令牌,但在发送短信功能上我得到 401 Unauthorized。路由器通过电缆连接到电脑。我使用wireshark 来捕获http://192.168.10.1/webpost.cgi帖子的功能和发送数据。这是我的代码:

  Dictionary<string, string> valuesForCookie = new Dictionary<string, string>();
        CookieContainer cookies = new CookieContainer();
        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = cookies;
        HttpClient client = new HttpClient(handler);
        var values = new Dictionary<string, string>
         {
               { "uname", "admin" },
           { "passwd", "mypassword" }
         };
        var content = new FormUrlEncodedContent(values);

        var response = client.PostAsync("http://192.168.10.1/login.cgi", content).Result;
        Uri uri = new Uri("http://192.168.10.1/login.cgi");
        IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri);

        valuesForCookie.Add("clear_web_language", "0");
        valuesForCookie.Add("clear_pageIndex", "1");
        valuesForCookie.Add("clear_pageNum", "0");
        valuesForCookie.Add("DWRLOGGEDUSER", "admin");
        valuesForCookie.Add("DWRLOGGEDTIMEOUT", "300");
        foreach (Cookie item in responseCookies)
        {
            if (valuesForCookie.ContainsKey(item.Name))
                valuesForCookie[item.Name] = item.Value;
            else
            {
                valuesForCookie.Add(item.Name, item.Value);
            }

        }
        valuesForCookie.Add("DWRLOGGEDID", valuesForCookie["qSessId"]);
        var responseString = response.Content.ReadAsStringAsync().Result;

        cookies = new CookieContainer();
        handler = new HttpClientHandler();
      
        foreach (KeyValuePair<string, string> entry in valuesForCookie)
        {
            cookies.Add(new Uri("http://192.168.10.1/"), new Cookie(entry.Key, entry.Value, "/", "192.168.10.1"));
        }

        values = new Dictionary<string, string>
         {
               { "check_valid","qSessId="+valuesForCookie["qSessId"] },

         };
        handler.CookieContainer = cookies;
        content = new FormUrlEncodedContent(values);


        client = new HttpClient(handler);
        response = client.GetAsync("http://192.168.10.1//data.ria?token=1").Result;
        string authID = response.Content.ReadAsStringAsync().Result.Trim();
       
        values = new Dictionary<string, string>
        {
              { "{\"CfgType\":\"sms_action\",\"type\":\"sms_send\",\"msg\":\"1111\",\"phone_list\":\"123456789\",\"authID\":\""+authID+"\"}", string.Empty },

        };
        content = new FormUrlEncodedContent(values);
        client = new HttpClient(handler);
       
        response = client.PostAsync("http://192.168.10.1/webpost.cgi", content).Result;

标签: c#posthttpclientrouter

解决方案


推荐阅读