private static void GetResponseCallback(IAsyncResult asynchronousResult) { var exchange = (TransportExchange)asynchronousResult.AsyncState; try { // End the operation string responsestring; using (var response = (HttpWebResponse)exchange.Request.EndGetResponse(asynchronousResult)) { using (var streamResponse = response.GetResponseStream()) { using (var streamRead = new StreamReader(streamResponse)) responsestring = streamRead.ReadToEnd(); } if (response.Cookies != null) { foreach (Cookie cookie in response.Cookies) { exchange.AddCookie(cookie); } } } exchange.Messages = DictionaryMessage.ParseMessages(responsestring); exchange.Listener.OnMessages(exchange.Messages); exchange.Dispose(); } catch (Exception e) { exchange.Listener.OnException(e, ObjectConverter.ToListOfIMessage(exchange.Messages)); exchange.Dispose(); } }
private static void GetResponseCallback(IAsyncResult asynchronousResult) { var exchange = (TransportExchange)asynchronousResult.AsyncState; try { // End the operation string responsestring; using (var response = (HttpWebResponse)exchange.Request.EndGetResponse(asynchronousResult)) { using (var streamResponse = response.GetResponseStream()) { using var streamRead = new StreamReader(streamResponse); responsestring = streamRead.ReadToEnd(); } _logger?.LogDebug("Received message(s)."); if (response.Cookies != null) { foreach (Cookie cookie in response.Cookies) { exchange.AddCookie(cookie); } } response.Close(); } try { exchange.Messages = DictionaryMessage.ParseMessages(responsestring); } catch (Exception e) { _logger?.LogError($"Failed to parse the messages json: {e}"); } exchange.Listener.OnMessages(exchange.Messages); exchange.Dispose(); } catch (Exception e) { exchange.Listener.OnException(e, ObjectConverter.ToListOfIMessage(exchange.Messages)); exchange.Dispose(); } }
private static void GetResponseCallback(IAsyncResult asyncResult) { LongPollingRequest exchange = asyncResult.AsyncState as LongPollingRequest; try { string responseString; HttpStatusCode responseStatus; // End the operation HttpWebResponse response; Exception error = null; try { response = exchange._request.EndGetResponse(asyncResult) as HttpWebResponse; } catch (WebException wex) { if (wex.Status == WebExceptionStatus.RequestCanceled) { throw; } response = wex.Response as HttpWebResponse; if (null == response) { throw; } error = wex; } using (response) { responseStatus = response.StatusCode; using (StreamReader streamRead = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true)) { responseString = streamRead.ReadToEnd(); // DEBUG if (logger.IsDebugEnabled) { logger.DebugFormat(CultureInfo.InvariantCulture, "Received message(s): {0}", responseString); } } // Stores the transport Cookies to use for next requests if (response.Cookies != null && response.Cookies.Count > 0) { foreach (Cookie cookie in response.Cookies) { if (null != cookie && (!cookie.Discard || !cookie.Expired)) { exchange._transport.SetCookie(cookie); } } } } if (responseStatus != HttpStatusCode.OK) // TODO: error != null { if (null != exchange._listener) { exchange._listener.OnProtocolError(String.Format(CultureInfo.InvariantCulture, "Unexpected response {0}: {1}", (int)responseStatus, responseString), error, exchange._messages); } else { // DEBUG logger.Error(String.Format(CultureInfo.InvariantCulture, "Unexpected response {0}: {1}", (int)responseStatus, responseString), error); } } else if (responseString != null && (responseString = responseString.Trim()).Length > 0) { // TODO: responseString.Replace('"', '\'') IList <IMutableMessage> messages = DictionaryMessage.ParseMessages(responseString); // Backups the transport Timeout value (in milliseconds) to use for next requests foreach (IMutableMessage message in messages) { if (message != null && message.IsSuccessful && Channel.MetaConnect.Equals(message.Channel, StringComparison.OrdinalIgnoreCase)) { IDictionary <string, object> advice = message.Advice; object timeout; if (advice != null && advice.TryGetValue(Message.TimeoutField, out timeout) && timeout != null && timeout.ToString().Trim().Length > 0) { exchange._transport.Advice = advice; } } } if (null != exchange._listener) { // Fixes the received messages before processing string requestChannel = null; string requestUrl, baseUrl; foreach (IMutableMessage msg in messages) { if (String.IsNullOrEmpty(msg.Channel)) { if (null == requestChannel) { requestUrl = exchange._request.RequestUri.ToString(); // Absolute request URI baseUrl = exchange._transport.Url; baseUrl = (null == baseUrl) ? String.Empty : baseUrl.Trim(); if (requestUrl.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase)) { requestChannel = requestUrl.Substring(baseUrl.Length).Trim('\\', '/'); requestChannel = Channel.Meta.TrimEnd('\\', '/') + "/" + requestChannel; } else { requestChannel = String.Empty; } } if (!String.IsNullOrEmpty(requestChannel)) { msg.Channel = requestChannel; } } } exchange._listener.OnMessages(messages); } } else { if (null != exchange._listener) { exchange._listener.OnProtocolError(String.Format(CultureInfo.InvariantCulture, "Empty response (204) from URL: {0}", exchange._request.RequestUri), null, exchange._messages); } else { // DEBUG logger.ErrorFormat(CultureInfo.InvariantCulture, "Empty response (204) from URL: {0}", exchange._request.RequestUri); } } } catch (Exception ex) { if (null != exchange._listener && !exchange._aborted) { exchange._listener.OnException(ex, exchange._messages); } else { logger.Error(ex); // DEBUG } } finally { exchange._isSending = false; exchange._transport.RemoveRequest(exchange); } }