How to remove proxy from WebRequest and leave DefaultWebProxy untouched
Asked Answered
U

4

16

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

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.

Any idea how to do it?

Ulda answered 13/10, 2009 at 12:19 Comment(0)
P
34

try setting the proxy to an empty WebProxy, ie:

request.Proxy = new WebProxy();

This should create an empty proxy.

Pettifer answered 13/10, 2009 at 12:26 Comment(2)
No probs, this one stumped me a little while ago.Pettifer
It's worth noting that the MSDN documentation says to use GlobalProxySelection.GetEmptyWebProxy() to obtain an empty proxy. But if you try this Visual Studio will inform you that the GlobalProxySelection class is obsolete and you should use WebRequest.DefaultWebProxy instead ... which is exactly what the OP does not want.Immersionism
D
9

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

request.Proxy = null;

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

Djokjakarta answered 27/3, 2010 at 9:14 Comment(1)
Actually, setting to null didn't help if I remember correctly (as said in the snippet comment). Disabling auto proxy detection would affect the rest of the application which I can't do either. Thanks anywayUlda
S
0
        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();
}
Schneider answered 30/3, 2016 at 14:34 Comment(0)
I
0

Add this to config:

<system.net>
  <defaultProxy enabled="false" useDefaultCredentials="false">
    <proxy />
    <bypasslist />
    <module />
  </defaultProxy>
</system.net>
Intelligibility answered 20/5, 2019 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.