用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(); 

解决方案 »

  1.   

    判断UserAgent是很可能的比如google就是根据UserAgent来输出不同代码,实现跨浏览器
      

  2.   

    怎么设置UserAgent可以读取上面的网址内容呢?
      

  3.   

    原因很简单,因为你的URL里面有中文的参数,而.net的字符默认是utf8所以你取这个地址之前,先要把URL改成gb2312的urlencode就拿你这个作例子//把URL的页面地址和页面参数分开
    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);//下面自己去取了