public string GetUrltoHtml(string Url)

摘自: 提供一个网页抓取hao123手机号码归属地的例子 http://www.cnblogs.com/sufei/archive/2011/04/29/2033036.html
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)
        {
        }
             这个方法在这里没有用到,大家可以做为参考
        /// <summary>
        /// 传入URL返回网页的html代码
        /// </summary>
        /// <param name="Url">URL</param>
        /// <returns></returns>
        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 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();

        }


        /// <summary>
        /// 好123查询,符合下列规则也可使用
        /// 返回xml
        /// 需要顺序的节点:
        /// QueryResult(查询结果状态True,False)
        /// Province(所属省份)
        /// City(所属地区)
        /// Corp(服务商)
        /// Card(卡类型 GSM)
        /// AreaCode(区号)
        /// PostCode(邮编)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="mobileNum"></param>
        /// <returns></returns>
        public static string[] GetInfoByxml(string url, string mobileNum)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                // xml.LoadXml("<?xml version='1.0' encoding='utf-8' ?><QueryResponse xmlns='http://api.showji.com/Locating/'><Mobile>15890636739</Mobile><QueryResult>True</QueryResult><Province>河南</Province><City>郑州</City><AreaCode>0371</AreaCode><PostCode>450000</PostCode><Corp>中国移动</Corp><Card>GSM</Card></QueryResponse>");
                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;
            }
        }
    }
}

posted @ 2011-05-01 12:39  彭成刚  阅读(567)  评论(0编辑  收藏  举报