C# WebClient Download String https

2019-02-18 01:00发布

问题:

In the webbrowser I can normally load the following url: https://security.ultimatxxxx.com:443/Serverstatus.ashx

When I do it with:

Webclient.DownloadStringAsync("https://security.ultimatxxxx.com:443/Serverstatus.ashx");

For testing purposes you can call: ultimate-eur instead of ultimatxxxx.

It does not work, but when I load a normal URL without https I have no problems. Which argument do I have to add so that the Webclient can also download the string?

I want to use that feature in Monotouch, but language is C#.

回答1:

If you just wish to accept all certificates, you'll need to set certificate check, before you make your request:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

This will accept all certificates.

Although - if you need to actually check the certificate do something like:

ServicePointManager.ServerCertificateValidationCallback =
    (sender, certificate, chain, sslPolicyErrors) => 
    {
        //to check certificate
    };


回答2:

One possible reason might be the fact that some website provide different result to different browser.

You can try to provide the browser information to pretend that the call is from a browser.

var client = new WebClient();
client.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15";
string download = client.DownloadString("https://security.ultimatxxxx.com:443/Serverstatus.ashx");

Or

you have not bind the completedEvent of the Aysnc call.