private ResultInfo getDetail(string backstring) { backstring = backstring.Replace("<a title=\"aa\">", ""); backstring = backstring.Replace("xmlns=\"http://www.w3.org/1999/xhtml\"", ""); backstring = backstring.Replace("<br>", ""); SgmlReader reader = new SgmlReader(); reader.DocType = "HTML"; reader.InputStream = new StringReader(backstring); StringWriter sw = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(sw); reader.WhitespaceHandling = WhitespaceHandling.None; writer.Formatting = Formatting.Indented; while (reader.Read()) { if (reader.NodeType != XmlNodeType.Whitespace) { writer.WriteNode(reader, true); } } XmlDocument doc = new XmlDocument(); doc.Load(new StringReader(sw.ToString())); XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable); xnm.AddNamespace("bottum", "http://www.w3.org/1999/xhtml"); XPathNavigator nav = doc.CreateNavigator(); string xpath = "//html/body/table/tr[3]/td/table/tr[2]/td/table/tr[2]/td/table/tr[1]/td[2]/table/tr[2]/td/table[2]/tr/td"; XPathNodeIterator nodes = nav.Select(xpath, xnm); ResultInfo backinfo = new ResultInfo(querynum); if (nodes.Count > 2) { nodes.MoveNext(); nodes.MoveNext(); } for (int i = 1; i < nodes.Count / 2; i++) { nodes.MoveNext(); string time = nodes.Current.Value; time.Replace("\n", ""); time.Replace("\t", ""); nodes.MoveNext(); string state = nodes.Current.Value; state = state.Replace("\t", ""); state = state.Replace("\n", ""); backinfo.add(time, state); } string xpath2 = "//html/body/table/tr[3]/td/table/tr[2]/td/table/tr[2]/td/table/tr[1]/td[2]/table/tr[2]/td/table[3]/tr[2]/td"; XPathNodeIterator nodes2 = nav.Select(xpath2, xnm); if (nodes2.Count > 0) { nodes2.MoveNext(); string time = nodes2.Current.Value; time.Replace("\n", ""); time.Replace("\t", ""); nodes2.MoveNext(); string state = nodes2.Current.Value; state = state.Replace("\t", ""); state = state.Replace("\n", ""); backinfo.add(time, state); } reader.Close(); writer.Close(); sw.Close(); return(backinfo); }
public ResultInfo query() { try { string strURL = "http://www.zjs.com.cn/"; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); WebHeaderCollection requestHeader; requestHeader = new WebHeaderCollection(); request.KeepAlive = true; request.Method = "get"; ///////////////////////获取返回的会话id System.Net.HttpWebResponse responseMain; CookieContainer container; request.CookieContainer = new CookieContainer(); responseMain = (System.Net.HttpWebResponse)request.GetResponse(); container = request.CookieContainer; responseMain.Close(); ///////////设置请求验证码的图片头 HttpWebRequest requestPic; string urlPic = "http://www.zjs.com.cn/VerifyImg.aspx"; requestPic = (HttpWebRequest)HttpWebRequest.Create(urlPic); requestPic.CookieContainer = container; requestPic.Method = "get"; /////////////获取返回图片流 HttpWebResponse responsePic = (HttpWebResponse)requestPic.GetResponse(); requestPic.ContentType = " image/Gif"; container = requestPic.CookieContainer; string validcode = responsePic.Cookies["ValiCode"].Value; responsePic.Close(); ///////////构造返回流 string resultUrl = "http://www.zjs.com.cn/WS_Business/WS_Business_Tracking.aspx?id=5"; string postparam = String.Format("__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJMzE0MTYyNzU5D2QWAmYPZBYCAgMPZBYCAgEPZBYCAgEPZBYCZg8WAh4JaW5uZXJodG1sBaokPGRpdiBjbGFzcz0ibmF2Ij4NCiA8ZGl2IGNsYXNzPSJ0YWJsZSI%2BDQo8dWwgY2xhc3M9ImN1cnJlbnQiPg0KPGxpPjxhIGhyZWY9Imh0dHA6Ly93d3cuempzLmNvbS5jbiI%2BPGI%2B6aaW6aG1PC9iPjwhLS1baWYgSUUgN10%2BPCEtLT48L2E%2BPCEtLTwhW2VuZGlmXS0tPiA8IS0tW2lmIGx0ZSBJRSA2XT48dGFibGU%2BPHRyPjx0ZD48IVtlbmRpZl0tLT4NCjxkaXYgY2xhc3M9InNlbGVjdF9zdWIgc2hvdyI%2BDQo8dWwgY2xhc3M9InN1YiI%2BDQo8bGk%2BPGEgY2xhc3M9InN1Yl9zaG93IiBocmVmPSdpbmRleC5hc3B4Jz7pm4blm6LmgLvpg6g8L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NzPSJzZWxlY3Rfc3ViIj4NCjx1bCBjbGFzcz0ic3ViIj4NCjxsaT48YSBocmVmPSdXU19BYm91dHVzL1dTX0Fib3V0VXNfaW5kZXguYXNweD9pZD03Jz7lhazlj7jnroDku4s8L2E%2BPC9saT4NCjxsaT48YSBocmVmPSdXU19BYm91dHVzL1dTX0Fib3V0VXNfY3VsdHVyZS5hc3B4P2lkPTcnPuaWh%2BWMlueQhuW%2FtTwvYT48L2xpPg0KPGxpPjxhIGhyZWY9J1dTX0Fib3V0dXMvV1NfQWJvdXRVc19wcmVkb21pbmFuY2UuYXNweD9pZD03Jz7ojrflvpfojaPoqok8L2E%2BPC9saT4NCjxsaT48YSBocmVmPSdXU19BYm91dHVzL1dTX0Fib3V0VVNfZHV0eS5hc3B4P2lkPTcnPuekvuS8mui0o%2BS7uzwvYT48L2xpPg0KPGxpPjxhIGhyZWY9J1dTX0Fib3V0dXMvV1NfQWJvdXRVU19zdGFmZi5hc3B4P2lkPTcnPuWRmOW3peaVheS6izwvYT48L2xpPg0KPC91bD4NCjwvZGl2Pg0KPCEtLVtpZiBsdGUgSUUgNl0%2BPC90ZD48L3RyPjwvdGFibGU%2BPC9hPjwhW2VuZGlmXS0tPg0KPC9saT4NCjwvdWw%2BDQo8L2Rpdj4NCjwvZGl2Pg0KZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUcY3RsMDAkQ29udGVudDEkaW1hZ2VzQnV0dG9uMamFVqAeeEWwHOozlKKSkNd2%2BTkI&__PREVIOUSPAGE=6SWGQczFmPPy7XPSLYwhUQ2&__EVENTVALIDATION=%2FwEWBAKu3vWjDgKJ8LnPAQK1lr22CwKOnPioDfNcLIdF7cd7InVIMUFnJs8dnHHM&ctl00%24Content1%24TextBox1={0}&ctl00%24Content1%24ValidateTxt={1}&ctl00%24Content1%24imagesButton1.x=14&ctl00%24Content1%24imagesButton1.y=11", querynum, validcode); HttpWebRequest requestResult; requestResult = (HttpWebRequest)HttpWebRequest.Create(resultUrl); requestResult.CookieContainer = container; requestResult.Method = "post"; /////写入post参数 byte[] postparaBin = Encoding.ASCII.GetBytes(postparam); requestResult.ContentType = "application/x-www-form-urlencoded"; requestResult.ContentLength = postparaBin.Length; Stream outStream = requestResult.GetRequestStream(); outStream.Write(postparaBin, 0, postparaBin.Length); outStream.Close(); HttpWebResponse responseResult = (HttpWebResponse)requestResult.GetResponse(); Encoding encode = Encoding.GetEncoding("UTF-8"); ////////获取返回流并分析 System.IO.Stream s; s = responseResult.GetResponseStream(); StreamReader reader = new StreamReader(s, Encoding.GetEncoding("GB2312")); String backstring = reader.ReadToEnd(); reader.Close(); responseResult.Close(); ResultInfo backinfo = getDetail(backstring); return(backinfo); } catch (Exception e) { return(new ResultInfo(querynum)); } }