asp.net抓取hao123手机号码查询数据

作者:袖梨 2022-06-25

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.net;
using system.io;
using system.security.cryptography.x509certificates;
using system.net.security;
using system.security.cryptography;
using system.xml;

namespace ccbtext
{
    public partial class form2 : form
    {

        public form2()
        {
            initializecomponent();
        }

        private void form2_load(object sender, eventargs e)
        {
        }
             这个方法在这里没有用到,大家可以做为参考
        ///


        /// 传入url返回网页的html代码
        ///

        /// url
        ///
        public string geturltohtml(string url)
        {
            stringbuilder content = new stringbuilder();

            try
            {
                // 与指定url创建http请求
                httpwebrequest request = (httpwebrequest)webrequest.create(url);
                request.keepalive = false;
                // 获取对应http请求的响应
                httpwebresponse response = (httpwebresponse)request.getresponse();
                // 获取响应流
                stream responsestream = response.getresponsestream();
                // 对接响应流(以"gbk"字符集)
                streamreader sreader = new streamreader(responsestream, encoding.getencoding("utf-8"));
                // 开始读取数据
                char[] sreaderbuffer = new char[256];
                int count = sreader.read(sreaderbuffer, 0, 256);
                while (count > 0)
                {
                    string temps教程tr = new string(sreaderbuffer, 0, count);
                    content.append(tempstr);
                    count = sreader.read(sreaderbuffer, 0, 256);
                }
                // 读取结束
                sreader.close();
            }
            catch (exception)
            {
                content = new stringbuilder("runtime error");
            }

            return content.tostring();

        }


        ///


        /// 好123查询,符合下列规则也可使用
        /// 返回xml
        /// 需要顺序的节点:
        /// queryresult(查询结果状态true,false)
        /// province(所属省份)
        /// city(所属地区)
        /// corp(服务商)
        /// card(卡类型 gsm)
        /// areacode(区号)
        /// postcode(邮编)
        ///

        ///
        ///
        ///
        public static string[] getinfobyxml(string url, string mobilenum)
        {
            try
            {
                xmldocument xml = new xmldocument();
                // xml.loadxml("15890636739true河南郑州0371450000中国移动gsm");
                xml.load(string.format(url, mobilenum));
                xmlnamespacemanager xmlnm = new xmlnamespacemanager(xml.nametable);
                xmlnm.addnamespace("content", "http://api.showji.com/locating/");
                xmlnodelist nodes = xml.selectnodes("//content:queryresult|//content:mobile|//content:province|//content:city|//content:corp|//content:card|//content:areacode|//content:postcode", xmlnm);
                if (nodes.count == 8)
                {
                    if ("true".equals(nodes[1].innertext))
                    {

                        return new string[] { nodes[0].innertext, nodes[2].innertext + nodes[3].innertext, nodes[6].innertext + nodes[7].innertext, nodes[4].innertext, nodes[5].innertext };
                    }
                }
                return new string[] { "false" };
            }
            catch
            {
                return new string[] { "false" };
            }
        }

        //调用方法查询数据
        private void button1_click(object sender, eventargs e)
        {
            foreach (string item in getinfobyxml(" http://vip.showji.com/locating/?m={0}", txtmobile.text.trim()))
            {
                richtextbox1.text += "__" + item;
            }
        }
    }
}


是用winfrom做的测试,大家如果想用asp教程。net也是一样的,把我的方法复制到你的web页面的cs代码下就ok了。

好了我们的分析到这里就算是结束了,

在这里我再给大空补充一个关于调用带有证书的网站的调用 方法

因为带证书的都是在要验证证书文件的,我们在这里直接让他在本地回调验证,这样的话就要吧重写方法了,下在看一下回调的方法吧

//回调验证证书问题

        public bool checkvalidationresult(object sender, x509certificate certificate, x509chain chain, sslpolicyerrors errors)
        {   // 总是接受   
            return true;
        }

其它很简单只要在 我们上面的方法geturltohtml()中加入几行代码就行了,修改后的方法

///


        /// 传入url返回网页的html代码
        ///

        /// url
        ///
        public string geturltohtml(string url)
        {
            stringbuilder content = new stringbuilder();
            try
            {
                // 与指定url创建http请求
                servicepointmanager.servercertificatevalidationcallback = new system.net.security.remotecertificatevalidationcallback(checkvalidationresult);//验证

                httpwebrequest request = (httpwebrequest)webrequest.create(url);
                request.keepalive = false;
                request.useragent = "mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/5.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; infopath.2; ms-rtc lm 8; .net4.0c; .net4.0e)";
                request.method = "get";
                request.accept = "*/*";

                //创建证书文件
                x509certificate objx509 = new x509certificate(application.startuppath + "123.cer");

                //添加到请求里
                request.clientcertificates.add(objx509);

                httpwebresponse response = (httpwebresponse)request.getresponse();
                // 获取对应http请求的响应
                // 获取响应流
                stream responsestream = response.getresponsestream();
                // 对接响应流(以"gbk"字符集)
                streamreader sreader = new streamreader(responsestream, encoding.getencoding("gbk"));
                // 开始读取数据
                char[] sreaderbuffer = new char[256];
                int count = sreader.read(sreaderbuffer, 0, 256);
                while (count > 0)
                {
                    string tempstr = new string(sreaderbuffer, 0, count);
                    content.append(tempstr);
                    count = sreader.read(sreaderbuffer, 0, 256);
                }
                // 读取结束
                sreader.close();
            }
            catch (exception)
            {
                content = new stringbuilder("runtime error");
            }

            return content.tostring();

        }

 

相关文章

精彩推荐