C# 如何从 WebRequest 中删除代理并保持 DefaultWebProxy 不变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1559863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:51:39  来源:igfitidea点击:

How to remove proxy from WebRequest and leave DefaultWebProxy untouched

c#.net

提问by Elephantik

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).

我使用 FtpWebRequest 来做一些 FTP 的事情,我需要直接连接(无代理)。但是 WebRequest.DefaultWebProxy 包含 IE 代理设置(我认为)。

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

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

我的代码是一个巨大应用程序中的一部分,我不想更改,WebRequest.DefaultWebProxy因为它是全局静态属性,它可能对应用程序的其他部分产生不利影响。

Any idea how to do it?

知道怎么做吗?

采纳答案by Alastair Pitts

try setting the proxy to an empty WebProxy, ie:

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

request.Proxy = new WebProxy();

This should create an empty proxy.

这应该创建一个空代理。

回答by dr. evil

Actually setting it to null will be enough as well to disable auto proxy detection, you might save some cycles :)

实际上将其设置为 null 也足以禁用自动代理检测,您可能会节省一些周期:)

request.Proxy = null;

http://msdn.microsoft.com/en-us/library/fze2ytx2.aspx

http://msdn.microsoft.com/en-us/library/fze2ytx2.aspx

回答by Tshepo Kwienana

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(yourRequestUrl);
        if (webRequest.Proxy != null)
        {
            webRequest.Proxy = null;
        }

        webRequest.KeepAlive = true;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/json";
        var json = JsonConvert.SerializeObject(yourObject);
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] postBytes = encoder.GetBytes(json);
        webRequest.ContentLength = postBytes.Length;
        webRequest.CookieContainer = new CookieContainer();
        String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Format("{0}:{1}", userName, password)));
        webRequest.Headers.Add("Authorization", "Basic " + encoded);
        Stream requestStream = webRequest.GetRequestStream();
        requestStream.Write(postBytes, 0, postBytes.Length);
        requestStream.Close();

        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        string result;
        using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
        {
                result = rdr.ReadToEnd();
}

回答by bside

Add this to config:

将此添加到配置:

<system.net>
  <defaultProxy enabled="false" useDefaultCredentials="false">
    <proxy />
    <bypasslist />
    <module />
  </defaultProxy>
</system.net>