Exemplo n.º 1
0
        /// <summary>
        /// Faz a requisição utilizando o método HTTP enviado via parâmetro e retornando o retorno em string
        /// </summary>
        /// <owner>Mnix-Victor</owner>
        private Stream MakeRequest(HttpMethod httpMethod, UploadProgressChangedHandler progressChangedHandler)
        {
            lock (@lock)
            {
                HttpMethodAdapter methodAdapter = HttpMethodAdapterFactory.GetAdapter(httpMethod, this.mParameters, this.UsedEncoding);

                mRunningRequest = (HttpWebRequest)HttpWebRequest.Create(methodAdapter.MountRequestUri(this._mTargetUri));

                if (mIsAborted)
                {
                    this.Abort();
                    this.mIsAborted = false;
                }

                mRunningRequest.Method = httpMethod.ToString();
                mRunningRequest.Timeout = (int)this.Timeout;
                mRunningRequest.ContentType = methodAdapter.MountContentType();

                if (methodAdapter is HttpMethodAdapterFactory.BasePOSTHttpMethodAdapter)
                {
                    this.mProgressChangedHandler = progressChangedHandler;

                    HttpMethodAdapterFactory.BasePOSTHttpMethodAdapter postHttpAdapter = (HttpMethodAdapterFactory.BasePOSTHttpMethodAdapter)methodAdapter;
                    postHttpAdapter.UploadProgressChangedEventHandler = SendProgressChangedNotification;

                    mRunningRequest.AllowWriteStreamBuffering = false;
                    mRunningRequest.ContentLength = postHttpAdapter.CachedContentSize;

                    //Pega o stream de saida para ser escrito na requsição
                    Stream writeableRequestStream = mRunningRequest.GetRequestStream();

                    postHttpAdapter.WriteRequestStream(writeableRequestStream);

                    writeableRequestStream.Close();
                }

                WebResponse response = mRunningRequest.GetResponse();

                Stream responseStream = response.GetResponseStream();

                return responseStream;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executa a requisição de modo assíncrono utilizando o método HTTP enviado e retorna o resultado a partir dos delegates de callback. Esse overload tem suporte a track de porcentagem de upload
        /// </summary>
        /// <param name="method">Método HTTP a ser utilizado para fazer a requisição</param>
        /// <param name="sucessResultWithStreamHandler">Handler chamado quando houver sucesso na requisição</param>
        /// <param name="progressChangedHandler">Handler chamado para notificar o progresso de upload da requisição</param>
        /// <param name="errorResultHandler">Handler chamado quando houver erro na requisição</param>
        /// <owner>Mnix-Victor</owner>
        public void ExecuteAsync(HttpMethod method, SuccessResultHandler successResultHandler, UploadProgressChangedHandler progressChangedHandler, ErrorResultHandler errorResultHandler)
        {
            if (method == HttpMethod.GET && progressChangedHandler != null)
            {
                throw new InvalidHTTPMethodForProgressTrackingException(method);
            }

            this._mIsRequestRunning = true;

            Thread newThread = new Thread(delegate()
            {
                try
                {
                    Stream responseStream = MakeRequest(method, progressChangedHandler);

                    string resultString = MountReturnString(responseStream);
                    responseStream.Close();

                    successResultHandler.Invoke(this, resultString);
                }
                catch (WebException webException)
                {
                    this.DiagnoseRequestErrors(webException, errorResultHandler);
                }
                finally
                {
                    this._mIsRequestRunning = false;
                }
            });
            
            newThread.Start();
        }