我是通过路由器和另一个人合用一个Adsl上网的,路由器用的是TP-link R402,我想在外面上网时能登陆到家中的主机,这样必须知道家里计算机上网的IP,但Adsl的IP地址是动态分配的,如何得到家中主机的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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
this.IPAddr = new System.Windows.Forms.TextBox();
this.GetIP = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// IPAddr
//
this.IPAddr.Enabled = false;
this.IPAddr.Location = new System.Drawing.Point(32, 56);
this.IPAddr.Name = "IPAddr";
this.IPAddr.Size = new System.Drawing.Size(224, 21);
this.IPAddr.TabIndex = 0;
this.IPAddr.Text = "";
//
// GetIP
//
this.GetIP.Location = new System.Drawing.Point(96, 112);
this.GetIP.Name = "GetIP";
this.GetIP.Size = new System.Drawing.Size(88, 23);
this.GetIP.TabIndex = 1;
this.GetIP.Text = "WebRequest";
this.GetIP.Click += new System.EventHandler(this.GetIP_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 197);
this.Controls.Add(this.GetIP);
this.Controls.Add(this.IPAddr);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion

/**//// <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改变,自动发邮件到我的邮箱,发送邮件函数在我的文档可以找到
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义