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("&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 "";
}
}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 车子锁了油箱盖能开怎么办 单位不接受档案和户口怎么办 完税凭证弄丢了怎么办 育种玉米公本早了怎么办 网银转账打错了怎么办 转账名字打错了怎么办 普通转账名字打错了怎么办 银行账号转错了怎么办 打款信息错了怎么办 报到证过期了档案怎么办 日本suica卡丢了怎么办 日本地铁卡丢了怎么办 极光卡五星老输怎么办 皮秒留下的色沉怎么办 鼻综合鼻内增生怎么办 做完鼻综合脸肿怎么办 上海车子卖了etc怎么办 车卖了etc设备怎么办 车卖了etc没摘怎么办 交通信息卡丢了怎么办 多囊卵巢无排卵怎么办 卵巢腹镜手术后一直出汗怎么办 双侧卵巢多囊怎么办 备孕子宫内膜厚怎么办 有成熟卵泡不破怎么办 卵泡不排萎缩了怎么办 卵泡8mm一直不长怎么办 卵泡已经长到28x19mm怎么办 多囊卵巢卵泡长不大怎么办 优势卵泡19不排怎么办 多囊卵泡不排卵怎么办 卵泡两天长2mm怎么办 子宫小43*38*26怎么办 优势卵泡打破卵针后并不破怎么办 ktv禁止自带酒水怎么办 记名西瓜卡丢了怎么办 日本电车卡丢了怎么办 网贷暂时没钱还怎么办 华泰倒闭了汽车怎么办 猫躲起来找不到了怎么办 狗生病了不吃饭怎么办