c#ip138自动获取代码

好久没有写文章了今天拿个小例子来练练手吧,2011年开个头。在这里先祝大家兔年大吉大利,祝愿博客园越办越好!!!

网上其它也有不少这样的方法,我是根据自己的想法来实现几个,正好也利用一个URl Get请求的方法来取得页面信息,并分析的过程,希望能给新手带来一些帮助。

先来看第一个手机号码信息查询:

我们打开http://www.ip138.com:8080/search.asp 

  

只要我们输入自己的手机号码就会得到如下信息,那怎么样把这些信息取出来呢,当我们单击查询时看Url的变化

http://www.ip138.com:8080/search.asp?mobile=13781033951&action=mobile

那我们现在是不是只要这样写上一行代码就可以随时变化手机号呢?

View Code
   
   
"http://www.ip138.com:8080/search.asp?action=mobile&mobile=" + number.Trim()

 然后我们通过下面一个方法来取得请求的页面的HTML代码,就是如上图提示界面的HtML代码

View Code
   
   
/// <summary>
/// 传入URL返回网页的html代码
/// </summary>

/// <param name="Url">URL</param>
/// <returns></returns>
public static string GetUrltoHtml(string Url)
{
try

{
System.Net.WebRequest wReq
= System.Net.WebRequest.Create(Url);
// Get the response instance.

System.Net.WebResponse wResp = wReq.GetResponse();
//
Read an HTTP-specific property
//
if (wResp.GetType() ==HttpWebResponse)
//
{
//
DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;
//
}
// Get the response stream.

System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)

  using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.Default))
            {
                 return reader.ReadToEnd();

            }
}
catch
(System.Exception ex)
{
//errorMsg = ex.Message;

}
return ""
;
}

 通过上面的方法我们可以得到Hthl信息,

我们来分析一下这个网页的内容

具体的过程我就不多说的,大家应该都很容易能想到,我把分析后得到的代码写出来吧

  
  

/// <summary>
/// 输入手机号码得到归属地信息
/// </summary>

/// <param name="number">手机号码</param>
/// <returns>数组类型0为归属地,1卡类型,2区 号,3邮 编</returns>
public static string[] getTelldate(string number)
{
try

{
string strSource = GetUrltoHtml("http://www.ip138.com:8080/search.asp?action=mobile&mobile=" + number.Trim());
//归属地

strSource = strSource.Substring(strSource.IndexOf(number));
strSource
=
StripHTML(strSource);
strSource
= strSource.Replace("/r", ""
);
strSource
= strSource.Replace("/n", ""
);
strSource
= strSource.Replace("/t", ""
);
strSource
= strSource.Replace("&nbsp;", ""
);
strSource
= strSource.Replace("-->", ""
);
string[] strnumber = strSource.Split(new string[] { "归属地", "卡类型", "邮 编", "区 号", "更详细", "卡号"
}, StringSplitOptions.RemoveEmptyEntries);
string[] strnumber1 = null
;
if (strnumber.Length > 4
)
{
strnumber1
= new string[] { strnumber[1].Trim(), strnumber[2].Trim(), strnumber[3].Trim(), strnumber[4
].Trim() };
}
return
strnumber1;
}
catch
(Exception)
{
return null
;
}
}

这里还有一个过滤HTML代码的方法提供如下

  
  
/// <summary>
/// 过滤html标签
/// </summary>

/// <param name="strHtml">html的内容</param>
/// <returns></returns>
public static string StripHTML(string stringToStrip)
{
// paring using RegEx
//
stringToStrip = Regex.Replace(stringToStrip, "</p(?://s*)>(?://s*)<p(?://s*)>", "/n/n", RegexOptions.IgnoreCase |
RegexOptions.Compiled);
stringToStrip
= Regex.Replace(stringToStrip, "<br(?://s*)/>", "/n", RegexOptions.IgnoreCase |
RegexOptions.Compiled);
stringToStrip
= Regex.Replace(stringToStrip, "/"", "''", RegexOptions.IgnoreCase | RegexOptions.Compiled);

stringToStrip = StripHtmlXmlTags(stringToStrip);
return
stringToStrip;
}

private static string StripHtmlXmlTags(string
content)
{
return Regex.Replace(content, "<[^>]+>", "", RegexOptions.IgnoreCase |
RegexOptions.Compiled);
}

这样的话我们就取到我们想要的信息啦,然后生成一个数组直接调用方法Ok

在原来的基础上我们还可以把身份证信息的查询出来

方法如下

View Code
   
   
/// <summary>
/// 输入身份证号得到相应信息
/// </summary>

/// <param name="number">手机号码</param>
/// <returns>数组类型0为性别,1出生日期,2发证地</returns>
public static string[] getCardate(string number)
{
try

{
string strSource = GetUrltoHtml("http://qq.ip138.com/idsearch/index.asp?action=idcard&userid=" + number.Trim() + "&B1=%B2%E9+%D1%AF" );
strSource
= strSource.Substring(strSource.IndexOf("查询结果"
));
strSource
=
StripHTML(strSource);
strSource
= strSource.Replace("/r", ""
);
strSource
= strSource.Replace("/n", ""
);
strSource
= strSource.Replace("/t", ""
);
strSource
= strSource.Replace("&nbsp;", ""
);
strSource
= strSource.Replace("查询结果 * ++", ""
);
strSource
= strSource.Replace("-->", ""
);
string[] strnumber = strSource.Split(new string[] { "性别:", "出生日期:", "发证地:", "点击这里查询验证身份", "提示:"
}, StringSplitOptions.RemoveEmptyEntries);
string[] strnumber1 = null
;
if (strnumber.Length > 3
)
{
strnumber1
= new string[] { strnumber[1].Trim(), strnumber[2].Trim(), strnumber[3
].Trim() };
}

return
strnumber1;
}
catch
(Exception)
{
return null
;
}
}

自己没事动手写了个,虽然不怎么样但是拿出来大家看看吧,呵呵,这还可以使用正则来实现,而且非常方便,

我们还以第一个查询手机号的为例子来看吧

  
  
//归属地
string ad = GetMatch(Getaddress(), "class=tdc2>(.*)</TD>").Replace("&nbsp;", "_" );
string[] str1 = ad.Split('_'
);
Privoice
= str1[0
].ToString().Trim();
City
= str1[1
].ToString().Trim();

GetMatch()方法的代码如下所示。

  
  

//得到数据
public static string GetMatch(string strSource, string strRegex)
{
try

{
Regex r;
MatchCollection m;
r
= new Regex(strRegex, RegexOptions.IgnoreCase);
m
=
r.Matches(strSource);

if (m.Count <= 0) return ""
;

return m[1].Groups[1
].Value;
}
catch

{
return "" ;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值