Beispiel #1
0
        /// <summary>
        /// 异步方式发起http get请求
        /// </summary>
        /// <param name="url">包含QueryString的URL</param>
        /// <param name="callback">结束后的回调</param>
        /// <returns>请求是否发出,异常时返回false</returns>
        public bool HttpGet(string url, AsyncHttpCallback callback)
        {
            var webRequest = CreateRequest(url, HttpMethod.GET);
            try
            {
                var state = new RequestState { AsyncCallBack = callback, Request = webRequest };
                IAsyncResult result = webRequest.BeginGetResponse(ResponseCallback, state);
                // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, webRequest, RequestTimeout, true);
                // The response came in the allowed time. The work processing will happen in the callback function.
                AllDone.WaitOne();
                // Release the HttpWebResponse resource.
                state.Response.Close();
            }
            catch
            {
                return false;
                //throw;
            }

            return true;
        }
Beispiel #2
0
        /// <summary>
        /// 异步方式发起http post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public bool HttpPost(string url, string postData, AsyncHttpCallback callback)
        {
            StreamWriter requestWriter = null;
            StreamReader responseReader = null;
            string responseData = null;

            var webRequest = CreateRequest(url, HttpMethod.POST);
            webRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {
                //POST the data.
                requestWriter = new StreamWriter(webRequest.GetRequestStream());
                requestWriter.Write(postData);
                requestWriter.Close();
                requestWriter = null;

                var state = new RequestState { AsyncCallBack = callback, Request = webRequest };

                IAsyncResult result = webRequest.BeginGetResponse(ResponseCallback, state);

                // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeoutCallback, webRequest,
                                                       RequestTimeout, true);

                // The response came in the allowed time. The work processing will happen in the
                // callback function.
                AllDone.WaitOne();

                // Release the HttpWebResponse resource.
                state.Response.Close();
            }
            catch
            {
                return false;
                //throw;
            }
            finally
            {
                if (requestWriter != null)
                    requestWriter.Close();
            }

            return true;
        }
Beispiel #3
0
 private void FireCallback(RequestState state)
 {
     //call back
     if (state.AsyncCallBack != null)
     {
         state.AsyncCallBack(this, state.RequestData.ToString());
     }
 }