C# 使用代理时如何获取google结果

C# 使用代理时如何获取google结果,c#,visual-studio-2010,web-scraping,C#,Visual Studio 2010,Web Scraping,我制作了一个C#应用程序,它可以获取提交的URL的谷歌图像和文本结果 问题是,它始终适用于使用HTTPWEBREQUEST的URL,但当我使用代理时,它不起作用。我会遇到诸如302文档移动或502服务器不可用之类的错误 同样,如果我使用webbrowser控件,它将与代理一起工作 我确实看到了许多与我的问题有关的问题和答案,但没有一个与之相近 有什么建议吗?为我自己的问题找到了解决方案: 我们需要使用https代理与谷歌为了它的工作。因为这为我工作,使适当的主机,因为我也使用多个网址根据不同的位

我制作了一个C#应用程序,它可以获取提交的URL的谷歌图像和文本结果

问题是,它始终适用于使用
HTTPWEBREQUEST
的URL,但当我使用代理时,它不起作用。我会遇到诸如302文档移动
502服务器不可用之类的错误

同样,如果我使用webbrowser控件,它将与代理一起工作

我确实看到了许多与我的问题有关的问题和答案,但没有一个与之相近


有什么建议吗?

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

我们需要使用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