摘自: 提供一個網頁抓取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; } } }}