简体   繁体   中英

How can I make a c# https web request that works in both proxy and no proxy situations

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 not behind a proxy 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.

I think the key may be in the timeout problem it's during the GetRequestStream() that it throws the timeout.

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

However from my c# code I cannot get the https request to work.

If I change the requests to http it works ok. (both on our network behind the proxy and from the laptop)

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.

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.

        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;

Ok so the problem I was having was to do with the settings in apache on the server. the solution is in here.

HttpWebRequest.GetRequestStream() breaks by timeout on SSL connection under Windows 7/Vista

Basically you need to ensure that the servername in your apache directives matches the https address of your c# down to the www. part.

Now what I'd like to know is if there is a way in the c# code to avoid or catch this before the call to GetRequestStream() that freezes up for 30 secs followed by the timeout exception.

How could I detect this better?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM