如何在.net中的HttpWebRequest自动检测/使用IE代理服务器设置 [英] How to AutoDetect/Use IE proxy settings in .net HttpWebRequest

查看:1077
本文介绍了如何在.net中的HttpWebRequest自动检测/使用IE代理服务器设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能检测/重复使用这些设置吗?



如何?



例外我是得到的
这是在连接到 http://www.google.com


异常

  System.Net.WebException:无法连接到远程服务器---> 
System.Net.Sockets.SocketException:A连接尝试失败,因为
连接的方没有正确一段时间后,因为连接主机未能响应66.102响应或已建立
连接失败.1.99:80

在System.Net.Sockets.Socket.DoConnect(端点endPointSnapshot,
的SocketAddress的SocketAddress)
在System.Net.Sockets.Socket.InternalConnect(端点remoteEP )
在System.Net.ServicePoint.ConnectSocketInternal(布尔connectFailure,
插槽S4,S6插座,插座及放大器;插座,ip地址和放大器;地址,
ConnectSocketState状态,IAsyncResult的asyncResult,超时的Int32,
异常和放大器;除外)
---内部异常堆栈跟踪的结尾---
在System.Net.HttpWebRequest.GetResponse()
在mvcTest.MvcApplication.Application_Start()在
C:\\home\\test\\Application1\\Application1\\Program.cs:第33行


解决方案

HttpWebRequest的默认情况下将实际使用IE代理设置。



如果你的的要使用它们,你必须明确覆盖.Proxy proprty要么空(无代理)或您选择的代理设置。

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(http://news.bbc.co.uk); 
//request.Proxy = NULL; //取消注释这绕过默认(IE)代理设置
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();

Console.WriteLine(完成 - 按回车键);
到Console.ReadLine();


Is it possible to detect/reuse those settings ?

How ?

The exception i'm getting is This is the exception while connecting to http://www.google.com

System.Net.WebException: Unable to connect to the remote server --->
  System.Net.Sockets.SocketException: A connection attempt failed because the
  connected party did not properly respond after a period of time, or established
  connection failed because connected host has failed to respond 66.102.1.99:80

  at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, 
     SocketAddress socketAddress)
  at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
  at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
     Socket s4, Socket s6, Socket& socket, IPAddress& address,
     ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,
     Exception& exception)
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.GetResponse()
  at mvcTest.MvcApplication.Application_Start() in
     C:\\home\\test\\Application1\\Application1\\Program.cs:line 33"

解决方案

HttpWebRequest will actually use the IE proxy settings by default.

If you don't want to use them, you have to specifically override the .Proxy proprty to either null (no proxy), or the proxy settings of you choice.

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://news.bbc.co.uk");
 //request.Proxy = null; // uncomment this to bypass the default (IE) proxy settings
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();

 Console.WriteLine("Done - press return");
 Console.ReadLine();

这篇关于如何在.net中的HttpWebRequest自动检测/使用IE代理服务器设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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