Beispiel #1
0
        /// <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 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);
            }
        }