private static string DownloadFile(string fetchUrl, int requestTimeout, int requestRetryCount) { while (true) { try { using (HttpWebResponse response = (HttpWebResponse)CreateWebRequest(fetchUrl, requestTimeout).GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { return reader.ReadToEnd(); } } } catch { if (requestRetryCount <= 0) { throw; } requestRetryCount--; } } }
private static WebRequest CreateWebRequest(string fetchUrl, int requestTimeout) { HttpWebRequest.DefaultCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default); WebRequest request = WebRequest.Create(new Uri(fetchUrl)); request.Timeout = requestTimeout; request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; request.Credentials = CredentialCache.DefaultCredentials; request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); request.Proxy = new WebProxy("sinproxy.southpacific.corp.microsoft.com"); return request; }
Note:
If the error "unable to connect to the remote server" is reported and can be accessed through a browser, you must have missed this sentence, as shown below.
Request. Proxy = new WebProxy ("sinproxy.southpacific.corp.microsoft.com ");
To access the URL through a browser or your own code, make sure that the environment is the same.