2012-08-22 16 views
1

我已经提出了C#应用程序来获取谷歌图像和文本结果,提交的网址。如何在使用代理时得到谷歌结果

问题是它总是适用于使用HTTPWEBREQUEST的网址,但是当我使用代理时它不起作用。我得到像302文档移动or 502服务器不可用的错误。

同样,如果我使用webbrowser控件,它可以与代理一起使用。

我确实看到了很多问题,并回答有关我的问题,但没有匹配关闭..

有什么建议?

回答

0

找到解决方案我自己的问题:

我们需要使用https与谷歌的代理在它为了工作。至于这个工作对我来说,让正确的主机,我还使用了根据不同位置的多个网址。

这是我的代码:

public string getHtmltt(string url) 
    { 

     string responseData = ""; 
     try 
     { 
      string host = string.Empty; 

      if (url.Contains("/search?")) 
      { 
       host = url.Remove(url.IndexOf("/search?")); 

       if(host.Contains("//")) 
       { 
        host = host.Remove(0, host.IndexOf("//")).Replace("//","").Trim(); 
       } 
      } 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
      request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; 
      request.AllowAutoRedirect = true; 
      request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"; 
      request.Timeout = 60000; 
      request.Method = "GET"; 
      request.KeepAlive = false; ; 


      // request.Host = "www.google.com.af"; 
      request.Host = host; 
      request.Headers.Add("Accept-Language", "en-US"); 

      //request.Proxy = null; 
      // WebProxy prx = new WebProxy("199.231.211.107:3128"); 

      WebProxy prx = new WebProxy(proxies[0].ToString().Trim()); 

      request.Proxy = prx; 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      if (response.StatusCode == HttpStatusCode.OK) 
      { 
       Stream responseStream = response.GetResponseStream(); 
       StreamReader myStreamReader = new StreamReader(responseStream); 
       responseData = myStreamReader.ReadToEnd(); 
      } 

      foreach (Cookie cook in response.Cookies) 
      { 
       inCookieContainer.Add(cook); 
      } 
      response.Close(); 



     } 
     catch (System.Exception e) 
     { 
      responseData = "An error occurred: " + e.Message; 

     } 

     return responseData; 

    } 

到目前为止其工作GR8。