Exemplo n.º 1
0
 internal async Task OnRequestComplete(JsonResponse response)
 {
     await RequestComplete(this, new RequestCompletedEventArgs(response)).ConfigureAwait(false);
 }
Exemplo n.º 2
0
        internal async Task <JsonResponse <TResponse> > GetJsonResponse <TResponse>(Uri baseUri, Uri relativeUri, CancellationToken cancellationToken, IProgress <ProgressReport> progress, AuthToken token = null)
        {
            progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.PreparingUrl_N_Headers, 0, 1));
            //验证对于一般的请求参数修改
            if (token != null)
            {
                var baseUrl = JsonClientHelper.GetBaseUriBuilder(baseUri, relativeUri, this.RequestData.UrlTemplateValues).Uri.ToString();
                token.FillToContext(baseUrl, this);
            }

            var uri = new Uri(JsonClientHelper.GetAbsoultUrl(baseUri, relativeUri, this.RequestData.QueryStringValues, this.RequestData.UrlTemplateValues));

#if DOTNET45
            var webrqst = WebRequest.CreateDefault(uri);
#else
            var webrqst = WebRequest.Create(uri);
#endif

            webrqst.Method = RequestMethod;
            //验证对于底层请求参数修改
            if (token != null)
            {
                token.FillToWebRequest(webrqst, this);
            }

            FillToHeader(webrqst);
            progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.PreparingUrl_N_Headers, 1, 1));

            await FillDataTo(webrqst, cancellationToken, progress).ConfigureAwait(false);

            if (typeof(WebRequest) == (typeof(TResponse)))
            {
                var o = new JsonResponse <WebRequest>(webrqst, webrqst, null, null);
                progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.Deserializing, 1, 1));
                return(o as JsonResponse <TResponse>);
            }

            using (var webrsps = await webrqst.GetResponseAsync().ConfigureAwait(false))
            {
                if (typeof(WebResponse) == (typeof(TResponse)))
                {
                    var o = new JsonResponse <WebResponse>(webrsps, webrqst, webrsps, null);
                    progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.Deserializing, 1, 1));
                    return(o as JsonResponse <TResponse>);
                }
#if SILVERLIGHT_4 || SILVERLIGHT_5 || WINDOWS_PHONE_7
                var h = new Dictionary <string, string[]>();
#endif

#if DOTNET45 || WINDOWS_PHONE_8
                var h = webrsps.Headers.AllKeys
                        .Select(k => new { k, v = webrsps.Headers[k].Split(',') })
                        .ToDictionary(x => x.k, x => x.v);
#endif

#if NETFX_CORE
                var h = webrsps.Headers.AllKeys
                        .Select(k => new { k, v = webrsps.Headers[k].Split(',') })
                        .ToDictionary(x => x.k, x => x.v);
#endif
                var ms           = new MemoryStream();
                var total        = webrsps.ContentLength;
                var current      = 0;
                var buffer       = new byte[4096];
                var sourceStream = webrsps.GetResponseStream();
                var eof          = false;
                while (!eof)
                {
                    progress.ReportWhenNotNull(new Func <ProgressReport> (() => ProgressReport.CreateNew(RequestResponseSteps.GettingResponse, current, total)));
                    cancellationToken.ThrowIfCancellationRequested();
                    var readCount = await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

                    current = current + readCount;

                    eof = (readCount == 0);
                    if (!eof)
                    {
                        ms.Write(buffer, 0, readCount);
                    }
                }

                if (typeof(Stream) == (typeof(TResponse)))
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    var value = ms;
                    var o     = new JsonResponse <Stream>(value, webrqst, webrsps, ms);
                    progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.Deserializing, 1, 1));
                    return(o as JsonResponse <TResponse>);
                }
                else
                {
                    try
                    {
                        progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.Deserializing, 0, 1));
                        ms.Seek(0, SeekOrigin.Begin);
                        var value = (TResponse)JsonSerializerCache <TResponse> .Serializer.ReadObject(ms);

                        ms.Seek(0, SeekOrigin.Begin);
                        var o = new JsonResponse <TResponse>(value, webrqst, webrsps, ms);
                        progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.Deserializing, 1, 1));
                        return(o);
                    }
                    catch (Exception ex)
                    {
                        ms.Seek(0, SeekOrigin.Begin);
                        return(new JsonResponse <TResponse>(
                                   default(TResponse),
                                   webrqst,
                                   webrsps,
                                   ms,
                                   new JsonContractException("Serializing exception", ex, ms, h)
                                   ));
                    }
                }
            }
        }