c#基础篇
来源:互联网 发布:ubuntu安装mysql失败 编辑:程序博客网 时间:2024/05/16 14:41
1.readonly 和const常量
(1)const只能在字段的声明语句中初始化,而readonly可以在声明语句和构造函数中都可以,readonly 字段被赋值了就不能改变
(2)readonly可以是实例字段也可以是静态字段
(3)readonly分配内存,const不分配
(4)const的值是在编译期决定的,readonly则是在运行期
获取公网ip
/// <summary>
/// 获取本机的上网IP
/// </summary>
/// <returns></returns>
private string GetConnectNetAddress()
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站返回的数据 格式:您的IP地址是:[x.x.x.x]
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "").Replace("<", "");
return ip;
}
/// 获取本机的上网IP
/// </summary>
/// <returns></returns>
private string GetConnectNetAddress()
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站返回的数据 格式:您的IP地址是:[x.x.x.x]
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "").Replace("<", "");
return ip;
}
/// <summary>
/// 获取用户远程ip
/// </summary>
/// <returns></returns>
private string GetUserHostIp()
{
string ip = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
else if ((string.IsNullOrEmpty(ip) || ip.Length <= 0) && HttpContext.Current.Request.ServerVariables["Proxy-Client-IP"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["Proxy-Client-IP"];
}
else if ((string.IsNullOrEmpty(ip) || ip.Length <= 0) && HttpContext.Current.Request.ServerVariables["WL-Proxy-Client-IP"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["WL-Proxy-Client-IP"];
}
else if ((string.IsNullOrEmpty(ip) || ip.Length <= 0) && HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else if (string.IsNullOrEmpty(ip) || ip.Length <= 0)
{
ip = HttpContext.Current.Request.UserHostAddress;
}
return ip;
}
/// 获取用户远程ip
/// </summary>
/// <returns></returns>
private string GetUserHostIp()
{
string ip = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
else if ((string.IsNullOrEmpty(ip) || ip.Length <= 0) && HttpContext.Current.Request.ServerVariables["Proxy-Client-IP"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["Proxy-Client-IP"];
}
else if ((string.IsNullOrEmpty(ip) || ip.Length <= 0) && HttpContext.Current.Request.ServerVariables["WL-Proxy-Client-IP"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["WL-Proxy-Client-IP"];
}
else if ((string.IsNullOrEmpty(ip) || ip.Length <= 0) && HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else if (string.IsNullOrEmpty(ip) || ip.Length <= 0)
{
ip = HttpContext.Current.Request.UserHostAddress;
}
return ip;
}
0 0
- C#基础篇---1
- C#基础篇
- C#之基础篇
- C#基础篇1
- C#篇之---基础
- c#基础篇
- C#基础篇01
- C#基础篇一
- C#学习基础篇----基础语法
- 二、C#学习基础篇----基础语法
- C#操作Excel---基础篇
- C#反射基础--代码篇
- C#基础篇总结(二)
- 【c#学习笔记】 基础篇
- C#学习指南1(基础篇)
- c#基础
- c#基础
- C#基础
- pat简单题:分支-18. 求一元二次方程的根
- 欢迎使用CSDN-markdown编辑器
- 快速排序
- 单例的多线程安全
- block专题
- c#基础篇
- HDU 5202 非回文串输出 DFS搜
- 树 Minimum Depth of Binary Tree
- 知识就是力量:什么是 HTML?
- 學習計劃-基礎前行
- 爱的故事
- Reverse Integer
- 第一章第七题
- spring单例和多例详解。如何在单例中调用多例对象