usingHtmlAgilityPack;usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Text;usingSystem.Threading;namespaceHtmlAnalyzeDemo
{public classHttpWebRequestBeginGetRequest
{private static ManualResetEvent allDone = new ManualResetEvent(false);static string postData = "{\"cateIds\":\"108698,2,108701,108703,108704,108705,108709,108712,108724,4\"}";private static string html = "";public static void Main(string[] args)
{
HttpWebRequest request= (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/aggsite/SubCategories");
request.Accept= "text/plain, */*; q=0.01";
request.Host= "www.cnblogs.com";
request.ContentType= "text/plain;charset=UTF-8";
request.ContentLength=postData.Length;
request.Method= "POST";//异步请求子菜单
request.BeginGetRequestStream(newAsyncCallback(GetRequestStreamCallback), request);
allDone.WaitOne();//将获取到的源代码转为流
byte[] array =Encoding.UTF8.GetBytes(html);
MemoryStream stream= newMemoryStream(array);
StreamReader reader= newStreamReader(stream);
HtmlDocument htmlDocumentSub= newHtmlDocument();//加载html成xml
htmlDocumentSub.Load(reader);//获取html首页的一级菜单
HtmlDocument htmlDocument = new HtmlWeb().Load("http://www.cnblogs.com");
HtmlNodeCollection htmlNodes= htmlDocument.DocumentNode.SelectNodes("//ul[@id='cate_item']/li");foreach (var node inhtmlNodes)
{string onmouseover = node.Attributes["onmouseover"].Value;string cateshowId=onmouseover.Split(new char[]{'(',')'})[1];
HtmlNodeCollection htmlCateNodes= htmlDocumentSub.DocumentNode.SelectNodes("//div[@id='cate_content_block_" + cateshowId + "']/div/ul/li");
Console.WriteLine("--" +node.InnerText);if (htmlCateNodes != null)
{foreach (var cateNode inhtmlCateNodes)
{
Console.WriteLine("----"+cateNode.InnerText);
}
}
}//Console.WriteLine(htmlDocumentSub.DocumentNode.InnerHtml);
}private static voidGetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request=(HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream=request.EndGetRequestStream(asynchronousResult);byte[] byteArray =Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray,0, postData.Length);
postStream.Close();//请求完成后异步读取数据
request.BeginGetResponse(newAsyncCallback(GetResponseCallback), request);
}private static voidGetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request=(HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response=(HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse=response.GetResponseStream();
StreamReader streamRead= newStreamReader(streamResponse);string responseString =streamRead.ReadToEnd();//请求完成的字符串保存到html中
html =responseString;
streamResponse.Close();
streamRead.Close();
response.Close();
allDone.Set();
}
}
}