【问题标题】:Calling api by proxy takes 15+seconds代理调用api需要15+秒
【发布时间】:2021-08-09 13:37:44
【问题描述】:

在C#应用程序中,调用API接口时,往往需要15+秒才能访问。 API部署在内网的另一个网段,需要代理访问。有人说是DNS问题,尝试设置主机,没有效果。 环境:Windows Server 2012 R2,IIS v8.5 代码脚本:

  private string PostHttp(string url, string authHeader, string requestBody)
        {
            var webRequest = System.Net.WebRequest.Create(url);

            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

            byte[] data = System.Text.Encoding.UTF8.GetBytes(requestBody);
            webRequest.Method = "POST";
            webRequest.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            webRequest.Headers.Add("Authorization", authHeader);
            webRequest.ContentLength = data.Length;
            webRequest.ContentType = "application/x-www-form-urlencoded";


            System.Net.WebProxy proxy = new System.Net.WebProxy("http://myHttpProxyAddress", false);
            proxy.Credentials = new System.Net.NetworkCredential("HttpProxyUser", "HttpProxyPassword");
        
            webRequest.Proxy = proxy;

            var writer = webRequest.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();

            using (WebResponse webResponse = webRequest.GetResponse())
            {
                System.IO.StreamReader reader = null;

                if (webResponse.Headers["Content-Encoding"] == "gzip")
                    reader = new System.IO.StreamReader(new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress), System.Text.Encoding.UTF8);
                else
                    reader = new System.IO.StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.UTF8);

                var result = reader.ReadToEnd();
                reader.Close();

                return result;
            }
        }

【问题讨论】:

标签: c# windows dns delay


【解决方案1】:

仅听到有人说其中一种可能性(例如 DNS 错误)很难指出,尝试使用一些 http 实用程序(例如 Curl)来检查和衡量。

您可以在本指南下方包装上面的请求并测量时间细节。

How do I measure request and response times at once using cURL?

【讨论】:

  • 感谢您的评论。这是一天的跟踪日志:` [Time] Elasted Time(ms) [14:20:14.5923] :15175 [14:21:18.2560] :147 [14:22:22.1694] :117 [14:47: 21.9716]:157 [14:47:52.3604]:106 [15:04:56.1902]:15155 [15:23:00.7822]:161 [15:24:12.0431]:125 [15:45:05.4117]:15149 [ 15:46:10.0582]:159 [15:46:54.2999]:105 [15:48:21.3480]:114 [16:09:09.8806]:172 [16:09:50.9243]:106 [16:22:59.3809 ] :15155 [16:23:50.5958] :107 [17:58:28.9433] :15216 [18:01:11.3708] :15113 [18:04:59.2404] :15153 [22:12:52.6497] :15225 [22 :13:00.3718]:104`
  • 您可以将其与时间详细信息一起导出,并与在服务器中设置代理的网络/基础设施团队共享。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2015-10-09
相关资源
最近更新 更多