Skip to main content
 首页 » 编程设计

c#之Httpwebrequest 问题 (503)

2024年02月23日28xing901022

我正在使用 HttpWebrequest 从 google 获取结果。我使用代理来获取数据。现在有一个奇怪的问题,对于一些查询它返回数据而对于一些它抛出异常远程服务器返回错误:(503) 服务器不可用。。有人可能认为代理不好,但是当你把它放在 Internet Explorer 中然后你打开谷歌时它就在那里。然后没有 503 错误。但是 httpwebrequest 在某些查询中给出它。即如果你打算得到

http://www.google.com/search?q=site:http://www.yahoo.com  

它会在你去的地方抛出异常

http://www.google.com/search?q=info:http://www.yahoo.com 

有效。

到目前为止我的代码是

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(file); 
                request.ProtocolVersion = HttpVersion.Version11; 
                request.Method = "GET"; 
               request.KeepAlive = false; 
                request.ContentType = "text/html"; 
                request.Timeout = 1000000000; 
                request.ReadWriteTimeout = 1000000000; 
                request.UseDefaultCredentials = true; 
                request.Credentials = CredentialCache.DefaultCredentials; 
    Uri newUri = new Uri("http://" + proxy[selectedProxy].ProxyAddress.Trim() + "/"); 
                    WebProxy myProxy = new WebProxy(); 
                    myProxy.Credentials = CredentialCache.DefaultCredentials; 
                    myProxy.Address = newUri; 
                    request.Proxy = myProxy; 
 WebResponse response = request.GetResponse(); 
                    // System.Threading.Thread.Sleep(Delay); 
                    StreamReader reader = null; 
                    string data = null; 
                    reader = new StreamReader(response.GetResponseStream()); 
                        data = reader.ReadToEnd(); 

请您参考如下方法:

您收到“抱歉,您是垃圾邮件”的提示,需要输入验证码才能继续或更改代理。出于某种原因,当您收到 503 错误时,默认情况下您无法拉取页面内容,但如果您在浏览器中执行相同的操作,内容将显示给您。