如何从WebRequest的去除代理并留下DefaultWebProxy不变WebRequest、DefaultWebProxy

2023-09-04 00:54:00 作者:手捧一夜月光

我用的FtpWebRequest做一些FTP的东西,我需要直接连接(无代理)。然而WebRequest.DefaultWebProxy包含IE代理服务器设置(我认为)。

I use FtpWebRequest to do some FTP stuff and I need to connect directly (no proxy). However WebRequest.DefaultWebProxy contains IE proxy settings (I reckon).

WebRequest request = WebRequest.Create("ftp://someftpserver/");
// request.Proxy is null here so setting it to null does not have any effect
WebResponse response = request.GetResponse();
// connects using WebRequest.DefaultWebProxy

我的code是一块在一个巨大的应用程序,我不想改变 WebRequest.DefaultWebProxy ,因为它是全球性的静态属性,它可以产生不利上的应用程序的其他部分的影响。

My code is a piece in a huge application and I don't want to change WebRequest.DefaultWebProxy because it is global static property and it can have adverse impact on the other parts of the application.

任何想法怎么办呢?

推荐答案

尝试代理设置为空WebProxy,即:

try setting the proxy to an empty WebProxy, ie:

request.Proxy = new WebProxy();

这应该创建一个空的代理服务器。

This should create an empty proxy.