1 //根据Url地址得到网页的html源码 2 public static string GetWebContent(string Url) 3 { 4 string strResult = ""; 5 try 6 { 7 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 8 //声明一个HttpWebRequest请求 9 request.Timeout = 30000; 10 //设置连接超时时间 11 request.Headers.Set("Pragma", "no-cache"); 12 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 13 Stream streamReceive = response.GetResponseStream(); 14 Encoding encoding = Encoding.GetEncoding("GB2312"); 15 StreamReader streamReader = new StreamReader(streamReceive, encoding); 16 strResult = streamReader.ReadToEnd(); 17 } 18 catch 19 { 20 Console.WriteLine("出错"); 21 } 22 return strResult; 23 } 24 25 26 27 public string Search(string argAddress) 28 { 29 //要抓取的URL地址 30 StringBuilder sb = new StringBuilder(); 31 //sb.Append("http://ditu.google.cn/maps"); 32 //sb.Append("?"); 33 //sb.Append("hl=zh-CN&newwindow=1&safe=strict"); 34 //sb.Append("&q="); 35 //sb.Append(argAddress); 36 //sb.Append("&bav=on.2,or.&bvm=bv.44158598,d.dGI&biw=1440&bih=775&um=1&ie=UTF-8&sa=N&tab=wl"); 37 sb.Append("http://query.customs.gov.cn/MNFTQ/MQuery.aspx"); 38 39 //得到指定Url的源码 40 string strWebContent = GetWebContent(sb.ToString()); 41 42 ////生成HtmlDocument 43 //WebBrowser webb = new WebBrowser(); 44 //webb.Navigate("about:blank"); 45 //HtmlDocument htmldoc = webb.Document.OpenNew(true); 46 //htmldoc.Write(strWebContent); 47 //HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("TR"); 48 //foreach (HtmlElement tr in htmlTR) 49 //{ 50 // string address = string.Empty; 51 52 53 // try 54 // { 55 // string resultspanel = tr.Document.GetElementById("resultspanel").Document.GetElementById("panel_A_2").InnerText; 56 57 // string[] ContentLines = resultspanel.Split(new string[] { " " }, StringSplitOptions.None);//不忽略空行 58 59 // address = ContentLines[1]; 60 // } 61 // catch { }; 62 63 64 // //插入DataTable 65 // if (address != string.Empty) 66 // { 67 // return address; 68 // } 69 // else 70 // { 71 // continue; 72 // } 73 //} 74 75 return strWebContent; 76 }