How do I connect to router “panel” using C#

我们两清 提交于 2019-12-22 12:45:10

问题


I'm trying to connect to my router "panel" (192.168.1.1) using a simple C# HTTP Web Request:

var userName = "admin";
var passWord = "admin";
var encoding = new ASCIIEncoding();
var postData = "" + userName;
postData += (":" + passWord); //I saw (using Wireshark) that the HTTP packet is sending the username & password in this form: "username:password"
byte[] data = encoding.GetBytes(postData);

var myRequest = (HttpWebRequest)WebRequest.Create("http://192.168.1.1");
myRequest.Method = "POST"; **//Or should I use GET?**
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
try
{
  var response = myRequest.GetResponse();
  var responseStream = response.GetResponseStream();
  var responseReader = new StreamReader(responseStream);
  var result = responseReader.ReadToEnd();
}
catch (WebException we)
{
  Console.WriteLine(we.Message);
}

When I'm running this code I get an saying '401 Not Authorized Exception', although the UN and Password are correct.

Can anyone suggest me, what's wrong here?


回答1:


Have you tried using this as the address and dropping the post data part of your code :

http://username:password@192.168.1.1

Else, you could use basic authentication, as explained in this article




回答2:


try this before get respond

myRequest.Credentials = new NetworkCredential(userName,passWord);


来源:https://stackoverflow.com/questions/17144556/how-do-i-connect-to-router-panel-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!