用HttpWebRequest访问这个页面时读到的内容与浏览器不一样:
http://www.qjy-chemonline.com/busi/sbusi_search.php?offset=1&busi_industry=&thename=漆&busi_all=&province=广东&searchtype=any浏览器可以读到很多页,很多条东西, 而用HttpWebRequest访问时没有查询结果:
共搜索到<font color="#FF0000">0</font>个查询结果,当前第<font color="#FF0000">1</font>页,共<font color="#FF0000">1</font>页如果设置HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)", 可以读到上面提到的内容, 如果不设置UserAgent, 则访问失败. 莫非他们是判断UserAgent来输出不同的内容? 我看了用HttpWebRequest读到的源文件里也没有什么特别的脚本.下面是主要代码:// Create the request instance.
string site = "http://www.qjy-chemonline.com/busi/sbusi_search.php?offset=1&busi_industry=&thename=漆&busi_all=&province=广东&searchtype=any";
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(site);wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";// Get the response instance
WebResponse wResp = wReq.GetResponse(); // Get the response stream.
Stream respStream = wResp.GetResponseStream(); // This example uses a StreamReader to read the entire response
// into a string and then writes the string to the console.
StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("GB2312"));
String respHTML = reader.ReadToEnd();
Console.WriteLine(respHTML); // Close the response and response stream.
wResp.Close();
http://www.qjy-chemonline.com/busi/sbusi_search.php?offset=1&busi_industry=&thename=漆&busi_all=&province=广东&searchtype=any浏览器可以读到很多页,很多条东西, 而用HttpWebRequest访问时没有查询结果:
共搜索到<font color="#FF0000">0</font>个查询结果,当前第<font color="#FF0000">1</font>页,共<font color="#FF0000">1</font>页如果设置HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)", 可以读到上面提到的内容, 如果不设置UserAgent, 则访问失败. 莫非他们是判断UserAgent来输出不同的内容? 我看了用HttpWebRequest读到的源文件里也没有什么特别的脚本.下面是主要代码:// Create the request instance.
string site = "http://www.qjy-chemonline.com/busi/sbusi_search.php?offset=1&busi_industry=&thename=漆&busi_all=&province=广东&searchtype=any";
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(site);wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";// Get the response instance
WebResponse wResp = wReq.GetResponse(); // Get the response stream.
Stream respStream = wResp.GetResponseStream(); // This example uses a StreamReader to read the entire response
// into a string and then writes the string to the console.
StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("GB2312"));
String respHTML = reader.ReadToEnd();
Console.WriteLine(respHTML); // Close the response and response stream.
wResp.Close();
解决方案 »
- 在M$.Chart For .Net3.5中,ScrollBar拖动速度很慢
- 求助,为啥 dataset 里面没有数据?
- 求<ASP.Net服务器控件开发技术与实例>一书光盘或源码
- 能不能将字符串值赋给一个enum变量
- 使用Infragistics 控件的Quick Design 功能时提示“无法获取执行权限”,如何解决?
- 关于在程序中调用带output参数的存储过程的问题?
- 请问一下,编程员一般的工作都是做什么啊?怎样去学习编程?怎样能应聘到编程员的工作?(我只是软件专业毕业没有实际编程经验)
- c#利用了FileShare.ReadWrite功能后还是不能操作被其他程序占用的文本文件有什么办法?
- GetCallingMethod() ?
- 本人WinForm菜,想学,前辈指教!
- 关于转换的问题!!!
- 怎样让.net程序在英文的windowXP操作系统上运行?
string url="http://www.qjy-chemonline.com/busi/sbusi_search.php";
string query="offset=1&busi_industry=&thename=漆&busi_all=&province=广东&searchtype=any";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
Encoding en=System.Text.Encoding.GetEncoding("gb2312");//用GB2312编码//将所有参数值UrlEncode
if (query != null)
{
int i=0, j;
while(i<query.Length)
{
j=query.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(query.Substring(i, query.Length-i),en));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(query.Substring(i, j-i),en));
UrlEncoded.Append(query.Substring(j,1));
i = j+1;
}
} Uri uri = new Uri(url+"?"+UrlEncoded.ToString());
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(uri);//下面自己去取了