如何模拟点击页面并获得并分析返回的结果 还有一个麻烦是:另外的那个网站上,查询参数不是带在它的地址中的。在它上面做手工查询时,有一个textbox,在其中输入待查询内容,然后点击页面上某一个button,然后得到查询结果。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 查看该目标页面的html,找到你所要点击的查询按钮所在的Form的PostUrl ,根据这个地址先创建一个指向目标网站的 HttpWebRequest 对象.根据这个Form的获取信息方式是get还是post, 向这个HttpWebRequest 插入数据, 注意参数名一定要跟目标网站获取输入的控件id一致. 然后补充(如果有的话)所有该Form下额外的input(包括hidden控件)的值. 你的HttpWebRequest调用GetResponse方法可以得到查询结果的html,自己分析这些字符串,获取想要的信息.难点: 如果目标站点有安全校验方式(比如图片验证码),比如ASPNET的ViewState等,你需要模拟这些值,是相当困难的. Thank you very much for your clue. 我刚试了一下,为什么得不到所需的数据?Code如下:using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { //string strId = "guest"; //string strPassword = "123456"; //string fid = "15"; string query = "txtLastName=xie&txtFirstName=&reqOption=HRPPNameEntry&advanced=&action=name_search&pane=bottom"; ASCIIEncoding encoding = new ASCIIEncoding(); // string postData="userid="+strId; // postData += ("&password="+strPassword); string postData; postData = query; string myurl = "https://www.hrpassport.cs.bns/hr/hrpassport"; byte[] data = encoding.GetBytes(postData); // Prepare web request...1 // System.Net.HttpWebRequest myRequest = // (System.Net.HttpWebRequest)WebRequest.Create("http://www.popyard.com/cgi-bin/artical.cgi"); System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)WebRequest.Create(myurl); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; Stream newStream = myRequest.GetRequestStream(); // Send the data. newStream.Write(data, 0, data.Length); newStream.Close(); // Get response HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default); string content = reader.ReadToEnd(); Console.WriteLine(content); } }}返回信息:"\r\n\r\n\r\n\r\n\r\n<HTML>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\" />\r\n<title>HR Passport (cs1il601)</title>\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"Common/StyleSheets/HRStyleSheet.css\" />\r\n\r\n<script>\r\n\r\nsetTimeout(\"top.location = '/hr/hrpassport'\", 9000);\r\n\r\n</script>\r\n\r\n</head>\r\n<BODY BACKGROUND=\"Common/Images/bg_globe.gif\" BGCOLOR=\"#FFFFFF\" TOPMARGIN=\"0\" LEFTMARGIN=\"0\" MARGINWIDTH=\"0\" MARGINHEIGHT=\"0\">\r\n \r\n<table border=\"0\" width=\"448\">\r\n <tr>\r\n <td width=\"20\"> \r\n <p> </td>\r\n <td width=\"350\" height=\"134\" align=\"center\" valign=\"bottom\"><font face=\"Verdana\" size=\"-1\">\r\n\tWe're sorry, but there was an error while you were browsing the HR PASSPORT web site.<br>\r\n\t\r\n</font></td>\r\n <td width=\"*\"></td>\r\n </tr>\r\n <tr>\r\n <td width=\"20\"></td>\r\n <td width=\"350\" height=\"134\" align=\"center\" valign=\"middle\">\r\n\t<table border=\"1\">\r\n\t <tr><td><font face=\"Verdana\" size=\"-1\">\r\n\t\r\n\t\tjava.lang.Exception\r\n\t\r\n\t</font></td></tr>\r\n\t</table>\r\n <td width=\"*\"></td>\r\n </tr>\r\n\r\n <tr>\r\n <td width=\"20\"></td>\r\n <td width=\"350\" height=\"134\" align=\"center\" valign=\"top\"><font face=\"Verdana\" size=\"-1\">\r\n....returning you to the home page for HR PASSPORT..... </font></td>\r\n <td width=\"*\"></td>\r\n </tr>\r\n\r\n</table>\r\n</BODY>\r\n\t</HTML>"我觉得大概是被检测到有什么举动,是不是HttpWebRequest是能够被检测到?如何绕过这个?谢谢。 自己顶一下。可能出问题的地方是:我是通过VPN连到目标站点的。难道这会是问题所在?我直接在IE中输入URL地址一点问题都没有啊?我还是怀疑那个站点能够知道我是通过HttpWebRequest进去的。哪位大侠知道是什么原因?谢谢。 问题以解决,需要设置一下Proxy 能不能扫描出某个网站目录中都有哪些文件? 同步加载JS文件问题! windows7下面运行winform出错,找不到类,界面也不显示 救命... login控件不能登录 救命 hashtable的特点是什么? 问个关于PreSendRequestContent的问题 两个sql查询问题 listbox的selectedindexchanged事件, 菜鸟求教数据库查询结果展示的问题。 Gridview问题 javascript怎么取到系统日期格式为 2007-09-01
难点: 如果目标站点有安全校验方式(比如图片验证码),比如ASPNET的ViewState等,你需要模拟这些值,是相当困难的.
Code如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//string strId = "guest";
//string strPassword = "123456";
//string fid = "15";
string query = "txtLastName=xie&txtFirstName=&reqOption=HRPPNameEntry&advanced=&action=name_search&pane=bottom"; ASCIIEncoding encoding = new ASCIIEncoding();
// string postData="userid="+strId;
// postData += ("&password="+strPassword);
string postData;
postData = query;
string myurl = "https://www.hrpassport.cs.bns/hr/hrpassport"; byte[] data = encoding.GetBytes(postData); // Prepare web request...1
// System.Net.HttpWebRequest myRequest =
// (System.Net.HttpWebRequest)WebRequest.Create("http://www.popyard.com/cgi-bin/artical.cgi");
System.Net.HttpWebRequest myRequest =
(System.Net.HttpWebRequest)WebRequest.Create(myurl); myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream(); // Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close(); // Get response HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
Console.WriteLine(content);
}
}
}返回信息:
"\r\n\r\n\r\n\r\n\r\n<HTML>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\" />\r\n<title>HR Passport (cs1il601)</title>\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"Common/StyleSheets/HRStyleSheet.css\" />\r\n\r\n<script>\r\n\r\nsetTimeout(\"top.location = '/hr/hrpassport'\", 9000);\r\n\r\n</script>\r\n\r\n</head>\r\n<BODY BACKGROUND=\"Common/Images/bg_globe.gif\" BGCOLOR=\"#FFFFFF\" TOPMARGIN=\"0\" LEFTMARGIN=\"0\" MARGINWIDTH=\"0\" MARGINHEIGHT=\"0\">\r\n \r\n<table border=\"0\" width=\"448\">\r\n <tr>\r\n <td width=\"20\"> \r\n <p> </td>\r\n <td width=\"350\" height=\"134\" align=\"center\" valign=\"bottom\"><font face=\"Verdana\" size=\"-1\">\r\n\tWe're sorry, but there was an error while you were browsing the HR PASSPORT web site.<br>\r\n\t\r\n</font></td>\r\n <td width=\"*\"></td>\r\n </tr>\r\n <tr>\r\n <td width=\"20\"></td>\r\n <td width=\"350\" height=\"134\" align=\"center\" valign=\"middle\">\r\n\t<table border=
\"1\">\r\n\t <tr><td><font face=\"Verdana\" size=\"-1\">\r\n\t\r\n\t\tjava.lang.Exception\r\n\t\r\n\t</font></td></tr>\r\n\t</table>\r\n <td width=\"*\"></td>\r\n </tr>\r\n\r\n <tr>\r\n <td width=\"20\"></td>\r\n <td width=\"350\" height=\"134\" align=\"center\" valign=\"top\"><font face=\"Verdana\" size=\"-1\">\r\n....returning you to the home page for HR PASSPORT..... </font></td>\r\n <td width=\"*\"></td>\r\n </tr>\r\n\r\n</table>\r\n</BODY>\r\n\t</HTML>"我觉得大概是被检测到有什么举动,是不是HttpWebRequest是能够被检测到?如何绕过这个?谢谢。
可能出问题的地方是:我是通过VPN连到目标站点的。难道这会是问题所在?我直接在IE中输入URL地址一点问题都没有啊?我还是怀疑那个站点能够知道我是通过HttpWebRequest进去的。
哪位大侠知道是什么原因?
谢谢。