因为项目需要利用google的搜索结果,可是写出来的程序在运行时老报“尝试自动重定向的次数太多”,这该如何处理呢?(没有使用google ajax search api,利用WebClient写的),因为使用的网上的例子,代码较多,就不一一贴出了,只贴出报错部分的代码:输入变量url="http://www.google.com.hk/search?hl=zh-CN&source=hp&biw=1280&bih=830&q=北京联通"
public static string GetSearchHtml(string url)
{
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;   
Byte[] pageData = MyWebClient.DownloadData(url);                
return Encoding.UTF8.GetString(pageData);                       
}                 运行到 Byte[] pageData = MyWebClient.DownloadData(url);就报“尝试自动重定向的次数太多”这个错误了,请问有解决办法吗?参考了好几个网上的例子,都是报这样的错,是谷歌做了什么限制么?(暂时不考虑百度,广告太多,影响准确度.......汗啊。)另外,能否提供一、两个利用google ajax search api进行检索后获取结果的示例?我不太明白怎么样才能让C#去调用api然后获取搜索结果。项目工期实在太紧,来不及去仔细学习了,有示例代码的那是最好不过了!!!!

解决方案 »

  1.   

    使用HttpWebRequest,设置CookieContainer即可
      

  2.   

    String url="http://www.google.com.hk/search?hl=zh-CN&source=hp&biw=1280&bih=830&q=北京联通";
    System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
    r.AllowAutoRedirect = true;
    System.Net.CookieContainer c = new System.Net.CookieContainer();
    r.CookieContainer = c;
    System.Net.HttpWebResponse res = r.GetResponse() as System.Net.HttpWebResponse;
    System.IO.StreamReader s = new System.IO.StreamReader(res.GetResponseStream(),System.Text.Encoding.GetEncoding("GB2312"));
    Response.Write(s.ReadToEnd());
    res.Close();
      

  3.   

    这样肯定不好了,你可以用ajax想google提交数据。然后返回到一个div里。。
    你可以参考一下这个页面的 http://www.315che.com/searchall.htm
    方法都写好了,你自己要传一个参数就可以了
      

  4.   

     http://www.315che.com/searchall.htm
      

  5.   

    ajax不能跨域,需要在服务器端处理的