2

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.

slugster
  • 49,403
  • 14
  • 95
  • 145
Martijn
  • 141
  • 1
  • 8

1 Answers1

0

I was dealing with this issue inside Autohotkey. But basically using the same ComObjCreate("WinHttp.WinHttpRequest.5.1").

I found out that turning off IPV6 protocol in network adapter settings on windows resolved the issue.

So it really could be server side compatibility with IPV6. After 20 sec fallback to IPV4 and any next request running good. Just try turning of the IPV6.

I am going to dig more into how to force WinHttpRequest to use IPV4.

Some resources: WINHTTP_OPTION_IPV6_FAST_FALLBACK

https://learn.microsoft.com/en-us/windows/win32/winhttp/option-flags

https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-option

https://github.com/wine-mirror/wine/blob/master/include/winhttp.h

https://www.rfc-editor.org/rfc/rfc6555

Unfortunately this param is not compatible with WinHttpRequest.5.1 because it is param of WinHttp.

Workaround is to disable IPV6 in registry (requires reboot) Or much better and advised by Microsoft is to use prefixpolicy.

https://kb.firedaemon.com/support/solutions/articles/4000160803-prioritising-ipv4-over-ipv6-on-windows-10

Prefer IPV4 over IPV6

commandline: 
netsh interface ipv6 set prefixpolicy ::ffff:0:0/96 46 4

Prefer IPV6 over IPV4

commandline: 
netsh interface ipv6 set prefixpolicy ::ffff:0:0/96 35 4

Run As Administrator

Community
  • 1
  • 1