public string GetUrltoHtml(string Url)

來源:互聯網
上載者:User
摘自: 提供一個網頁抓取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;            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.