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?

Elephantik
  • 1,998
  • 1
  • 17
  • 23

4 Answers4

34

try setting the proxy to an empty WebProxy, ie:

request.Proxy = new WebProxy();

This should create an empty proxy.

Alastair Pitts
  • 19,423
  • 9
  • 68
  • 97
  • No probs, this one stumped me a little while ago. – Alastair Pitts Oct 13 '09 at 12:53
  • 2
    It's worth noting that the [MSDN documentation](https://msdn.microsoft.com/en-us/library/czdt10d3(v=vs.110).aspx) 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. – David Jun 14 '17 at 12:22
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

dr. evil
  • 26,944
  • 33
  • 131
  • 201
  • 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 anyway – Elephantik Apr 06 '10 at 13:02
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();
}
Andrei
  • 42,814
  • 35
  • 154
  • 218
0

Add this to config:

<system.net>
  <defaultProxy enabled="false" useDefaultCredentials="false">
    <proxy />
    <bypasslist />
    <module />
  </defaultProxy>
</system.net>
Andrii Viazovskyi
  • 777
  • 2
  • 9
  • 15