/// <summary> /// Hidden constructor. /// </summary> internal RecordedDataSourceSelectorEventArgs(RecordedDataSource dataSource, IList<RecordedDataSourceResponse> responses, int selectedResponseIndex, int requestNumber, byte[] requestDataAsBinary, string requestDataAsString, string requestMethod, string requestContentType, object requestTag, HttpDataSourceResponseType responseType) { if (dataSource == null) throw new ArgumentNullException("dataSource"); if (responses == null) throw new ArgumentNullException("responses"); if (requestDataAsBinary == null) throw new ArgumentNullException("requestDataAsBinary"); if (requestDataAsString == null) throw new ArgumentNullException("requestDataAsString"); if (string.IsNullOrEmpty(requestMethod)) throw new ArgumentNullException("requestMethod"); DataSource = dataSource; Responses = responses; SelectedResponseIndex = selectedResponseIndex; RequestNumber = requestNumber; RequestDataAsBinary = requestDataAsBinary; RequestDataAsString = requestDataAsString; RequestMethod = requestMethod; RequestContentType = requestContentType; RequestTag = requestTag; ResponseType = responseType; SentAt = DateTime.Now; }
/// <summary> /// Sends request to the data source. /// </summary> public void SendRequest(string relativeUrlPath, string data, string method, HttpDataSourceResponseType responseType) { if (string.IsNullOrEmpty(data)) { InternalSendRequest(relativeUrlPath, NoContent, null, 0, method, false, responseType); } else { var serializedData = GetSerializedData(data); InternalSendRequest(relativeUrlPath, data, serializedData, serializedData != null ? serializedData.Length : 0, method, false, responseType); } }
public AsyncDataRequest(HttpWebRequest request, string dataDescription, byte[] data, int dataLength, HttpDataSourceResponseType responseType) { if (request == null) { throw new ArgumentNullException("request"); } Request = request; Data = data; DataLength = dataLength; ResponseType = responseType; #if DEBUG DataDescription = dataDescription; #endif }
/// <summary> /// Hidden constructor. /// </summary> internal RecordedDataSourceSelectorEventArgs(RecordedDataSource dataSource, IList <RecordedDataSourceResponse> responses, int selectedResponseIndex, int requestNumber, byte[] requestDataAsBinary, string requestDataAsString, string requestMethod, string requestContentType, object requestTag, HttpDataSourceResponseType responseType) { if (dataSource == null) { throw new ArgumentNullException("dataSource"); } if (responses == null) { throw new ArgumentNullException("responses"); } if (requestDataAsBinary == null) { throw new ArgumentNullException("requestDataAsBinary"); } if (requestDataAsString == null) { throw new ArgumentNullException("requestDataAsString"); } if (string.IsNullOrEmpty(requestMethod)) { throw new ArgumentNullException("requestMethod"); } DataSource = dataSource; Responses = responses; SelectedResponseIndex = selectedResponseIndex; RequestNumber = requestNumber; RequestDataAsBinary = requestDataAsBinary; RequestDataAsString = requestDataAsString; RequestMethod = requestMethod; RequestContentType = requestContentType; RequestTag = requestTag; ResponseType = responseType; SentAt = DateTime.Now; }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string method, HttpDataSourceResponseType responseType) { InternalSendRequestAsync(new byte[0], 0, BinaryContentDescription, method, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, byte[] data, string method, HttpDataSourceResponseType responseType) { InternalSendRequestAsync(data ?? new byte[0], data != null ? data.Length : 0, BinaryContentDescription, method, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, byte[] data, int dataLength, string method, HttpDataSourceResponseType responseType) { InternalSendRequest(relativeUrlPath, data != null && data.Length > 0 && dataLength > 0 ? BinaryContent : NoContent, data, data != null ? dataLength : 0, method, true, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, string data, string method, HttpDataSourceResponseType responseType) { var asBinary = data != null ? Encoding.UTF8.GetBytes(data) : new byte[0]; InternalSendRequestAsync(asBinary, data != null ? data.Length : 0, data, method, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, byte[] data, int dataLength, string method, HttpDataSourceResponseType responseType) { InternalSendRequest(relativeUrlPath, data != null && data.Length > 0 && dataLength > 0 ? BinaryContent : NoContent, data, data != null ? dataLength : 0, method, true, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string method, HttpDataSourceResponseType responseType) { InternalSendRequest(null, NoContent, null, 0, method, true, responseType); }
private void ReadResponseData(HttpWebResponse response, HttpDataSourceResponseType responseType, out string stringData, out byte[] binaryData, out Stream streamData) { stringData = null; binaryData = null; streamData = null; if (responseType == HttpDataSourceResponseType.AsString) { using (Stream responseStream = response.GetResponseStream()) { stringData = ReadResponseStringData(responseStream, GetContentLength(response), GetResponseEncoding(response)); } return; } if (responseType == HttpDataSourceResponseType.AsBinary) { using (Stream responseStream = response.GetResponseStream()) { binaryData = ReadResponseBinaryData(responseStream, (int)GetContentLength(response)); } return; } if (responseType == HttpDataSourceResponseType.AsRawStream) { streamData = response.GetResponseStream(); return; } throw new ArgumentOutOfRangeException("responseType", "Not supported response type"); }
private void InternalSendRequestAsync(byte[] data, int dataLength, string dataDescription, string method, HttpDataSourceResponseType responseType) { VerifyRequest(data, dataLength, dataDescription, method); _isActive = true; ThreadPool.QueueUserWorkItem(ResponseResponseAsync, new RecordedDataSourceSelectorEventArgs(this, _responses, SelectedIndex, _requestNumber++, data, dataDescription, method, ContentType, Tag, responseType)); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, string data, string method, HttpDataSourceResponseType responseType) { var asBinary = data != null?Encoding.UTF8.GetBytes(data) : new byte[0]; InternalSendRequestAsync(asBinary, data != null ? data.Length : 0, data, method, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, byte[] data, int dataLength, string method, HttpDataSourceResponseType responseType) { var copy = data != null ? new byte[dataLength] : new byte[0]; if (data != null) { Array.Copy(data, copy, copy.Length); } InternalSendRequestAsync(copy, copy.Length, BinaryContentDescription, method, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, byte[] data, string method, HttpDataSourceResponseType responseType) { InternalSendRequestAsync(data ?? new byte[0], data != null ? data.Length : 0, BinaryContentDescription, method, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, byte[] data, int dataLength, string method, HttpDataSourceResponseType responseType) { var copy = data != null ? new byte[dataLength] : new byte[0]; if (data != null) Array.Copy(data, copy, copy.Length); InternalSendRequestAsync(copy, copy.Length, BinaryContentDescription, method, responseType); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string relativeUrlPath, string data, string method, HttpDataSourceResponseType responseType) { if (string.IsNullOrEmpty(data)) InternalSendRequest(relativeUrlPath, NoContent, null, 0, method, true, responseType); else { var serializedData = GetSerializedData(data); InternalSendRequest(relativeUrlPath, data, serializedData, serializedData != null ? serializedData.Length : 0, method, true, responseType); } }
private void InternalSendRequestAsync(byte[] data, int dataLength, string dataDescription, string method, HttpDataSourceResponseType responseType) { VerifyRequest(data, dataLength, dataDescription, method); _isActive = true; ThreadPool.QueueUserWorkItem(ResponseResponseAsync, new RecordedDataSourceSelectorEventArgs(this, _responses, SelectedIndex, _requestNumber++, data, dataDescription, method, ContentType, Tag, responseType)); }
/// <summary> /// Sends request to the data source. /// </summary> private void InternalSendRequest(string relativeUrlPath, string dataDescription, byte[] data, int dataLength, string method, bool asynchronous, HttpDataSourceResponseType responseType) { if (dataDescription == null) throw new ArgumentOutOfRangeException("dataDescription"); if (_request != null) throw new InvalidOperationException("Another request is being processed. Call Cancel() method first."); if (method != MethodPost && method != MethodGet && method != MethodDelete && method != MethodPut) throw new InvalidOperationException("Invalid method used. Try 'POST' or 'GET'."); if (data != null && dataLength > data.Length) throw new ArgumentOutOfRangeException("dataLength"); ////////////////////// // FILL HTTP request: DateTime now = DateTime.Now; string uri = CreateUri(_url, relativeUrlPath); HttpWebRequest webRequest = CreateHttpWebRequest(uri, method, now, GetSerializedDataLength(data, dataLength)); SetRequest(webRequest); _sendAt = now; if (asynchronous) { ////////////////////// // Format data to sent: var dataRequest = new AsyncDataRequest(webRequest, dataDescription, data, dataLength, responseType); if (data != null && data.Length > 0 && dataLength > 0) { webRequest.BeginGetRequestStream(AsyncWebRequestStreamCallback, dataRequest); } else { DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "<--- Starting request [{0}] (at: {1}): {2}", _id, now, webRequest.RequestUri.AbsoluteUri)); ////////////////////// // Send request: try { webRequest.BeginGetResponse(AsyncWebResponseCallback, dataRequest); } catch (SecurityException ex) { ProcessResponse(webRequest, null, responseType, ex); } catch (ProtocolViolationException ex) { ProcessResponse(webRequest, null, responseType, ex); } catch (WebException ex) { if (ex.Status != WebExceptionStatus.RequestCanceled) ProcessResponse(webRequest, null, responseType, ex); } } } else { ////////////////////// // Format data to sent: if (data != null && data.Length > 0 && dataLength > 0) { DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "<--- Sending request [{0}] (length: {1} bytes, at: {2}): {3} ({4})", _id, dataDescription.Length, now, webRequest.RequestUri.AbsoluteUri, ContentType)); DebugLogWriteSentContentDescription(ContentType, dataDescription); if (WriteRequestData(webRequest, null, data, dataLength)) return; } else { DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "<--- Starting request [{0}] (at: {1}): {2}", _id, now, webRequest.RequestUri.AbsoluteUri)); } ////////////////////// // Send request and process response: HttpWebResponse response; bool canProcess = true; Exception exception = null; try { response = GetResponse(webRequest); } catch (SecurityException ex) { response = null; exception = ex; } catch (ProtocolViolationException ex) { response = null; exception = ex; } catch (WebException ex) { canProcess = ex.Status != WebExceptionStatus.RequestCanceled; response = (HttpWebResponse)ex.Response; exception = ex; } if (canProcess) ProcessResponse(webRequest, response, responseType, exception); } }
/// <summary> /// Processes the content of response received in context of given request. /// </summary> protected void ProcessResponse(HttpWebRequest request, HttpWebResponse response, HttpDataSourceResponseType responseType, Exception ex) { // data reception failed or timeouted/cancelled? if (response == null) { string statusDescription = ex != null ? string.IsNullOrEmpty(ex.Message) ? ex.GetType().Name : string.Concat(ex.GetType().Name, ": ", ex.Message) : "Unknown error"; ProcessFailedResponse(request, HttpStatusCode.ServiceUnavailable, statusDescription); return; } ProcessSuccessfulResponse(request, response, responseType); #if WINDOWS_STORE response.Dispose(); #else response.Close(); #endif }
private void ProcessSuccessfulResponse(HttpWebRequest request, HttpWebResponse response, HttpDataSourceResponseType responseType) { // process the data: HttpStatusCode responseStatusCode = response.StatusCode; string responseStatusDescription = string.IsNullOrEmpty(response.StatusDescription) ? responseStatusCode.ToString() : response.StatusDescription; string stringData; byte[] binaryData; Stream streamData; if (IsFailureCode(responseStatusCode)) { DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "---> Received response [{0}] (length: {1} bytes, at: {2}, waiting: {3:F2} sec) with status: {4} ({5}, {6}, {7})", _id, GetContentLength(response), DateTime.Now, (DateTime.Now - GetSendAt(request)).TotalSeconds, responseStatusCode, (int)responseStatusCode, responseStatusDescription, string.IsNullOrEmpty(response.ContentType)? UnknownContentType : response.ContentType)); try { ReadResponseData(response, responseType, out stringData, out binaryData, out streamData); DebugLogWriteReceivedContentDescription(response.ContentType, stringData, binaryData != null || streamData != null); } catch { stringData = null; binaryData = null; streamData = null; } if (TryCompleteRequest(request)) Event.Invoke(DataReceiveFailed, this, new HttpDataSourceEventArgs(this, responseStatusCode, responseStatusDescription, stringData, binaryData, streamData)); else DebugLog.WriteCoreLine("Ignoring reception due to previous request cancellation!"); return; } try { ReadResponseData(response, responseType, out stringData, out binaryData, out streamData); long length = stringData != null ? stringData.Length : (binaryData != null ? binaryData.Length : GetContentLength(response)); DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "---> Received response [{0}] (length: {1} bytes, at: {2}, waiting: {3:F2} sec) with status: {4} ({5}, {6}, {7})", _id, length, DateTime.Now, (DateTime.Now - GetSendAt(request)).TotalSeconds, responseStatusCode, (int)responseStatusCode, responseStatusDescription, string.IsNullOrEmpty(response.ContentType) ? UnknownContentType : response.ContentType)); DebugLogWriteReceivedContentDescription(response.ContentType, stringData, binaryData != null || streamData != null); } catch (Exception ex) { DebugLog.WriteCoreLine(string.Format("Response reception error: {0}", ex.Message)); if (TryCompleteRequest(request)) Event.Invoke(DataReceiveFailed, this, new HttpDataSourceEventArgs(this, responseStatusCode, responseStatusDescription)); else DebugLog.WriteCoreLine("Ignoring reception due to previous request cancellation!"); return; } if (TryCompleteRequest(request)) Event.Invoke(DataReceived, this, new HttpDataSourceEventArgs(this, responseStatusCode, responseStatusDescription, stringData, binaryData, streamData)); else DebugLog.WriteCoreLine("Ignoring reception due to previous request cancellation!"); }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string method, HttpDataSourceResponseType responseType) { InternalSendRequest(null, NoContent, null, 0, method, true, responseType); }
public AsyncDataRequest(HttpWebRequest request, string dataDescription, byte[] data, int dataLength, HttpDataSourceResponseType responseType) { if (request == null) throw new ArgumentNullException("request"); Request = request; Data = data; DataLength = dataLength; ResponseType = responseType; #if DEBUG DataDescription = dataDescription; #endif }
/// <summary> /// Sends request to the data source. /// </summary> private void InternalSendRequest(string relativeUrlPath, string dataDescription, byte[] data, int dataLength, string method, bool asynchronous, HttpDataSourceResponseType responseType) { if (dataDescription == null) { throw new ArgumentOutOfRangeException("dataDescription"); } if (_request != null) { throw new InvalidOperationException("Another request is being processed. Call Cancel() method first."); } if (method != MethodPost && method != MethodGet && method != MethodDelete && method != MethodPut) { throw new InvalidOperationException("Invalid method used. Try 'POST' or 'GET'."); } if (data != null && dataLength > data.Length) { throw new ArgumentOutOfRangeException("dataLength"); } ////////////////////// // FILL HTTP request: DateTime now = DateTime.Now; string uri = CreateUri(_url, relativeUrlPath); HttpWebRequest webRequest = CreateHttpWebRequest(uri, method, now, GetSerializedDataLength(data, dataLength)); SetRequest(webRequest); _sendAt = now; if (asynchronous) { ////////////////////// // Format data to sent: var dataRequest = new AsyncDataRequest(webRequest, dataDescription, data, dataLength, responseType); if (data != null && data.Length > 0 && dataLength > 0) { webRequest.BeginGetRequestStream(AsyncWebRequestStreamCallback, dataRequest); } else { DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "<--- Starting request [{0}] (at: {1}): {2}", _id, now, webRequest.RequestUri.AbsoluteUri)); ////////////////////// // Send request: try { webRequest.BeginGetResponse(AsyncWebResponseCallback, dataRequest); } catch (SecurityException ex) { ProcessResponse(webRequest, null, responseType, ex); } catch (ProtocolViolationException ex) { ProcessResponse(webRequest, null, responseType, ex); } catch (WebException ex) { if (ex.Status != WebExceptionStatus.RequestCanceled) { ProcessResponse(webRequest, null, responseType, ex); } } } } else { ////////////////////// // Format data to sent: if (data != null && data.Length > 0 && dataLength > 0) { DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "<--- Sending request [{0}] (length: {1} bytes, at: {2}): {3} ({4})", _id, dataDescription.Length, now, webRequest.RequestUri.AbsoluteUri, ContentType)); DebugLogWriteSentContentDescription(ContentType, dataDescription); if (WriteRequestData(webRequest, null, data, dataLength)) { return; } } else { DebugLog.WriteCoreLine(string.Format(CultureInfo.InvariantCulture, "<--- Starting request [{0}] (at: {1}): {2}", _id, now, webRequest.RequestUri.AbsoluteUri)); } ////////////////////// // Send request and process response: HttpWebResponse response; bool canProcess = true; Exception exception = null; try { response = GetResponse(webRequest); } catch (SecurityException ex) { response = null; exception = ex; } catch (ProtocolViolationException ex) { response = null; exception = ex; } catch (WebException ex) { canProcess = ex.Status != WebExceptionStatus.RequestCanceled; response = (HttpWebResponse)ex.Response; exception = ex; } if (canProcess) { ProcessResponse(webRequest, response, responseType, exception); } } }
/// <summary> /// Sends asynchronous request to the data source. /// </summary> public void SendRequestAsync(string method, HttpDataSourceResponseType responseType) { InternalSendRequestAsync(new byte[0], 0, BinaryContentDescription, method, responseType); }