【问题标题】:Slow first HTTP request with HttpWebRequest使用 HttpWebRequest 减慢第一个 HTTP 请求
【发布时间】:2021-04-02 17:58:48
【问题描述】:

所以我尝试在我的 Revit(BIM 软件)C# 插件中使用 HttpWebRequest,向我的 API 发送请求。但是每次我尝试这个时,它所花费的时间都比 Chrome/Firefox/Postman 中的请求要长。

如果我通过 Postman 发送请求,大约需要 1 到 1.5 秒。但如果我在我的应用程序中发送它,大约需要 21 到 21.5 秒。所以看起来HttpWebrequest创建了某种超时,但我似乎无法弄清楚为什么会这样。

我的代码:

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;
    }

我尝试了以下方法:

  • 使用 RestSharp

  • 使用 HttpWebRequest

  • 发送两个请求(两个不同的请求相同),其中第二个请求只需要 1.5 秒。

  • 我试过 request.Proxy = null; /ServicePointManager.UseNagleAlgorithm = false; /request.KeepAlive/ServicePointManager.DefaultConnectionLimit = 15;

我想不出其他任何事情,而且调试器没有给我任何有用的信息,说明它在这 20 秒内做了什么。

【问题讨论】:

标签: c# .net revit-api revit


【解决方案1】:

我在 Autohotkey 中处理了这个问题。但基本上使用相同的 ComObjCreate("WinHttp.WinHttpRequest.5.1")。

我发现在 Windows 的网络适配器设置中关闭 IPV6 协议解决了这个问题。

所以它确实可能是服务器端与 IPV6 的兼容性。 20 秒后回退到 IPV4 并且任何下一个请求运行良好。 只需尝试开启 IPV6。

我将深入研究如何强制 WinHttpRequest 使用 IPV4。

一些资源: WINHTTP_OPTION_IPV6_FAST_FALLBACK

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

https://docs.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

很遗憾,这个参数与 WinHttpRequest.5.1 不兼容,因为它是 WinHttp 的参数。

解决方法是在注册表中禁用 IPV6(需要重新启动) 或者微软建议更好的方法是使用前缀策略。

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

IPV4 优于 IPV6

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

IPV6 优于 IPV4

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

以管理员身份运行

【讨论】:

  • 另一种选择是在您的 API 网络服务器中启用 ipv6。
猜你喜欢
  • 2015-06-16
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
相关资源
最近更新 更多