jeudi 3 janvier 2019

C# HttpWebRequest extremely slow ? Here is the easy quick fix.

I have just found on StackOverflow that there is a very easy solution to boost the time of execution of an Http web request : Setting the proxy property of the HttpWebRequest object to null, as in he example that follows :

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://192.168.1.254:8080/api/SuperRestAPI");
            req.Proxy = null;
            req.Timeout = 5000;
            req.Method = "POST";
            req.ContentType = "text/json";
            using (var streamWriter = new StreamWriter(req.GetRequestStream()))
            {
                string json = JsonConvert.SerializeObject(loginRequest);
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }


Another far more elegant solution is setting the defaultProxy enabled parameter to false in the App.Config, as explained in the following Microsoft page :
https://docs.microsoft.com/fr-fr/dotnet/framework/configure-apps/file-schema/network/defaultproxy-element-network-settings