コード例 #1
0
 public AsyncHttpURLConnection(MethodType methodType, string url, string message, AsyncHttpCallback callback)
 {
     _methodType = methodType;
     _url        = new Uri(url);
     _message    = message ?? "";
     _callback   = callback;
 }
コード例 #2
0
ファイル: AsyncHttp.cs プロジェクト: wingahi/CSHive
        /// <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);
        }
コード例 #3
0
ファイル: AsyncHttp.cs プロジェクト: dkme/moooyo
        const int DefaultTimeout = 60 * 1000; // 1 minutes timeout

        #endregion Fields

        #region Methods

        //异步方式发起http get请求
        public bool HttpGet(string url, string queryString, AsyncHttpCallback callback)
        {
            if (!string.IsNullOrEmpty(queryString))
            {
                url += "?" + queryString;
            }

            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
            webRequest.Method = "GET";
            webRequest.ServicePoint.Expect100Continue = false;

            try
            {
                RequestState state = new RequestState();
                state.cb = callback;
                state.request = webRequest;

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

                // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), webRequest, DefaultTimeout, 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;
            }

            return true;
        }
コード例 #4
0
ファイル: AsyncHttp.cs プロジェクト: wingahi/CSHive
 public RequestState()
 {
     BufferRead     = new byte[BUFFER_SIZE];
     RequestData    = new StringBuilder("");
     Request        = null;
     StreamResponse = null;
     AsyncCallBack  = null;
 }
コード例 #5
0
ファイル: AsyncHttp.cs プロジェクト: zhongshuiyuan/MT2017
 public RequestState()
 {
     BufferRead     = new byte[BUFFER_SIZE];
     requestData    = new StringBuilder("");
     request        = null;
     streamResponse = null;
     cb             = null;
 }
コード例 #6
0
ファイル: AsyncHttp.cs プロジェクト: zhongshuiyuan/MT2017
        //异步方式发起http post请求
        public bool HttpPost(string url, string queryString, AsyncHttpCallback callback)
        {
            StreamWriter requestWriter = null;
            // StreamReader responseReader = null;

            // string responseData = null;

            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ServicePoint.Expect100Continue = false;

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

                RequestState state = new RequestState();
                state.cb      = callback;
                state.request = webRequest;

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

                // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), webRequest, DefaultTimeout, 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);
            }
            finally
            {
                if (requestWriter != null)
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            }

            return(true);
        }
コード例 #7
0
ファイル: AsyncHttp.cs プロジェクト: dkme/moooyo
        //异步方式发起http post请求
        public bool HttpPost(string url, string queryString, AsyncHttpCallback callback)
        {
            StreamWriter requestWriter = null;
            // StreamReader responseReader = null;

            // string responseData = null;

            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ServicePoint.Expect100Continue = false;

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

                RequestState state = new RequestState();
                state.cb = callback;
                state.request = webRequest;

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

                // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), webRequest, DefaultTimeout, 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;
            }
            finally
            {
                if (requestWriter != null)
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            }

            return true;
        }
コード例 #8
0
ファイル: AsyncHttp.cs プロジェクト: wingahi/CSHive
        /// <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);
        }
コード例 #9
0
ファイル: AsyncHttp.cs プロジェクト: wingahi/CSHive
        /// <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;
        }
コード例 #10
0
ファイル: AsyncHttp.cs プロジェクト: zhongshuiyuan/MT2017
        const int DefaultTimeout        = 60 * 1000; // 1 minutes timeout

        //异步方式发起http get请求
        public bool HttpGet(string url, string queryString, AsyncHttpCallback callback)
        {
            if (!string.IsNullOrEmpty(queryString))
            {
                url += "?" + queryString;
            }

            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;

            webRequest.Method = "GET";
            webRequest.ServicePoint.Expect100Continue = false;

            try
            {
                RequestState state = new RequestState();
                state.cb      = callback;
                state.request = webRequest;

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

                // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), webRequest, DefaultTimeout, 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);
            }

            return(true);
        }
コード例 #11
0
ファイル: AsyncHttp.cs プロジェクト: dkme/moooyo
 public RequestState()
 {
     BufferRead = new byte[BUFFER_SIZE];
     requestData = new StringBuilder("");
     request = null;
     streamResponse = null;
     cb = null;
 }
コード例 #12
0
ファイル: HttpHelper.cs プロジェクト: wingahi/CSHive
        /// <summary>
        /// 异步:Post方法请求并返回消息 ,可配超时
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <param name="urlParameters">参数集合</param>
        /// <param name="timeout">超时,豪秒</param>
        /// <param name="callback">结束后的回调</param>
        /// <returns></returns>
        public static bool AsyncPost(string url, HttpParams urlParameters, int timeout, AsyncHttpCallback callback)
        {
            var sync = new AsyncHttp(timeout);

            return(sync.HttpPost(url, urlParameters, callback));
        }
コード例 #13
0
ファイル: AsyncHttp.cs プロジェクト: zhongshuiyuan/MT2017
        //异步方式发起http post请求,可以同时上传文件
        public bool HttpPostWithFile(string url, string queryString, List <APIParameter> files, AsyncHttpCallback callback)
        {
            Stream requestStream = null;
            string boundary      = DateTime.Now.Ticks.ToString("x");

            url += '?' + queryString;
            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;

            webRequest.ServicePoint.Expect100Continue = true;
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            webRequest.Method      = "POST";
            webRequest.KeepAlive   = true;
            webRequest.Credentials = CredentialCache.DefaultCredentials;

            try
            {
                Stream memStream = new MemoryStream();

                byte[] boundarybytes    = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
                string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

                List <APIParameter> listParams = HttpUtil.GetQueryParameters(queryString);

                foreach (APIParameter param in listParams)
                {
                    //需要将
                    string formitem      = string.Format(formdataTemplate, param.Name, (FormParamDecode(param.Value)));
                    byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
                    memStream.Write(formitembytes, 0, formitembytes.Length);
                }

                memStream.Write(boundarybytes, 0, boundarybytes.Length);

                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: \"{2}\"\r\n\r\n";

                foreach (APIParameter param in files)
                {
                    string name        = param.Name;
                    string filePath    = param.Value;
                    string file        = Path.GetFileName(filePath);
                    string contentType = HttpUtil.GetContentType(file);

                    string header      = string.Format(headerTemplate, name, file, contentType);
                    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

                    memStream.Write(headerbytes, 0, headerbytes.Length);

                    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    byte[]     buffer     = new byte[1024];
                    int        bytesRead  = 0;

                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        memStream.Write(buffer, 0, bytesRead);
                    }

                    memStream.Write(boundarybytes, 0, boundarybytes.Length);
                    fileStream.Close();
                }

                webRequest.ContentLength = memStream.Length;

                requestStream = webRequest.GetRequestStream();

                memStream.Position = 0;
                byte[] tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                requestStream.Close();
                requestStream = null;

                RequestState state = new RequestState();
                state.cb      = callback;
                state.request = webRequest;

                IAsyncResult result = (IAsyncResult)webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), state);
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback),
                                                       webRequest, DefaultTimeout, true);
                allDone.WaitOne();

                state.response.Close();
            }
            catch
            {
                return(false);
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                    requestStream = null;
                }
            }

            return(true);
        }
コード例 #14
0
ファイル: AsyncHttp.cs プロジェクト: wingahi/CSHive
        /// <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;
        }
コード例 #15
0
ファイル: AsyncHttp.cs プロジェクト: wingahi/CSHive
 public RequestState()
 {
     BufferRead = new byte[BUFFER_SIZE];
     RequestData = new StringBuilder("");
     Request = null;
     StreamResponse = null;
     AsyncCallBack = null;
 }
コード例 #16
0
ファイル: HttpHelper.cs プロジェクト: wingahi/CSHive
        /// <summary>
        /// 异步:Get方法请求并返回消息 ,超时可配
        /// </summary>
        /// <param name="url">包含查询字符串的Url地址</param>
        /// <param name="timeout">超时,豪秒</param>
        /// <param name="callback">结束后的回调</param>
        /// <returns></returns>
        public static bool AsyncGet(string url, int timeout, AsyncHttpCallback callback)
        {
            var sync = new AsyncHttp(timeout);

            return(sync.HttpGet(url, callback));
        }
コード例 #17
0
ファイル: HttpHelper.cs プロジェクト: wingahi/CSHive
 /// <summary>
 /// 异步:Get方法请求并返回消息 ,超时可配
 /// </summary>
 /// <param name="url">包含查询字符串的Url地址</param>
 /// <param name="timeout">超时,豪秒</param>
 /// <param name="callback">结束后的回调</param>
 /// <returns></returns>
 public static bool AsyncGet(string url, int timeout, AsyncHttpCallback callback)
 {
     var sync = new AsyncHttp(timeout);
     return sync.HttpGet(url, callback);
 }
コード例 #18
0
ファイル: HttpHelper.cs プロジェクト: wingahi/CSHive
 /// <summary>
 /// 异步:Post方法请求并返回消息 ,可配超时
 /// </summary>
 /// <param name="url">包含查询字符串的Url地址</param>
 /// <param name="urlParameters">参数集合</param>
 /// <param name="timeout">超时,豪秒</param>
 /// <param name="callback">结束后的回调</param>
 /// <returns></returns>
 public static bool AsyncPost(string url, HttpParams urlParameters, int timeout, AsyncHttpCallback callback)
 {
     var sync = new AsyncHttp(timeout);
     return sync.HttpPost(url, urlParameters, callback);
 }
コード例 #19
0
ファイル: AsyncHttp.cs プロジェクト: dkme/moooyo
        //异步方式发起http post请求,可以同时上传文件
        public bool HttpPostWithFile(string url, string queryString, List<APIParameter> files, AsyncHttpCallback callback)
        {
            Stream requestStream = null;
            string boundary = DateTime.Now.Ticks.ToString("x");

            url += '?' + queryString;
            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
            webRequest.ServicePoint.Expect100Continue = true;
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            webRequest.Method = "POST";
            webRequest.KeepAlive = true;
            webRequest.Credentials = CredentialCache.DefaultCredentials;

            try
            {
                Stream memStream = new MemoryStream();

                byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
                string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

                List<APIParameter> listParams = HttpUtil.GetQueryParameters(queryString);

                foreach (APIParameter param in listParams)
                {
                    //需要将
                    string formitem = string.Format(formdataTemplate, param.Name, (FormParamDecode(param.Value)));
                    byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
                    memStream.Write(formitembytes, 0, formitembytes.Length);
                }

                memStream.Write(boundarybytes, 0, boundarybytes.Length);

                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: \"{2}\"\r\n\r\n";

                foreach (APIParameter param in files)
                {
                    string name = param.Name;
                    string filePath = param.Value;
                    string file = Path.GetFileName(filePath);
                    string contentType = HttpUtil.GetContentType(file);

                    string header = string.Format(headerTemplate, name, file, contentType);
                    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

                    memStream.Write(headerbytes, 0, headerbytes.Length);

                    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;

                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        memStream.Write(buffer, 0, bytesRead);
                    }

                    memStream.Write(boundarybytes, 0, boundarybytes.Length);
                    fileStream.Close();
                }

                webRequest.ContentLength = memStream.Length;

                requestStream = webRequest.GetRequestStream();

                memStream.Position = 0;
                byte[] tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                requestStream.Close();
                requestStream = null;

                RequestState state = new RequestState();
                state.cb = callback;
                state.request = webRequest;

                IAsyncResult result = (IAsyncResult)webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), state);
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback),
                    webRequest, DefaultTimeout, true);
                allDone.WaitOne();

                state.response.Close();
            }
            catch
            {
                return false;
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                    requestStream = null;
                }
            }

            return true;
        }