首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

急请圣人指教:C#设置WebProxy后发送HttpWebRequest,HttpWebResponse为何超时

2011-12-26 
急请高人指教:C#设置WebProxy后发送HttpWebRequest,HttpWebResponse为何超时?急切请教大家一个问题!对您的

急请高人指教:C#设置WebProxy后发送HttpWebRequest,HttpWebResponse为何超时?
急切请教大家一个问题!对您的任何建议表示感谢!!

我有一段程序是向服务端发送http   request并接收response,一切正常,后来由于网络环境需要,要求设置http代理(通过IE设置http代理),为了支持http代理,我为代码设置了WebProxy属性(下面代码中被注释的那三句),可结果是:接收response总是超时(HttpWebResponse   response   =   (HttpWebResponse)request.GetResponse();),急请高人指教!

代码片段如下:

                        try
                        {
                                Encoding   encoding   =   Encoding.UTF8;
                                byte[]   bytes   =   encoding.GetBytes(requestBody);

                                HttpWebRequest   request   =   (HttpWebRequest)WebRequest.Create(global.Hosts.SmapUri);
                                request.Method   =   "POST ";
                                request.ContentType   =   "application/x-www-form-urlencoded ";
                                request.ContentLength   =   bytes.Length;

                                //WebProxy   proxyObject   =   new   WebProxy( "192.168.1.112 ",   8080);
                                //request.Proxy   =   proxyObject;
                                //request.Proxy   =   WebProxy.GetDefaultProxy();
                               
                                Stream   sw   =   request.GetRequestStream();
                                sw.Write(bytes,   0,   bytes.Length);
                                sw.Close();

                                HttpWebResponse   response   =   (HttpWebResponse)request.GetResponse();
                                StreamReader   readStream   =   new   StreamReader(response.GetResponseStream(),   encoding);
                                String   responseBody   =   readStream.ReadToEnd();
                                response.Close();
                                readStream.Close();


                        }
                        catch   (System.Exception   e)
                        {
                                log.Info( "Exception:   "   +   e.Message);
                        }


[解决办法]
if (proxy != null)
{
request.Proxy = proxy;
}

设好代理 应就行了,我一直这样用的. 注意看http代理是否可用. 
到网上搜一些可用的,来测试. 我有时,用QQ的代理功能.来测试代理是否可用.

热点排行