我如何连接到路由器“面板"?使用C# [英] How do I connect to router "panel" using C#

查看:122
本文介绍了我如何连接到路由器“面板"?使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用简单的C#HTTP Web请求连接到路由器面板"(192.168.1.1):

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);
}

运行此代码时,我会说" 401 未经授权的异常",尽管UN和密码正确.

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?

推荐答案

您是否尝试过使用它作为地址并删除代码中的post data部分:

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

这篇关于我如何连接到路由器“面板"?使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆