示例#1
0
        private void GetResponseCallback(IAsyncResult ar)
        {
            HttpKsiServiceAsyncResult asyncResult = (HttpKsiServiceAsyncResult)ar.AsyncState;

            if (asyncResult.IsCompleted)
            {
                return;
            }

            try
            {
                using (WebResponse response = asyncResult.Request.EndGetResponse(ar))
                {
                    HandleWebResponse(response, asyncResult);
                    asyncResult.BeginWaitHandle.Set();
                }
            }
            catch (WebException e)
            {
                HttpWebResponse webResponse = e.Response as HttpWebResponse;

                if (webResponse != null && webResponse.StatusCode == HttpStatusCode.BadRequest)
                {
                    asyncResult.ResultStream = new MemoryStream();
                    HandleWebResponse(e.Response, asyncResult);
                    asyncResult.BeginWaitHandle.Set();
                }
                else
                {
                    SetError(asyncResult, e, "Get http response failed.");
                }
            }
        }
示例#2
0
        private void GetRequestStreamCallback(IAsyncResult ar)
        {
            HttpKsiServiceAsyncResult asyncResult = (HttpKsiServiceAsyncResult)ar.AsyncState;

            if (asyncResult.IsCompleted)
            {
                return;
            }

            byte[] data = asyncResult.PostData;
            try
            {
                using (Stream stream = asyncResult.Request.EndGetRequestStream(ar))
                {
                    stream.Write(data, 0, data.Length);
                }

                if (asyncResult.IsCompleted)
                {
                    return;
                }

                asyncResult.Request.BeginGetResponse(GetResponseCallback, asyncResult);
            }
            catch (Exception e)
            {
                SetError(asyncResult, e, "Request failed.");
            }
        }
示例#3
0
        private static void SetError(HttpKsiServiceAsyncResult asyncResult, Exception e, string errorMessage)
        {
            string message = errorMessage + string.Format(" (request id: {0}).", asyncResult.RequestId);

            Logger.Warn(message + " " + e);
            asyncResult.Error = new KsiServiceProtocolException(message, e);
            asyncResult.BeginWaitHandle.Set();
        }
示例#4
0
        private static void EndBeginCallback(object state, bool timedOut)
        {
            HttpKsiServiceAsyncResult httpAsyncResult = (HttpKsiServiceAsyncResult)state;

            if (timedOut)
            {
                Logger.Debug("Request timed out (request id: {0})", httpAsyncResult.RequestId);
                httpAsyncResult.Error = new KsiServiceProtocolException("Request timed out.");
            }

            httpAsyncResult.SetComplete();
        }
示例#5
0
        private IAsyncResult BeginRequest(string url, byte[] data, ulong requestId, AsyncCallback callback, object asyncState)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new KsiServiceProtocolException("Service url is missing.");
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            HttpWebRequest request             = null;
            Exception      webRequestException = null;

            try
            {
                request = WebRequest.Create(url) as HttpWebRequest;

                if (request == null)
                {
                    throw new KsiServiceProtocolException("Invalid http web request: null.");
                }

                InitProxySettings(request);
                request.KeepAlive = false;
                request.ServicePoint.Expect100Continue = false;
            }
            catch (Exception e)
            {
                webRequestException = e;
            }

            if (request == null || webRequestException != null)
            {
                string message = "Begin http request failed. Invalid service URL: " + url;
                Logger.Warn(message + Environment.NewLine + webRequestException);
                throw new KsiServiceProtocolException(message, webRequestException);
            }

            request.Method        = WebRequestMethods.Http.Post;
            request.ContentType   = "application/ksi-request";
            request.ContentLength = data.Length;

            HttpKsiServiceAsyncResult httpAsyncResult = new HttpKsiServiceAsyncResult(request, data, requestId, callback, asyncState);

            Logger.Debug("Begin http request (request id: {0}).", httpAsyncResult.RequestId);

            request.BeginGetRequestStream(GetRequestStreamCallback, httpAsyncResult);
            ThreadPool.RegisterWaitForSingleObject(httpAsyncResult.BeginWaitHandle, EndBeginCallback, httpAsyncResult, _requestTimeOut, true);
            return(httpAsyncResult);
        }
示例#6
0
        /// <summary>
        ///     Begin get publications file.
        /// </summary>
        /// <param name="callback">callback when publications file is downloaded</param>
        /// <param name="asyncState">callback async state object</param>
        /// <returns>HTTP KSI service async result</returns>
        public IAsyncResult BeginGetPublicationsFile(AsyncCallback callback, object asyncState)
        {
            HttpWebRequest request             = null;
            Exception      webRequestException = null;

            try
            {
                request = WebRequest.Create(_publicationsFileUrl) as HttpWebRequest;

                if (request == null)
                {
                    throw new KsiServiceProtocolException("Invalid http web request: null.");
                }

                InitProxySettings(request);
                request.KeepAlive = false;
                request.ServicePoint.Expect100Continue = false;
            }
            catch (Exception e)
            {
                webRequestException = e;
            }

            if (request == null || webRequestException != null)
            {
                string message = "Begin get publications file http request failed. Invalid publications file HTTP URL(\"" + _publicationsFileUrl + "\").";
                Logger.Warn(message + " " + webRequestException);
                throw new KsiServiceProtocolException(message, webRequestException);
            }

            request.Method = WebRequestMethods.Http.Get;

            HttpKsiServiceAsyncResult httpAsyncResult = new HttpKsiServiceAsyncResult(request, null, Utils.Util.GetRandomUnsignedLong(), callback, asyncState);

            Logger.Debug("Begin get publications file http request (request id: {0})", httpAsyncResult.RequestId);

            request.BeginGetResponse(GetPublicationResponseCallback, httpAsyncResult);
            ThreadPool.RegisterWaitForSingleObject(httpAsyncResult.BeginWaitHandle, EndBeginCallback, httpAsyncResult, _requestTimeOut, true);

            return(httpAsyncResult);
        }
示例#7
0
        private void HandleWebResponse(WebResponse response, HttpKsiServiceAsyncResult asyncResult)
        {
            byte[] buffer = new byte[_bufferSize];

            try
            {
                using (Stream s = response.GetResponseStream())
                {
                    int bytesLength;
                    while (s != null && (bytesLength = s.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        asyncResult.ResultStream.Write(buffer, 0, bytesLength);
                    }
                }
                Logger.Debug("Handle http web response successful (request id: {0})", asyncResult.RequestId);
            }
            catch (Exception ex)
            {
                SetError(asyncResult, ex, "Handling web response failed.");
            }
        }
示例#8
0
        private void GetPublicationResponseCallback(IAsyncResult ar)
        {
            HttpKsiServiceAsyncResult asyncResult = (HttpKsiServiceAsyncResult)ar.AsyncState;

            if (asyncResult.IsCompleted)
            {
                return;
            }

            try
            {
                using (WebResponse response = asyncResult.Request.EndGetResponse(ar))
                {
                    HandleWebResponse(response, asyncResult);
                    asyncResult.BeginWaitHandle.Set();
                }
            }
            catch (WebException e)
            {
                SetError(asyncResult, e, "Get publication http response failed.");
            }
        }
示例#9
0
        /// <summary>
        ///     End get result from web request.
        /// </summary>
        /// <param name="ar">HTTP KSI service async result</param>
        /// <returns>result bytes</returns>
        private byte[] EndGetResult(IAsyncResult ar)
        {
            HttpKsiServiceAsyncResult asyncResult = ar as HttpKsiServiceAsyncResult;

            if (asyncResult == null)
            {
                throw new KsiServiceProtocolException("Invalid IAsyncResult.");
            }

            if (!asyncResult.IsCompleted)
            {
                asyncResult.AsyncWaitHandle.WaitOne();
            }

            if (asyncResult.HasError)
            {
                throw asyncResult.Error;
            }

            Logger.Debug("HTTP service protocol returning {0} bytes (request id: {1}).", asyncResult.ResultStream.Length, asyncResult.RequestId);

            return(asyncResult.ResultStream.ToArray());
        }