Httpwebrequest问题(503) [英] Problem with Httpwebrequest (503)

查看:289
本文介绍了Httpwebrequest问题(503)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HttpWebrequest从google获取结果.我使用代理来获取数据.现在有一个奇怪的问题,对于某些查询,它返回数据,而对于某些查询,它会抛出异常The remote server returned an error: (503) Server Unavailable..有人可能认为代理是不好的,但是当您将其放在Internet Explorer中时,便打开了google.那里没有503错误.但是httpwebrequest在某些查询中给出了它.即,如果您打算获得

I am using HttpWebrequest to GET the result from google.I use proxies to get the data.now there is a strange problem that for some queries it return the data and for some it throws the exception The remote server returned an error: (503) Server Unavailable.. One might think that proxy is bad but when you put it in internet explorer then you open google it is there.no 503 error then.but httpwebrequest gives it on certain query.i.e if you intend to get

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

它将抛出异常,就像您要去的地方

it would throw exception where as if you go for

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

有效.

到目前为止,我的代码是

my code so far is

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

推荐答案

那很奇怪.也许是一些网址编码问题.尝试以下应妥善处理所有事情的方法:

That's weird. Maybe some url encoding issue. Try the following which should take care of properly handling everything:

using System;
using System.Net;
using System.Web;

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            var newUri = new Uri("http://proxy.foo.com/");
            var myProxy = new WebProxy();
            myProxy.Credentials = CredentialCache.DefaultCredentials;
            myProxy.Address = newUri;
            client.Proxy = myProxy;

            var query = HttpUtility.ParseQueryString(string.Empty);
            query["q"] = "info:http://www.yahoo.com";
            var url = new UriBuilder("http://www.google.com/search");
            url.Query = query.ToString();
            Console.WriteLine(client.DownloadString(url.ToString()));
        }
    }
}

这篇关于Httpwebrequest问题(503)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆