public void TestRobot() { WebRobot robot = new WebRobot(); robot.IsAjax = true; var response = robot.Post("http://zhaokaifang.com/q.php", "application/x-www-form-urlencoded", "name=&idcard=1"); Console.WriteLine(response); }
public IEnumerable <CheckinEntry> GetEntries(QueryType queryType, string id) { string queryString = ""; if (queryType == QueryType.Name) { queryString = "name=" + id + "&idcard="; } else if (queryType == QueryType.IdCardNumber) { queryString = "name=&idcard=" + id; } else { throw new ArgumentException("Invalid query type: " + queryType); } var response = webRobot.Post(QueryPageUrl, "application/x-www-form-urlencoded", queryString); /* response: * <table cellspacing="0" cellpadding="0" class="tb2"> * <tr><th>姓名</th><th>证件</th><th>性别</th><th>生日</th><th>地址</th><th>手机</th><th>电话</th><th>邮箱</th><th>开房时间</th></tr> * <tr><td>周蓉琦</td><td>3</td><td>女</td><td></td><td>南大街14弄12号204-206室</td><td></td><td></td><td></td><td>2012-11-23 2:31:07</td></tr> * </table> */ var result = new List <CheckinEntry>(); int iFirstTr = response.IndexOf("<tr>"); if (iFirstTr < 0) { return(result); } int iTrBegin = response.IndexOf("<tr>", iFirstTr + 4); var lines = new List <string>(); while (iTrBegin >= 0) { int iTrEnd = response.IndexOf("</tr>", iTrBegin); if (iTrEnd < 0) { lines.Add((response + "</tr>").Substring(iTrBegin)); iTrEnd = response.Length; } else { lines.Add(response.Substring(iTrBegin, iTrEnd + 5 - iTrBegin)); } iTrBegin = response.IndexOf("<tr>", iTrEnd + 5); } for (int i = 0; i < lines.Count; ++i) { var line = lines[i].ToString(); line = line.Trim().Replace("\r\n", " ").Replace('\n', ' ').Replace('\r', ' ').Replace('\t', ' '); var match = Regex.Match(line, EntryLinePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); if (match.Success) { CheckinEntry entry = ParseCheckinEntryFromRegexMatch(match); result.Add(entry); } else { Trace.TraceError("[id = {0}] Cannot parse entry line: {1}", id, line); } } return(result); }