欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现路由器断开连接,更改公网ip的实例代码

程序员文章站 2024-02-12 12:23:30
复制代码 代码如下:public static void disconnect(){    string url = "断 线"; ...

复制代码 代码如下:

public static void disconnect(){
    string url = "断 线";
    string uri = "http://192.168.1.1/userrpm/statusrpm.htm?disconnect=" + system.web.httputility.urlencode(url, system.text.encoding.getencoding("gb2312")) + "&wan=1";
    string suser = "admin";
    string spwd = "admin";
    string sdomain = "";
    networkcredential ocredential;
    httpwebrequest orequest = (system.net.httpwebrequest)webrequest.create(uri);
    if (orequest != null)
    {
        orequest.protocolversion = httpversion.version11;// send request
        orequest.method = "get";
        orequest.contenttype = "application/x-www-form-urlencoded";
        orequest.useragent = "mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; gtb6.4; .net clr 2.0.50727; .net clr 3.0.4506.2152; .net clr 3.5.30729)";
        orequest.referer = "http://192.168.1.1";

        if (suser != string.empty)
        {
             ocredential = new networkcredential(suser, spwd, sdomain);
             orequest.credentials = ocredential.getcredential(new uri(uri), string.empty);
        }
        else
        {
             orequest.credentials = credentialcache.defaultcredentials;
        }
        streamreader sr = new streamreader(orequest.getresponse().getresponsestream(), system.text.encoding.default);
        string line = sr.readtoend();
        sr.close();
        if (line.indexof("lan口状态") > -1)//登录成功
        {
             console.writeline("断开登录成功");
        }
        else
        {
            console.writeline("断开登录失败");
        }
    }
}