gxh973121

博客园 首页 新随笔 联系 订阅 管理
  125 随笔 :: 93 文章 :: 81 评论 :: 15万 阅读

    我是通过路由器和另一个人合用一个Adsl上网的,路由器用的是TP-link R402,我想在外面上网时能登陆到家中的主机,这样必须知道家里计算机上网的IP,但AdslIP地址是动态分配的,如何得到家中主机的IP呢?

我的解决方法:路由器的管理页面中有当前的主机IP地址 ,我写了一个程序,解析得到ip

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using Sgml;
using System.Xml;

namespace GetIPAddress
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.TextBox IPAddr;
        
private System.Windows.Forms.Button GetIP;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void GetIP_Click(object sender, System.EventArgs e)
        
{
            
string sPageUrl="http://192.168.1.1/userRpm/StatusRpm.htm";
            
string sXPath="/html/body/center/form/table[2]/tr[2]/td/table/tr/td[2]/table/tr[2]";
            
string sContent="";

            WebRequest  request 
= WebRequest.Create(sPageUrl); 
            request.Credentials
=new NetworkCredential("admin","admin");
            WebResponse response 
= request.GetResponse(); 
            Stream resStream 
= response.GetResponseStream();     
            StreamReader sr 
= new StreamReader(resStream, System.Text.Encoding.Default); 

            SgmlReader reader 
= new SgmlReader();
            reader.DocType 
= "HTML";

            reader.InputStream 
= new StringReader(sr.ReadToEnd());
            StringWriter sw 
= new StringWriter();
            XmlTextWriter writer 
= new XmlTextWriter(sw);
            writer.Formatting 
= Formatting.Indented;
            
while (reader.Read())
            
{
                
if (reader.NodeType != XmlNodeType.Whitespace)
                
{
                    writer.WriteNode(reader, 
true);
                }

            }

            
            sContent 
= sw.ToString();

            
if (sXPath.Length == 0)
            
{
                
this.IPAddr.Text = sw.ToString();
            }

            
else
            
{
                System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
                System.Xml.XPath.XPathDocument doc 
= new System.Xml.XPath.XPathDocument(new StringReader(sw.ToString()));
                System.Xml.XPath.XPathNavigator nav 
= doc.CreateNavigator();
                System.Xml.XPath.XPathNodeIterator nodes 
= nav.Select(sXPath);
                
while (nodes.MoveNext())
                
{
                    sb.Append(nodes.Current.Value 
+ "\n");
                }

                
this.IPAddr.Text = sb.ToString();
            }


            resStream.Close();  
            sr.Close();
        }

    }

}


这里引用了一个组件SGML,是对SGML进行解析的
运行结果如图 
 

这里引用了一个组件SGML,是对SGML进行解析的 运行结果如图 

接着就是做一服务,每个X分钟检测一次ip是否跟以前相同,如ip改变,自动发邮件到我的邮箱,发送邮件函数在我的文档可以找到

posted on   gxh973121  阅读(5713)  评论(16编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示