C#获取页面上的Html

 //根据Url地址得到网页的html源码
public static string GetWebContent(string Url)
{
  string strResult = "";
  try
  {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
    //声明一个HttpWebRequest请求
    request.Timeout = ;
    //设置连接超时时间
    request.Headers.Set("Pragma", "no-cache");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream streamReceive = response.GetResponseStream();
    Encoding encoding = Encoding.GetEncoding("GB2312");
    StreamReader streamReader = new StreamReader(streamReceive, encoding);
    strResult = streamReader.ReadToEnd();
  }
  catch
  {
    Console.WriteLine("出错");
  }
    return strResult;
  } public string Search(string argAddress)
{
  //要抓取的URL地址
  StringBuilder sb = new StringBuilder();
  //sb.Append("http://ditu.google.cn/maps");
  //sb.Append("?");
  //sb.Append("hl=zh-CN&newwindow=1&safe=strict");
  //sb.Append("&q=");
  //sb.Append(argAddress);
  //sb.Append("&bav=on.2,or.&bvm=bv.44158598,d.dGI&biw=1440&bih=775&um=1&ie=UTF-8&sa=N&tab=wl");
  sb.Append("http://query.customs.gov.cn/MNFTQ/MQuery.aspx");   //得到指定Url的源码
  string strWebContent = GetWebContent(sb.ToString());   ////生成HtmlDocument
  //WebBrowser webb = new WebBrowser();
  //webb.Navigate("about:blank");
  //HtmlDocument htmldoc = webb.Document.OpenNew(true);
  //htmldoc.Write(strWebContent);
  //HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("TR");
  //foreach (HtmlElement tr in htmlTR)
  //{
    // string address = string.Empty;     // try
    // {
      // string resultspanel = tr.Document.GetElementById("resultspanel").Document.GetElementById("panel_A_2").InnerText;       // string[] ContentLines = resultspanel.Split(new string[] { "\r\n" }, StringSplitOptions.None);//不忽略空行       // address = ContentLines[1];
    // }
    // catch { };     // //插入DataTable
    // if (address != string.Empty)
    // {
      // return address;
    // }
    // else
    // {
      // continue;
    // }
  //}   return strWebContent;
}
上一篇:关于Java 软件工程师应该知道或掌握的技术栈


下一篇:(转)没有IE就没有伤害!浏览器兼容性问题解决方案汇总