C#获取页面上的Html
//根据url地址得到网页的html源码
public static string getwebcontent(string url)
{
string strresult = "";
try
{
httpwebrequest request = (httpwebrequest)webrequest.create(url);
//声明一个httpwebrequest请求
request.timeout = 30000;
//设置连接超时时间
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;
}