So I'm trying to use HttpWebRequest in my C# plugin for Revit(BIM software), to send a request to my API. But every time I try this, it takes way longer than the request would take in Chrome/Firefox/Postman.
If I send my request with Postman, it takes about 1 to 1,5 seconds. But if I send it within my application, it takes about 21 to 21,5 seconds. So it seems like there is some kind of timeout created by the HttpWebrequest, but I can't seem to figure out why this is the case.
My code:
static public string Get(string baseURI, Dictionary<string, string> requestParameters)
{
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.DefaultConnectionLimit = 15;
string requestURI = baseURI;
if (requestURI.Length != 0)
{
foreach (KeyValuePair<string, string> parameter in requestParameters)
{
if (requestURI[requestURI.Length - 1] != '?')
{
requestURI = requestURI + '&';
}
requestURI = requestURI + parameter.Key + "=" + parameter.Value;
}
}
HttpWebRequest request = WebRequest.Create(requestURI) as HttpWebRequest;
request.Method = "GET";
string results = string.Empty;
request.Proxy = null;
request.KeepAlive = false;
HttpWebResponse response;
using (response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
results = reader.ReadToEnd();
reader.Close();
response.Close();
}
return results;
}
I've tried the following:
Using RestSharp
Using HttpWebRequest
Sending two request (the same of two different requests), where the second request only takes 1,5 seconds, as it should.
I've tried request.Proxy = null; / ServicePointManager.UseNagleAlgorithm = false; / request.KeepAlive / ServicePointManager.DefaultConnectionLimit = 15;
I can't think of anything else, and the debugger doesn't give me any useful information on what's it doing in those 20 seconds.