获取真实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;
    }
获取代理IP:
    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;
    }

http://www.cnblogs.com/jianphu/archive/2007/05/22/756041.html

取字符串的实用方法(超过一定长度自动换行)

1/// <summary>
 2        /// 截取字符串,不限制字符串长度
 3        /// </summary>
 4        /// <param name="str">待截取的字符串</param>
 5        /// <param name="len">每行的长度,多于这个长度自动换行</param>
 6        /// <returns></returns>

 7        public string CutStr(string str,int len)
 8        {    string s="";
 9            
10            for(int i=0;i<str.Length ;i++)
11            {
12                int r= i% len;
13                int last =(str.Length/len)*len;
14                if (i!=0 && i<=last)
15                {
16                    
17                    if( r==0)
18                    {
19                        s+=str.Substring(i-len,len)+"<br>";
20                    }

21                        
22                }

23                else if (i>last)
24                {
25                    s+=str.Substring(i-1) ;
26                    break;
27                }

28                
29            }

30            
31            return s;
32            
33        }

34
35
36        /// <summary>
37        /// 截取字符串并限制字符串长度,多于给定的长度+。。。
38        /// </summary>
39        /// <param name="str">待截取的字符串</param>
40        /// <param name="len">每行的长度,多于这个长度自动换行</param>
41        /// <param name="max">输出字符串最大的长度</param>
42        /// <returns></returns>

43        public string CutStr(string str,int len,int max)
44        {
45            string s="";
46            string sheng="";
47            if (str.Length >max)
48            {
49                str=str.Substring(0,max) ;
50                sheng="";
51            }

52            for(int i=0;i<str.Length ;i++)
53            {
54                int r= i% len;
55                int last =(str.Length/len)*len;
56                if (i!=0 && i<=last)
57                {
58                    
59                    if( r==0)
60                    {
61                        s+=str.Substring(i-len,len)+"<br>";
62                    }

63                        
64                }

65                else if (i>last)
66                {
67                    s+=str.Substring(i-1) ;
68                    break;
69                }

70                
71            }

72            
73            return s+sheng;
74            
75        }

多个域下共享Cookie的实现

必须设置同一个域

写Cookie的代码

 1HttpCookie userEmailCookie = new HttpCookie("GCEmail");
 2        userEmailCookie.Value = email.ToString();
 3        //userEmailCookie.Expires = DateTime.Now.AddYears(1); //浏览器关闭,cookie自动过期
 4        userEmailCookie.Domain = domain;
 5        HttpContext.Current.Response.Cookies.Add(userEmailCookie);
 6
 7        HttpCookie userIdCookie = new HttpCookie("GCUserID");
 8        userIdCookie.Value = userId.ToString();
 9        userIdCookie.Expires = DateTime.Now.AddYears(1);
10        userIdCookie.Domain = domain;
11        HttpContext.Current.Response.Cookies.Add(userIdCookie);                
12        
13        HttpCookie lastLoginTime = new HttpCookie("LastLoginTime");
14        lastLoginTime.Value = DateTime.Now.ToString();
15        //lastLoginTime.Expires = DateTime.Now.AddYears(1);
16        lastLoginTime.Domain = domain;
17        HttpContext.Current.Response.Cookies.Add(lastLoginTime);
18        
19        HttpCookie userRemark = new HttpCookie("GCRemark");
20        userRemark.Value = userRemarkInfo;
21        //userRemark.Expires = DateTime.Now.AddYears(1);
22        userRemark.Domain = domain;
23        HttpContext.Current.Response.Cookies.Add(userRemark);        


这里设置的域是有意义的,比如 你的域名为www.111.com
你的asp程序和asp.net程序都放在上面
那么,你那个设置域的地方都写 www.111.com 即可

2进制、8进制、10进制、16进制...各种进制间的轻松转换(c#)

.net Framework中,System.Convert类中提供了较为全面的各种类型、数值之间的转换功能。其中的两个方法可以轻松的实现各种进制的数值间的转换:

Convert.ToInt32(string value, int fromBase):

可以把不同进制数值的字符串转换为数字,其中fromBase参数为进制的格式,只能是2、8、10及16:

如Convert.ToInt32(”0010”,2)执行的结果为2;

Convert.ToString(int value, int toBase):

可以把一个数字转换为不同进制数值的字符串格式,其中toBase参数为进制的格式,只能是2、8、10及16:

如Convert.ToString(2,2)执行的结果为”0010”

现在我们做一个方法实现各种进制间的字符串自由转换:选把它转成数值型,然后再转成相应的进制的字符串:

public string ConvertString(string value, int fromBase, int toBase)

{

  int intValue = Convert.ToInt32(value, fromBase);

  return Convert.ToString(intValue, toBase);
}

其中fromBase为原来的格式

toBase为将要转换成的格式

posted on 2007-05-23 11:54  mbskys  阅读(202)  评论(0编辑  收藏  举报