title: How can I make a c# https web request that works in both proxy and no proxy situations
body: <p>This code has changed and hacked and tried and tried, I currently have my webrequest as shown, followed by a GetRequestStream() and GetResponse() section. This works from behind our proxy but from a laptop on wifi <em>not behind a proxy</em> the GetProxy bit returns the uri that was passed to it, this results in the error "The ServicePointManager does not support proxies of https scheme." So I tried as below to set the proxy to a blank new proxy when it matches the uri passed. This results in the process locking up for a good while and then returning "operation has timed out error". If I put Fiddler on the laptop it acts as a proxy so the code works as it does from our network and everything is fine, so fiddler changes the operation of the code.</p>
  
  <p>I think the key may be in the timeout problem it's during the GetRequestStream() that it throws the timeout.</p>
  
  <p>The laptop has no proxy settings in ie or chrome. Via a browser I can access both http and https websites from it without issue (including the one I'm accessing from this code).</p>
  
  <p>However from my c# code I cannot get the https request to work.</p>
  
  <p>If I change the requests to http it works ok. (both on our network behind the proxy and from the laptop)</p>
  
  <p>While the below show the current code I've got, I've tried swapping out with ever block of code I've found on here and various blogs / guides without finding a working solution.</p>
  
  <p>Just to be clear, I'm getting a working connection on the machines behind a proxy on both https and http. I can get it working from the non proxy laptop when using http. the errors occur on the non proxy laptop when trying to get a https connection. </p>
  
  <pre><code>        HttpWebRequest myWebRequest;
          myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
          myWebRequest.CookieContainer = new System.Net.CookieContainer();
  
          if (myWebRequest.Proxy.GetProxy(new Uri(url)).ToString() == url)
          {
              myWebRequest.Proxy = new WebProxy();
          }
          else
          {
              string proxyuri = myWebRequest.Proxy.GetProxy(new Uri(url)).ToString();
              myWebRequest.Proxy = new WebProxy(proxyuri, true);
              myWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
          }
          myWebRequest.UseDefaultCredentials = true;
  </code></pre>