急请高人指教: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();