private void ResponseReady(IAsyncResult asyncResult)
        {
            WebRequest request = asyncResult.AsyncState as WebRequest;

            using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult))
            {
                using (Stream stream = response.GetResponseStream())
                {
                    // 设置流格式的编码格式

                    Encoding encoding = Encoding.UTF8;

                    if (!string.IsNullOrEmpty(response.CharacterSet))
                    {
                        encoding = Encoding.GetEncoding(response.CharacterSet);
                    }

                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        this.responseData = ParseResponseData(streamReader.ReadToEnd());

                        if (this.callback != null)
                        {
                            this.callback(this.responseData.ResponseText);
                        }
                    }
                }
            }
        }
        /// <summary>发送同步请求</summary>
        /// <param name="requestData">请求的数据</param>
        /// <param name="httpMethod">请求的方式( GET | POST )</param>
        /// <param name="contentType">内容类型</param>
        /// <param name="content">内容</param>
        public static string Request(AjaxRequestData requestData, string httpMethod, string contentType, string content)
        {
            AjaxRequest request = new AjaxRequest(requestData);

            AjaxResponseData response = request.Request(requestData.ActionUri, httpMethod, contentType, content);

            return(response.ResponseText);
        }
        // -------------------------------------------------------
        // 静态方法
        // -------------------------------------------------------

        #region 静态函数:Request(AjaxRequestData requestData)
        /// <summary>发送同步请求</summary>
        /// <param name="requestData">请求的数据信息</param>
        public static string Request(AjaxRequestData requestData)
        {
            AjaxRequest request = new AjaxRequest(requestData);

            AjaxResponseData response = request.Request(requestData.ActionUri, "POST");

            return(response.ResponseText);
        }
        // -------------------------------------------------------
        // 解析返回结果
        // -------------------------------------------------------

        #region 函数:ParseResponseData(AjaxResponseData responseData)
        /// <summary>解析相应的数据</summary>
        /// <param name="responseData"></param>
        /// <returns></returns>
        private AjaxResponseData ParseResponseData(string responseText)
        {
            if (this.responseData == null)
            {
                this.responseData = new AjaxResponseData();
            }

            responseData.ResponseText = responseText;

            if (string.IsNullOrEmpty(responseText))
            {
                this.responseData.Result.Set("1", "没有返回任何信息。");
                return(this.responseData);
            }

            return(this.responseData);
        }
예제 #5
0
 public AjaxRequestException(string message, AjaxResponseData responseData, Exception innerException)
     : base(message, innerException)
 {
     this.responseData = responseData;
 }
예제 #6
0
 public AjaxRequestException(string message, AjaxResponseData responseData)
     : base(message)
 {
     this.responseData = responseData;
 }
        /// <summary>发送请求</summary>
        /// <param name="uri">请求的地址</param>
        /// <param name="httpMethod">请求的方式( GET | POST )</param>
        /// <param name="contentType">内容类型</param>
        /// <param name="content">内容</param>
        /// <returns>响应的数据信息</returns>
        private AjaxResponseData Request(Uri uri, string httpMethod, string contentType, string content)
        {
            // -------------------------------------------------------
            // 1.发送请求
            // -------------------------------------------------------

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            if (!string.IsNullOrEmpty(requestData.LoginName))
            {
                // 设置身份验证信息
                request.Credentials = new NetworkCredential(requestData.LoginName, requestData.Password);
            }

            request.Method = httpMethod;

            request.ContentType = contentType;

            if (request.Method == "POST")
            {
                byte[] buffer = Encoding.UTF8.GetBytes(content);

                request.ContentLength = buffer.Length;

                if (buffer.Length > 0)
                {
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            else
            {
                // Some limitations
                request.MaximumAutomaticRedirections = 4;
                request.MaximumResponseHeadersLength = 4;
                request.ContentLength = 0;
            }

            // -------------------------------------------------------
            // 2.返回响应信息
            // -------------------------------------------------------

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    // 获取相关的响应流
                    using (Stream stream = response.GetResponseStream())
                    {
                        // 设置流格式的编码格式

                        Encoding encoding = Encoding.UTF8;

                        if (!string.IsNullOrEmpty(response.CharacterSet))
                        {
                            encoding = Encoding.GetEncoding(response.CharacterSet);
                        }

                        using (StreamReader streamReader = new StreamReader(stream, encoding))
                        {
                            this.responseData = ParseResponseData(streamReader.ReadToEnd());

                            streamReader.Close();
                        }

                        stream.Close();
                    }

                    response.Close();
                }
            }
            catch (Exception ex)
            {
                throw new AjaxRequestException(ex.Message, requestData, ex);
            }

            return(this.responseData);
        }