0

I'm writing the function of HTTP request for my UWP project. And I accidentally got the data last night ,which shows me that it takes longer to make a HTTP request with HTTP Client on UWP project than the same operation on Android

As for seraching the answer,I found out another similar question: UWP http client delay in getting response

So,My question is: What make this delay happen ? Just for the diff of application framework or others ?

Here is my HTTP request codes on UWP:

using ServerMonitor.Controls;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace ServerMonitor.Services.RequestServices
{
    public class HTTPRequest : BasicRequest, IRequest
    {
        private const short HTTPPORT = 80;
        private const short HTTPSPORT = 443;
        private string requestInfo = null;

        public static HTTPRequest Instance
        {
            get
            {
               return Nested.instance;
            }
        }

        private TransportProtocol httpOrhttps = TransportProtocol.http;

        public string Uri { set => uri = value; }
        public TransportProtocol ProtocolType { set => httpOrhttps = value; }
        public string RequestInfo { get => requestInfo; }

        private HTTPRequest() { }

        private async Task<bool> HttpRequest(string uri)
        {
            Stopwatch stopwatch = new Stopwatch();
            try
            {
                HttpClientHandler handler = new HttpClientHandler
                {
                    AllowAutoRedirect = true
                };
                using (HttpClient client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.Referrer = new Uri(uri);
                    client.Timeout = TimeSpan.FromSeconds(OverTime);
                    CancellationTokenSource cts = new CancellationTokenSource();
                    cts.CancelAfter(TimeSpan.FromSeconds(OverTime));
                    HttpResponseMessage message = null; 
                    stopwatch.Start();

                    Task queryTask = Task.Run(async() =>
                    {
                        message = await client.GetAsync(uri, cts.Token);
                        stopwatch.Stop();
                    });   

                    var ranTask = Task.WaitAny(queryTask, Task.Delay(OverTime));                    
                    if (0 != ranTask)
                    {
                        stopwatch.Stop();
                        if (!cts.IsCancellationRequested) {
                            cts.Cancel();
                        }                       
                        TimeCost = OverTime;
                        Status = "1002";
                        Exception e = new TaskCanceledException("Overtime");
                        requestInfo = e.ToString();
                        ErrorException = e;
                        return false;
                    }                                     
                    if (null == message)
                    {
                        Status = "500";
                        TimeCost = (int)(OverTime * 1.5);
                        requestInfo = "Failed Request : Response Message Is Null";
                    }
                    else {
                        TimeCost = (int)stopwatch.ElapsedMilliseconds;
                        Status = ((int)Enum.Parse(typeof(System.Net.HttpStatusCode), message.StatusCode.ToString())).ToString();
                        requestInfo = string.Format("{0} in {1}ms",message.StatusCode, stopwatch.ElapsedMilliseconds);
                        Debug.WriteLine(requestInfo);
                    }                    
                }
                await Task.CompletedTask;
                return true;
            }
            catch (TaskCanceledException e)
            {
                Debug.WriteLine("请求超时");
                DBHelper.InsertErrorLog(e);
                TimeCost = OverTime;
                Status = "1002";
                ErrorException = e;
                requestInfo = "Request OverTime !";
                return false;
            }
            catch (OperationCanceledException e)
            {
                Debug.WriteLine("请求失败" + e.Message);
                DBHelper.InsertErrorLog(e);
                TimeCost = (int)(OverTime*1.5);
                ErrorException = e;
                Status = "1002";
                requestInfo = e.Message;
                return false;
            }
            catch (Exception e)
            {
                Debug.WriteLine("请求失败" + e.Message);
                DBHelper.InsertErrorLog(e);
                TimeCost = (int)(OverTime * 1.5);
                ErrorException = e;
                Status = "1001";
                requestInfo = e.Message;
                return false;
            }
        }

        public async Task<bool> MakeRequest()
        {
            bool result = false;
            switch (httpOrhttps)
            {
                case TransportProtocol.http:
                    result = await HttpRequest(uri);
                    return result;
                default:
                    return result;
            }
        }

        private class Nested
        {
            static Nested()
            {

            }
            internal static readonly HTTPRequest instance = new HTTPRequest();
        }
    }


    public enum TransportProtocol
    {
        http,
        https
    };
}

And followed are some of my screen shots:

Request from uwp application: Request from uwp application

Request on Android application: Request on Android application

My UWP environment:

Windows 10.16299 SDK + Framework 4.6 + Visual Studio 2017

Chopping
  • 321
  • 1
  • 2
  • 11
  • What is UWP ???? – greenapps Jun 03 '18 at 07:53
  • Just the desktop application MS pushed mainly for `Windows 10 OS`,It is `Universal Windows Application` . Here is the link to it's panel:[Develop UWP Application](https://learn.microsoft.com/en-us/windows/uwp/design/basics/design-and-ui-intro) – Chopping Jun 03 '18 at 07:56
  • So UWP means ...... Platform. – greenapps Jun 03 '18 at 08:04
  • Absolutely...not,my friend. It is just the type of application (I think) with the aim to adapt to multiple platforms(like: Windows Desktop ,Xbox 360, Windows Mobile and so on).Em... Do I describe it clearly? I 'm actually a fresh man for it. – Chopping Jun 03 '18 at 08:10
  • There are many factors affecting the response time(e.g, CDN, net speed etc.). I used HttpClient to request yahoo.com, the response time is 300+ ms. – Xie Steven Jun 11 '18 at 09:47

0 Answers0