예제 #1
0
        async Task <string> SendRequestAndGetJsonResponseHttpClient <T>(Uri requestUri, RequestState <T> pubnubRequestState, HttpWebRequest request)
        {
            string jsonString            = "";
            HttpResponseMessage response = null;

            try
            {
                LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime: {0}, Inside SendRequestAndGetJsonResponseHttpClient", DateTime.Now.ToString(CultureInfo.InvariantCulture)), pubnubConfig.LogVerbosity);
                System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
                stopWatch.Start();
                if (pubnubRequestState.ResponseType == PNOperationType.PNSubscribeOperation)
                {
                    response = await httpClientSubscribe.GetAsync(requestUri).ConfigureAwait(false);
                }
                else if (pubnubRequestState.ResponseType == PNOperationType.PNDeleteMessageOperation)
                {
                    response = await httpClientNonsubscribe.DeleteAsync(requestUri).ConfigureAwait(false);
                }
                else
                {
                    response = await httpClientNonsubscribe.GetAsync(requestUri).ConfigureAwait(false);
                }
                if (response.IsSuccessStatusCode || response.Content != null)
                {
                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    stopWatch.Stop();
                    if (pubnubTelemetryMgr != null)
                    {
                        pubnubTelemetryMgr.StoreLatency(stopWatch.ElapsedMilliseconds, pubnubRequestState.ResponseType);
                    }
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        jsonString = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                        pubnubRequestState.GotJsonResponse = true;
                    }
                    System.Diagnostics.Debug.WriteLine(string.Format("DateTime {0}, Got HttpResponseMessage for {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), requestUri));
                }
                else
                {
                    stopWatch.Stop();
                    System.Diagnostics.Debug.WriteLine(string.Format("DateTime {0}, No HttpResponseMessage for {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), requestUri));
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                if (response != null && response.Content != null)
                {
                    response.Content.Dispose();
                    pubnubRequestState.Response = null;
                    pubnubRequestState.Request  = null;
                }
            }
            return(jsonString);
        }
예제 #2
0
        async Task <string> SendRequestAndGetJsonResponseHttpClient <T>(Uri requestUri, RequestState <T> pubnubRequestState, HttpWebRequest request)
        {
            string jsonString = "";
            HttpResponseMessage     response = null;
            CancellationTokenSource cts      = new CancellationTokenSource();

            try
            {
                LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime: {0}, Inside SendRequestAndGetJsonResponseHttpClient", DateTime.Now.ToString(CultureInfo.InvariantCulture)), pubnubConfig.LogVerbosity);
                cts.CancelAfter(GetTimeoutInSecondsForResponseType(pubnubRequestState.ResponseType) * 1000);
                System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
                stopWatch.Start();
                if (pubnubRequestState.ResponseType == PNOperationType.PNSubscribeOperation)
                {
                    response = await httpClientSubscribe.GetAsync(requestUri, cts.Token).ConfigureAwait(false);
                }
                else if (pubnubRequestState.ResponseType == PNOperationType.PNDeleteMessageOperation)
                {
                    response = await httpClientNonsubscribe.DeleteAsync(requestUri, cts.Token).ConfigureAwait(false);
                }
                else
                {
                    response = await httpClientNonsubscribe.GetAsync(requestUri, cts.Token).ConfigureAwait(false);
                }
                if (response.IsSuccessStatusCode || response.Content != null)
                {
                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    stopWatch.Stop();
                    if (pubnubTelemetryMgr != null)
                    {
                        await pubnubTelemetryMgr.StoreLatency(stopWatch.ElapsedMilliseconds, pubnubRequestState.ResponseType);
                    }
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        jsonString = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                        pubnubRequestState.GotJsonResponse = true;
                    }
                    System.Diagnostics.Debug.WriteLine(string.Format("DateTime {0}, Got HttpResponseMessage for {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), requestUri));
                }
                else
                {
                    stopWatch.Stop();
                    System.Diagnostics.Debug.WriteLine(string.Format("DateTime {0}, No HttpResponseMessage for {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), requestUri));
                }
            }
            catch (HttpRequestException httpReqEx)
            {
                if (httpReqEx.InnerException is WebException)
                {
                    WebException currentWebException = httpReqEx.InnerException as WebException;
                    if (currentWebException != null)
                    {
                        if (currentWebException.Response != null)
                        {
                            pubnubRequestState.Response = currentWebException.Response as HttpWebResponse;
                            using (StreamReader streamReader = new StreamReader(currentWebException.Response.GetResponseStream()))
                            {
                                //Need to return this response
                                jsonString = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                                System.Diagnostics.Debug.WriteLine(jsonString);
                                System.Diagnostics.Debug.WriteLine("");
                                System.Diagnostics.Debug.WriteLine(string.Format("DateTime {0}, Retrieved JSON from HttpClient WebException response", DateTime.Now.ToString(CultureInfo.InvariantCulture)));
                                return(jsonString);
                            }
                        }
                    }

                    LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime: {0}, SendRequestAndGetJsonResponseHttpClient InnerException WebException status {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), ((WebException)httpReqEx.InnerException).Status.ToString()), pubnubConfig.LogVerbosity);
                    throw httpReqEx.InnerException;
                }

                LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime: {0}, SendRequestAndGetJsonResponseHttpClient HttpRequestException {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), httpReqEx.Message), pubnubConfig.LogVerbosity);
                throw;
            }
            catch (Exception ex)
            {
                LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime: {0}, SendRequestAndGetJsonResponseHttpClient Exception {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), ex.Message), pubnubConfig.LogVerbosity);
                throw;
            }
            finally
            {
                if (response != null && response.Content != null)
                {
                    response.Content.Dispose();
                    pubnubRequestState.Response = null;
                    pubnubRequestState.Request  = null;
                }
            }
            return(jsonString);
        }