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

我的解决方法:路由器的管理页面中有当前的主机IP地址

 ,

我写了一个程序,解析得到ip

用XPath对Html进行解析using System;
用XPath对Html进行解析
using System.Drawing;
用XPath对Html进行解析
using System.Collections;
用XPath对Html进行解析
using System.ComponentModel;
用XPath对Html进行解析
using System.Windows.Forms;
用XPath对Html进行解析
using System.Data;
用XPath对Html进行解析
using System.Net;
用XPath对Html进行解析
using System.IO;
用XPath对Html进行解析
using Sgml;
用XPath对Html进行解析
using System.Xml;
用XPath对Html进行解析
用XPath对Html进行解析
namespace GetIPAddress
{

用XPath对Html进行解析    
/// Form1 的摘要说明。
用XPath对Html进行解析    
/// </summary>
用XPath对Html进行解析    public class Form1 : System.Windows.Forms.Form
{
用XPath对Html进行解析        
private System.Windows.Forms.TextBox IPAddr;
用XPath对Html进行解析        
private System.Windows.Forms.Button GetIP;

用XPath对Html进行解析        
/// 必需的设计器变量。
用XPath对Html进行解析        
/// </summary>
用XPath对Html进行解析        private System.ComponentModel.Container components = null;
用XPath对Html进行解析
用XPath对Html进行解析        
public Form1()
{
用XPath对Html进行解析            
//
用XPath对Html进行解析            
// Windows 窗体设计器支持所必需的
用XPath对Html进行解析            
//
用XPath对Html进行解析
            InitializeComponent();
用XPath对Html进行解析
用XPath对Html进行解析            
//
用XPath对Html进行解析            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
用XPath对Html进行解析            
//
用XPath对Html进行解析
        }
用XPath对Html进行解析

用XPath对Html进行解析        
/// 清理所有正在使用的资源。
用XPath对Html进行解析        
/// </summary>
用XPath对Html进行解析        protected override void Dispose( bool disposing )
{
用XPath对Html进行解析            
if( disposing )
{
用XPath对Html进行解析                
if (components != null
{
用XPath对Html进行解析                    components.Dispose();
用XPath对Html进行解析                }

用XPath对Html进行解析            }

用XPath对Html进行解析            
base.Dispose( disposing );
用XPath对Html进行解析        }

用XPath对Html进行解析


用XPath对Html进行解析        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
用XPath对Html进行解析        
/// 此方法的内容。
用XPath对Html进行解析        
/// </summary>
用XPath对Html进行解析        private void InitializeComponent()
{
用XPath对Html进行解析            
this.IPAddr = new System.Windows.Forms.TextBox();
用XPath对Html进行解析            
this.GetIP = new System.Windows.Forms.Button();
用XPath对Html进行解析            
this.SuspendLayout();
用XPath对Html进行解析            
// 
用XPath对Html进行解析            
// IPAddr
用XPath对Html进行解析            
// 
用XPath对Html进行解析
            this.IPAddr.Enabled = false;
用XPath对Html进行解析            
this.IPAddr.Location = new System.Drawing.Point(3256);
用XPath对Html进行解析            
this.IPAddr.Name = "IPAddr";
用XPath对Html进行解析            
this.IPAddr.Size = new System.Drawing.Size(22421);
用XPath对Html进行解析            
this.IPAddr.TabIndex = 0;
用XPath对Html进行解析            
this.IPAddr.Text = "";
用XPath对Html进行解析            
// 
用XPath对Html进行解析            
// GetIP
用XPath对Html进行解析            
// 
用XPath对Html进行解析
            this.GetIP.Location = new System.Drawing.Point(96112);
用XPath对Html进行解析            
this.GetIP.Name = "GetIP";
用XPath对Html进行解析            
this.GetIP.Size = new System.Drawing.Size(8823);
用XPath对Html进行解析            
this.GetIP.TabIndex = 1;
用XPath对Html进行解析            
this.GetIP.Text = "WebRequest";
用XPath对Html进行解析            
this.GetIP.Click += new System.EventHandler(this.GetIP_Click);
用XPath对Html进行解析            
// 
用XPath对Html进行解析            
// Form1
用XPath对Html进行解析            
// 
用XPath对Html进行解析
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
用XPath对Html进行解析            
this.ClientSize = new System.Drawing.Size(296197);
用XPath对Html进行解析            
this.Controls.Add(this.GetIP);
用XPath对Html进行解析            
this.Controls.Add(this.IPAddr);
用XPath对Html进行解析            
this.Name = "Form1";
用XPath对Html进行解析            
this.Text = "Form1";
用XPath对Html进行解析            
this.ResumeLayout(false);
用XPath对Html进行解析
用XPath对Html进行解析        }

用XPath对Html进行解析        
#endregion
用XPath对Html进行解析

用XPath对Html进行解析        
/// 应用程序的主入口点。
用XPath对Html进行解析        
/// </summary>
用XPath对Html进行解析        [STAThread]
用XPath对Html进行解析        
static void Main() 
{
用XPath对Html进行解析            Application.Run(
new Form1());
用XPath对Html进行解析        }

用XPath对Html进行解析
用XPath对Html进行解析        
private void GetIP_Click(object sender, System.EventArgs e)
{
用XPath对Html进行解析            
string sPageUrl="http://192.168.1.1/userRpm/StatusRpm.htm";
用XPath对Html进行解析            
string sXPath="/html/body/center/form/table[2]/tr[2]/td/table/tr/td[2]/table/tr[2]";
用XPath对Html进行解析            
string sContent="";
用XPath对Html进行解析
用XPath对Html进行解析            WebRequest  request 
= WebRequest.Create(sPageUrl); 
用XPath对Html进行解析            request.Credentials
=new NetworkCredential("admin","admin");
用XPath对Html进行解析            WebResponse response 
= request.GetResponse(); 
用XPath对Html进行解析            Stream resStream 
= response.GetResponseStream();     
用XPath对Html进行解析            StreamReader sr 
= new StreamReader(resStream, System.Text.Encoding.Default); 
用XPath对Html进行解析
用XPath对Html进行解析            SgmlReader reader 
= new SgmlReader();
用XPath对Html进行解析            reader.DocType 
= "HTML";
用XPath对Html进行解析
用XPath对Html进行解析            reader.InputStream 
= new StringReader(sr.ReadToEnd());
用XPath对Html进行解析            StringWriter sw 
= new StringWriter();
用XPath对Html进行解析            XmlTextWriter writer 
= new XmlTextWriter(sw);
用XPath对Html进行解析            writer.Formatting 
= Formatting.Indented;
用XPath对Html进行解析            
while (reader.Read())
{
用XPath对Html进行解析                
if (reader.NodeType != XmlNodeType.Whitespace)
{
用XPath对Html进行解析                    writer.WriteNode(reader, 
true);
用XPath对Html进行解析                }

用XPath对Html进行解析            }

用XPath对Html进行解析            
用XPath对Html进行解析            sContent 
= sw.ToString();
用XPath对Html进行解析
用XPath对Html进行解析            
if (sXPath.Length == 0)
{
用XPath对Html进行解析                
this.IPAddr.Text = sw.ToString();
用XPath对Html进行解析            }

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

用XPath对Html进行解析                
this.IPAddr.Text = sb.ToString();
用XPath对Html进行解析            }

用XPath对Html进行解析
用XPath对Html进行解析            resStream.Close();  
用XPath对Html进行解析            sr.Close();
用XPath对Html进行解析        }

用XPath对Html进行解析    }

用XPath对Html进行解析}

用XPath对Html进行解析

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

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

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

相关文章:

  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2022-01-23
猜你喜欢
  • 2021-07-17
  • 2021-06-12
  • 2022-12-23
  • 2021-07-11
  • 2021-08-17
  • 2022-12-23
相关资源
相似解决方案