c# - 如何使用 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;
解决方案
推荐阅读
- ruby-on-rails - 迁移文件存在,但没有适用于 Rails 应用程序的模型
- amazon-web-services - AWS S3,从 Angular 或 JS 或 Python 或其他文件上传
- python - 通过 python 将数据记录器中的实时数据保存到 csv 文件
- php - 从会话购物车中读取多个项目时出现问题
- javascript - 使用 jQuery-EasyFilter 过滤“多类别”
- json - VBA-JSON parser v2.2.3 运行时不一致
- java - 是否可以将数字分配给字符串,然后将这些数字放入出队系统中?
- javascript - 如何显示/隐藏什么时候
被选中/未选中? - c# - 使用模板和创建冲突来创建人员对象
- excel - 如何在word文档中表格的同一单元格中添加2张来自excel的jpeg图片?