怎么实现c#获取ip内网,外网地址?
展开全部
rivate static string getIPAddress ( )
{
System.Net.IPAddress addr;
// 获得本机局域网IP地址
addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
return addr.ToString ( ) ;
读取计算机名称本机固定IP地址源程序
导入程序用到的名称空间
using System ;
using System.Net;
using System.Windows.Forms ;
using System.Drawing ;
public class Form3 : Form
{
//定义二个标签
private Label label1 ;
private Label label2 ;
public static void Main ( )
{
Application.Run ( new Form3 ( ) ) ;
}
// 构造窗体
public Form3 ( )
{
// 建立标签并且初始化
this.label1 = new System.Windows.Forms.Label ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
//先继承一个Label 类
label1.Location = new System.Drawing.Point ( 24 , 16 ) ;
label2.Location = new System.Drawing.Point ( 44 , 36 ) ;
//设定 Label的显示位置
label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ;
// 显示本机的计算机名称
label2.Text = "IP 地址:" + getIPAddress ( ) ;
// 显示本机的局域网IP地址
label1.Size = new System.Drawing.Size ( 200 , 50 ) ;
label2.Size = new System.Drawing.Size ( 200 , 80 ) ;
//设定标签的大小
label1.TabIndex = 0 ;
label2.TabIndex = 1 ;
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
// 设定标签的对齐方式
this.Text = "获得主机名称和IP地址!" ;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 8 , 16 ) ;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D ;
// 设定窗体的边界类型
this.ForeColor = System.Drawing.SystemColors.Desktop ;
this.Font = new System.Drawing.Font ( "宋体" , 10 , System.Drawing.FontStyle.Bold ) ;
// 设定字体、大小就字体的式样
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide ;
this.ClientSize = new System.Drawing.Size ( 250 , 250 ) ;
//把标签加到窗体中
this.Controls.Add ( this.label1 ) ;
this.Controls.Add ( this.label2 ) ;
}
private static string getIPAddress ( )
{
System.Net.IPAddress addr;
// 获得本机局域网IP地址
addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
return addr.ToString ( ) ;
}
}
{
System.Net.IPAddress addr;
// 获得本机局域网IP地址
addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
return addr.ToString ( ) ;
读取计算机名称本机固定IP地址源程序
导入程序用到的名称空间
using System ;
using System.Net;
using System.Windows.Forms ;
using System.Drawing ;
public class Form3 : Form
{
//定义二个标签
private Label label1 ;
private Label label2 ;
public static void Main ( )
{
Application.Run ( new Form3 ( ) ) ;
}
// 构造窗体
public Form3 ( )
{
// 建立标签并且初始化
this.label1 = new System.Windows.Forms.Label ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
//先继承一个Label 类
label1.Location = new System.Drawing.Point ( 24 , 16 ) ;
label2.Location = new System.Drawing.Point ( 44 , 36 ) ;
//设定 Label的显示位置
label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ;
// 显示本机的计算机名称
label2.Text = "IP 地址:" + getIPAddress ( ) ;
// 显示本机的局域网IP地址
label1.Size = new System.Drawing.Size ( 200 , 50 ) ;
label2.Size = new System.Drawing.Size ( 200 , 80 ) ;
//设定标签的大小
label1.TabIndex = 0 ;
label2.TabIndex = 1 ;
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
// 设定标签的对齐方式
this.Text = "获得主机名称和IP地址!" ;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 8 , 16 ) ;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D ;
// 设定窗体的边界类型
this.ForeColor = System.Drawing.SystemColors.Desktop ;
this.Font = new System.Drawing.Font ( "宋体" , 10 , System.Drawing.FontStyle.Bold ) ;
// 设定字体、大小就字体的式样
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide ;
this.ClientSize = new System.Drawing.Size ( 250 , 250 ) ;
//把标签加到窗体中
this.Controls.Add ( this.label1 ) ;
this.Controls.Add ( this.label2 ) ;
}
private static string getIPAddress ( )
{
System.Net.IPAddress addr;
// 获得本机局域网IP地址
addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
return addr.ToString ( ) ;
}
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
展开全部
获取真实IPview plaincopy to clipboardprint?
public static string GetRealIP()
{
string ip;
try
{
HttpRequest request = HttpContext.Current.Request;
if (request.ServerVariables["HTTP_VIA"] != null)
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
}
else
{
ip = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
}
return ip;
} public static string GetRealIP()
{
string ip;
try
{
HttpRequest request = HttpContext.Current.Request; if (request.ServerVariables["HTTP_VIA"] != null)
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
}
else
{
ip = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
} return ip;
}获取代理IPview plaincopy to clipboardprint?
public static string GetViaIP()
{
string viaIp = null;
try
{
HttpRequest request = HttpContext.Current.Request;
if (request.ServerVariables["HTTP_VIA"] != null)
{
viaIp = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
}
return viaIp; } 至于判断是否公网,将2个获取的IP进行比较即可,如果相等,则说明是公网用户,如果不相等,则是内网用户.
public static string GetRealIP()
{
string ip;
try
{
HttpRequest request = HttpContext.Current.Request;
if (request.ServerVariables["HTTP_VIA"] != null)
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
}
else
{
ip = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
}
return ip;
} public static string GetRealIP()
{
string ip;
try
{
HttpRequest request = HttpContext.Current.Request; if (request.ServerVariables["HTTP_VIA"] != null)
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
}
else
{
ip = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
} return ip;
}获取代理IPview plaincopy to clipboardprint?
public static string GetViaIP()
{
string viaIp = null;
try
{
HttpRequest request = HttpContext.Current.Request;
if (request.ServerVariables["HTTP_VIA"] != null)
{
viaIp = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
}
return viaIp; } 至于判断是否公网,将2个获取的IP进行比较即可,如果相等,则说明是公网用户,如果不相等,则是内网用户.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询