예제 #1
0
        /// <summary>
        /// Do asynchronous web post.
        /// Register EndAsyncResponse and callback method.
        /// </summary>
        /// <param name="state">Async Web method state</param>
        private void EndAsyncRequest(IAsyncResult state)
        {
            AsyncWebMethodRequest input = state.AsyncState as AsyncWebMethodRequest;

            try
            {
                WebRequest request       = input.Request;
                Stream     requestStream = request.EndGetRequestStream(state);

                ASCIIEncoding encoding  = new ASCIIEncoding();
                byte[]        postBytes = encoding.GetBytes(input.PostData);
                requestStream.Write(postBytes, 0, postBytes.Length);
                requestStream.Close();

                request.BeginGetResponse(EndAsyncResponse, input);
            }
            catch (Exception ex)
            {
                AsyncWebMethodResponse asyncResponse = new AsyncWebMethodResponse(input.State);
                asyncResponse.StatusDescription = ex.Message;
                if (ex.InnerException != null)
                {
                    asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                    "{0}\n{1}",
                                                                    asyncResponse.StatusDescription,
                                                                    ex.InnerException.Message);
                }

                asyncResponse.Status = AsyncMethodState.Failed;
                asyncResponse.Error  = ex;

                input.Callback(asyncResponse);
            }
        }
예제 #2
0
        /// <summary>
        /// Read asynchronous web response.
        /// Invoke the callback method to report completion.
        /// </summary>
        /// <param name="state">Async Web method state</param>
        private void EndAsyncResponse(IAsyncResult state)
        {
            AsyncWebMethodRequest  input         = state.AsyncState as AsyncWebMethodRequest;
            AsyncWebMethodResponse asyncResponse = new AsyncWebMethodResponse(input.State);

            try
            {
                WebRequest      request     = input.Request;
                HttpWebResponse webResponse = (HttpWebResponse)request.EndGetResponse(state);
                asyncResponse.StatusDescription = webResponse.StatusDescription;
                if (asyncResponse.StatusDescription == "OK")
                {
                    asyncResponse.Result = webResponse.GetResponseStream();
                    asyncResponse.Status = AsyncMethodState.Passed;
                }
                else
                {
                    asyncResponse.Status = AsyncMethodState.Passed;
                }
            }
            catch (WebException we)
            {
                HttpWebResponse response = (HttpWebResponse)we.Response;
                if (response == null)
                {
                    asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                    "{0}{1}",
                                                                    asyncResponse.StatusDescription,
                                                                    we.Message);
                    if (we.InnerException != null)
                    {
                        asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                        "{0}\n{1}",
                                                                        asyncResponse.StatusDescription,
                                                                        we.InnerException.Message);
                    }
                }
                else
                {
                    asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                    "{0}WebException: {1}",
                                                                    asyncResponse.StatusDescription,
                                                                    response.StatusDescription);
                }

                asyncResponse.Status = AsyncMethodState.Failed;
                asyncResponse.Error  = we;
            }
            catch (Exception ex)
            {
                asyncResponse.StatusDescription = ex.Message;
                if (ex.InnerException != null)
                {
                    asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                    "{0}\n{1}",
                                                                    asyncResponse.StatusDescription,
                                                                    ex.InnerException.Message);
                }

                asyncResponse.Status = AsyncMethodState.Failed;
                asyncResponse.Error  = ex;
            }

            input.Callback(asyncResponse);
        }
예제 #3
0
        /// <summary>
        /// This method instantiates and invokes asynchronous web call.
        /// Create the web request object and do a Async call
        /// 1.	If post data is required register EndAsyncRequest as callback.
        /// 2.	Otherwise register EndAsyncResponse as callback method.
        /// </summary>
        /// <param name="input">Input parameters</param>
        public void BeginAsyncRequest(AsyncWebMethodRequest input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            AsyncWebMethodResponse asyncResponse = new AsyncWebMethodResponse(input.State);

            try
            {
                WebRequest request = null;
                if (string.IsNullOrEmpty(input.PostData))
                {
                    string queryString = BuildQueryString(input.Parameter);
                    request = CreateGetRequest(input.Url, queryString, input.Credential);

                    Close();    // get rid of any old response
                    input.Request = request;
                    request.BeginGetResponse(EndAsyncResponse, input);
                }
                else
                {
                    request       = CreatePostRequest(input.Url, input.Credential, input.PostData.Length);
                    input.Request = request;
                    request.BeginGetRequestStream(EndAsyncRequest, input);
                }
            }
            catch (WebException we)
            {
                HttpWebResponse response = (HttpWebResponse)we.Response;
                if (response == null)
                {
                    asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                    "{0}{1}",
                                                                    asyncResponse.StatusDescription,
                                                                    we.Message);
                    if (we.InnerException != null)
                    {
                        asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                        "{0}\n{1}",
                                                                        asyncResponse.StatusDescription,
                                                                        we.InnerException.Message);
                    }
                }
                else
                {
                    asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                    "{0}WebException: {1}",
                                                                    asyncResponse.StatusDescription,
                                                                    response.StatusDescription);
                }

                asyncResponse.Status = AsyncMethodState.Failed;
                asyncResponse.Error  = we;

                input.Callback(asyncResponse);
            }
            catch (Exception ex)
            {
                asyncResponse.StatusDescription = ex.Message;
                if (ex.InnerException != null)
                {
                    asyncResponse.StatusDescription = string.Format(CultureInfo.InvariantCulture,
                                                                    "{0}\n{1}",
                                                                    asyncResponse.StatusDescription,
                                                                    ex.InnerException.Message);
                }

                asyncResponse.Status = AsyncMethodState.Failed;
                asyncResponse.Error  = ex;

                input.Callback(asyncResponse);
            }
        }