0

In C # application, when calling API interface, it often takes 15+ seconds to access. API is deployed in another network segment of intranet and needs to be accessed by proxy. Some one said that it was a DNS problem, try to setting the host, which has no effect. Environment: Windows Server 2012 R2, IIS v8.5 Code Script:

  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;
            }
        }
Tinker
  • 49
  • 1

1 Answers1

0

it's very difficult to point out by just hearing someone said one of the possibility (e.g DNS error) , try to use some http utility (e.g Curl) to check and measure that.

you can wrap the request above with below this guide and measure the timing detail.

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

Turbot
  • 5,095
  • 1
  • 22
  • 30
  • Thanks for comment. Here is the trace log in a day: ` [Time] Elasped 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` – Tinker May 20 '21 at 10:38
  • you can export that with timing details , and share with your network/infra team that setup the proxy in the server. – Turbot May 21 '21 at 06:11