c#ip138自动获取代码
来源:互联网 发布:身份证信息读取器 数据 编辑:程序博客网 时间:2024/05/11 00:39
好久没有写文章了今天拿个小例子来练练手吧,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(" ", "");
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(" ", "");
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(" ", "_");
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 "";
}
}
- c#ip138自动获取代码
- linux c方式 从www.ip138.com获取IP数据
- C语言使用socket通过IP138获取外网IP
- ip138
- ASP.NET通过IP138来获取外面IP地址
- C#-自动获取IP
- 自动获取城市天气代码
- java调用IP138获取本机IP以及通过IP地址获取区域名称
- [C#]一步一步开发自己的自动代码生成工具之一:获取SQL2005的数据库表结构
- [c#]一步一步开发自己的自动代码生成工具之一:获取sql2005的数据库表结构
- VIM自动格式化C代码
- 通过www.ip138.com获取本机外网ip以及所在地理位置
- 从IP138上获取数据,查询多个IP的归宿地
- 自动编码获取页面html代码
- 自动获取用户屏幕分辨率的JS代码
- php 教程自动获取关键字代码
- JDBC获取Mysql自动增长字段代码
- [C#] C#代码获取机器码
- Android:推荐学习
- teamviewer远程连接号
- 通过Openlayers实现实时定位
- 使用SPQuery进行条件查询和排序
- shell脚本读写文本文件
- c#ip138自动获取代码
- Ubuntu下pdf、gedit、vim 中文无法读取或者显示乱码的解决方法
- tomcat6 数据库链接配置
- GeoJSON and GeoJSON Plugin for GeoServer
- Windows上安装Subversion+Apache server
- 构建高性能ASP.NET站点 开篇
- 不再纠结的NOKIA
- 在一字符串中统计单词
- 构建高性能ASP.NET站点之一 剖析页面的处理过程(前端)