调用ip138的页面获取IP地址

本源码类参考http: // p3p3pp3.yo2.cn开发的动态DNS程序
http: // www.ajaxcn.net/archives/1671
  using  System;
using  System.Collections.Generic;
using  System.IO;
using  System.Net;
using  System.Runtime.Serialization.Json;
using  System.Text;
 
namespace  tphp
{
    
///   <summary>
    
///  REST方式指令工具
    
///   </summary>
     public   class  RESTCommand
    {
        
///   <summary>
        
///  发送指令
        
///   </summary>
        
///   <param name="URL"> 所在URL </param>
        
///   <param name="postinfo"> 指令信息 </param>
        
///   <returns> 返回的JSON结果 </returns>
         public   static   string  SendCommand( string  URL,  string  postinfo)
        {
            
return  htmlGetter(URL, postinfo,  " UTF-8 " true null null );
        }
 
 
        
public   static   string  htmlGetter( string  URL,  string  post,  string  codename,  bool  isPost,  string [] headinfo,  string  Referer)
        {
 
            Encoding myEncoding 
=  Encoding.GetEncoding(codename);
 
            HttpWebRequest request 
=  (HttpWebRequest)HttpWebRequest.Create(URL);
            request.ServicePoint.Expect100Continue 
=   false ;
 
            
byte [] byteData  =  myEncoding.GetBytes(post);
            
if  (isPost)
            {
                request.Method 
=   " POST " ;
            }
            request.ContentType 
=   " application/x-www-form-urlencoded " ;
            request.ContentLength 
=  byteData.Length;
            request.UserAgent 
=   " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727) " ;
            
if  (Referer  !=   null )
            {
                request.Referer 
=  Referer;
            }
 
            
if  (headinfo  !=   null )
            {
                
for  ( int  i  =   0 ; i  <  headinfo.Length; i ++ )
                {
                    request.Headers.Add(headinfo[i]);
                }
            }
 
            
// if (_isDebug)
            
// {
            
//     WebProxy myproxy = new WebProxy("127.0.0.1", 8888);
            
//     request.Proxy = myproxy;
            
// }
 
            
if  (isPost)
            {
                
using  (Stream requestStream  =  request.GetRequestStream())
                {
                    requestStream.Write(byteData, 
0 , byteData.Length);
                }
            }
 
            
try
            {
                
// 处理响应  
                 using  (HttpWebResponse response  =  (HttpWebResponse)request.GetResponse())
                {
                    
using  (StreamReader sr2  =   new  StreamReader(response.GetResponseStream(), myEncoding))
                    {
                        
string  rhtml  =  sr2.ReadToEnd();
                        
return  rhtml;
                    }
                }
            }
            
catch  (WebException ex)
            {
                
throw ;
            }
        }
    }
 
    
///   <summary>
    
///  序列化工具
    
///   </summary>
     public   class  SerializationTool
    {
        
///   <summary>
        
///  从Jsontext获取实体
        
///   </summary>
        
///   <typeparam name="T"></typeparam>
        
///   <param name="JsonText"></param>
        
///   <returns></returns>
         public   static  T GetObj < T > ( string  JsonText)
        {
            DataContractJsonSerializer ds 
=   new  DataContractJsonSerializer( typeof (T));
            MemoryStream ms 
=   new  MemoryStream(Encoding.UTF8.GetBytes(JsonText));
            T obj 
=  (T)ds.ReadObject(ms);
            ms.Close();
 
            
return  obj;
        }
 
        
///   <summary>
        
///  从实体转换到JsonText
        
///   </summary>
        
///   <param name="myobj"> 实体实例 </param>
        
///   <returns></returns>
         public   static   string  ParseObj( object  myobj)
        {
            DataContractJsonSerializer ds 
=   new  DataContractJsonSerializer(myobj.GetType());
            MemoryStream ms2 
=   new  MemoryStream();
            ds.WriteObject(ms2, myobj);
            
string  JSONString  =  Encoding.UTF8.GetString(ms2.ToArray());
            ms2.Close();
 
            
return  JSONString;
        }
    }
}
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Text.RegularExpressions;
 
namespace  tphp
{
    
public   partial   class  _Default : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
string  pagehtml  =  RESTCommand.htmlGetter( @" http://www.ip138.com/ip2city.asp " "" " gb2312 " false null null );
            
string  ipregex  =   @" 您的IP地址是:\[(?<ip>.*)\] " ;
 
            Regex myRegex 
=   new  Regex(ipregex);
            Match mymatch 
=  myRegex.Match(pagehtml);
 
 
            
string  ipaddress  =  mymatch.Groups[ " ip " ].Captures[ 0 ].Value;
            Response.Write(ipaddress);
        }
    }
}

调用ip138的页面获取IP地址

你可能感兴趣的:(IP地址)