Beispiel #1
0
        void BeginGetResponseCallback(IAsyncResult asyncResult)
        {
            WebResponse webResponse             = null;
            Stream      responseStream          = null;
            HttpWebRequestAsyncState asyncState = null;

            try
            {
                asyncState     = (HttpWebRequestAsyncState)asyncResult.AsyncState;
                webResponse    = asyncState.HttpWebRequest.EndGetResponse(asyncResult);
                responseStream = webResponse.GetResponseStream();
                var webRequestCallbackState = new HttpWebRequestCallbackState(responseStream, asyncState.State);
                asyncState.ResponseCallback(webRequestCallbackState);
                responseStream.Close();
                responseStream = null;
                webResponse.Close();
                webResponse = null;
            }
            catch (Exception ex)
            {
                //SharpLog.Error("BeginGetResponseCallback", ex.ToString());
                if (asyncState != null)
                {
                    asyncState.ResponseCallback(new HttpWebRequestCallbackState(ex));
                }
                else
                {
                    throw new Exception(ex.ToString());
                }
            }
            finally
            {
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
        }
Beispiel #2
0
        void BeginGetRequestStreamCallback(IAsyncResult asyncResult)
        {
            Stream requestStream = null;
            HttpWebRequestAsyncState asyncState = null;

            try
            {
                asyncState    = (HttpWebRequestAsyncState)asyncResult.AsyncState;
                requestStream = asyncState.HttpWebRequest.EndGetRequestStream(asyncResult);
                requestStream.Write(asyncState.RequestBytes, 0, asyncState.RequestBytes.Length);
                requestStream.Close();
                asyncState.HttpWebRequest.BeginGetResponse(BeginGetResponseCallback,
                                                           new HttpWebRequestAsyncState
                {
                    HttpWebRequest   = asyncState.HttpWebRequest,
                    ResponseCallback = asyncState.ResponseCallback,
                    State            = asyncState.State
                });
            }
            catch (Exception ex)
            {
                if (asyncState != null)
                {
                    asyncState.ResponseCallback(new HttpWebRequestCallbackState(ex));
                }
                else
                {
                    throw new Exception(ex.ToString());
                }
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
            }
        }
Beispiel #3
0
        public virtual void PostUploadAsyncTask(string url, byte[] data,
                                                Action <HttpWebRequestCallbackState> responseCallback, string strCookies, string strHost, string strReferer, string strAccept, object state = null, bool isEucKR = true,
                                                string contentType = "application/x-www-form-urlencoded; charset=UTF-8")
        {
            var httpWebRequest = CreateHttpWebRequest(url, "POST", contentType);

            httpWebRequest.ContentLength = data.Length;
            httpWebRequest.Host          = strHost;
            httpWebRequest.Referer       = strReferer;
            //httpWebRequest.Accept = strAccept;
            //httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            //httpWebRequest.UserAgent = "UnityPlayer/2018.4.10f1 (UnityWebRequest/1.0, libcurl/7.52.0-DEV)";

            //  httpWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

            CookieContainer cookiecontainer = new CookieContainer();
            //string[] cookies = strCookies.Split(';');
            //foreach (string cookie in cookies)
            //    cookiecontainer.SetCookies(new Uri(url), cookie);
            //httpWebRequest.CookieContainer = cookiecontainer;

            //Stream rs = httpWebRequest.GetRequestStream();
            //rs.Write(data, 0, data.Length);
            //rs.Close();

            var asyncState = new HttpWebRequestAsyncState()
            {
                RequestBytes     = data,
                HttpWebRequest   = httpWebRequest,
                ResponseCallback = responseCallback,
                State            = state
            };

            Task.Factory.FromAsync <Stream>(httpWebRequest.BeginGetRequestStream,
                                            httpWebRequest.EndGetRequestStream, asyncState, TaskCreationOptions.None)
            .ContinueWith <HttpWebRequestAsyncState>(task =>
            {
                var asyncState2 = (HttpWebRequestAsyncState)task.AsyncState;
                using (var requestStream = task.Result)
                {
                    requestStream.Write(asyncState2.RequestBytes, 0, asyncState2.RequestBytes.Length);
                }
                return(asyncState2);
            })
            .ContinueWith(task =>
            {
                var httpWebRequestAsyncState2 = (HttpWebRequestAsyncState)task.Result;
                var hwr2 = httpWebRequestAsyncState2.HttpWebRequest;
                Task.Factory.FromAsync <WebResponse>(hwr2.BeginGetResponse,
                                                     hwr2.EndGetResponse, httpWebRequestAsyncState2, TaskCreationOptions.None)
                .ContinueWith(task2 =>
                {
                    WebResponse webResponse = null;
                    Stream responseStream   = null;
                    try
                    {
                        var asyncState3 = (HttpWebRequestAsyncState)task2.AsyncState;
                        webResponse     = task2.Result;
                        responseStream  = webResponse.GetResponseStream();
                        responseCallback(new HttpWebRequestCallbackState(responseStream, asyncState3));
                    }
                    finally
                    {
                        if (responseStream != null)
                        {
                            responseStream.Close();
                        }
                        if (webResponse != null)
                        {
                            webResponse.Close();
                        }
                    }
                });
            });
        }
Beispiel #4
0
        /// <summary>
        /// This method does an Http Post asyncronous task to the provided url and calls the responseCallback delegate
        /// </summary>
        /// <param name="url"></param>
        /// <param name="strHost"></param>
        /// <param name="strReferer"></param>
        /// <param name="postParameters"></param>
        /// <param name="strOrigin"></param>
        /// <param name="responseCallback"></param>
        /// <param name="strCookies"></param>
        /// <param name="state"></param>
        /// <param name="contentType"></param>
        public virtual void PostAsyncTask(string url, string strHost, string strReferer, byte[] postParameters, string strOrigin,
                                          Action <HttpWebRequestCallbackState> responseCallback, string strCookies, object state = null,
                                          string contentType = "application/x-www-form-urlencoded; charset=UTF-8")
        {
            var httpWebRequest = CreateHttpWebRequest(url, "POST", contentType);
            //var requestBytes = GetRequestBytes(postParameters, isEucKR);
            var requestBytes = postParameters;

            httpWebRequest.ContentLength          = requestBytes.Length;
            httpWebRequest.Host                   = strHost;
            httpWebRequest.Referer                = strReferer;
            httpWebRequest.Accept                 = "application/json, text/javascript, */*; q=0.01";
            httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            httpWebRequest.UserAgent              = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36";
            httpWebRequest.Headers.Add("Accept-Language", "en-US,en;q=0.9,ko;q=0.8");
            WebHeaderCollection headers = httpWebRequest.Headers;

            headers.Add("Accept-Encoding:gzip, deflate");
            headers.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.7,ko;q=0.3");
            headers.Add("Cache-Control", "no-cache");
            headers.Add("Origin", strOrigin);
            headers.Add("Upgrade-Insecure-Requests", "1");
            headers.Add("Sec-Fetch-Dest", "document");
            headers.Add("Sec-Fetch-Site", "same-origin");
            headers.Add("Sec-Fetch-Mode", "navigate");
            headers.Add("Sec-Fetch-User", "?1");
            headers.Add("X_REQUESTED_WITH", "XMLHttpRequest");

            CookieContainer cookiecontainer = new CookieContainer();

            string[] cookies = strCookies.Split(';');
            foreach (string cookie in cookies)
            {
                cookiecontainer.SetCookies(new Uri(url), cookie);
            }
            httpWebRequest.CookieContainer = cookiecontainer;


            var asyncState = new HttpWebRequestAsyncState()
            {
                RequestBytes     = requestBytes,
                HttpWebRequest   = httpWebRequest,
                ResponseCallback = responseCallback,
                State            = state
            };

            Task.Factory.FromAsync <Stream>(httpWebRequest.BeginGetRequestStream,
                                            httpWebRequest.EndGetRequestStream, asyncState, TaskCreationOptions.None)
            .ContinueWith <HttpWebRequestAsyncState>(task =>
            {
                var asyncState2 = (HttpWebRequestAsyncState)task.AsyncState;
                using (var requestStream = task.Result)
                {
                    requestStream.Write(asyncState2.RequestBytes, 0, asyncState2.RequestBytes.Length);
                }
                return(asyncState2);
            })
            .ContinueWith(task =>
            {
                var httpWebRequestAsyncState2 = (HttpWebRequestAsyncState)task.Result;
                var hwr2 = httpWebRequestAsyncState2.HttpWebRequest;
                Task.Factory.FromAsync <WebResponse>(hwr2.BeginGetResponse,
                                                     hwr2.EndGetResponse, httpWebRequestAsyncState2, TaskCreationOptions.None)
                .ContinueWith(task2 =>
                {
                    WebResponse webResponse = null;
                    Stream responseStream   = null;
                    try
                    {
                        var asyncState3 = (HttpWebRequestAsyncState)task2.AsyncState;
                        webResponse     = task2.Result;
                        responseStream  = webResponse.GetResponseStream();
                        responseCallback(new HttpWebRequestCallbackState(responseStream, asyncState3));
                    }
                    finally
                    {
                        if (responseStream != null)
                        {
                            responseStream.Close();
                        }
                        if (webResponse != null)
                        {
                            webResponse.Close();
                        }
                    }
                });
            });
        }