Example #1
0
        private Boolean TrySend()
        {
            Exception      lastException = null;
            HttpStatusCode status        = HttpStatusCode.BadRequest; // valor inicial antes do envio

            if (listener != null)
            {
                listener.NotifyObject("Enviando requisição...");
            }

            HttpWebRequest  request        = null;
            Stream          requestStream  = null;
            HttpWebResponse response       = null;
            Stream          responseStream = null;

            try
            {
                Byte[] serializedObject = ObjectSerializer.SerializeObjectToArray(requestData);
                String encodedData      = HttpUtility.UrlEncode(Convert.ToBase64String(serializedObject));
                Byte[] postData         = Encoding.UTF8.GetBytes("txtPostData=" + encodedData);

                request        = (HttpWebRequest)WebRequest.Create(serviceUrl + "?action=" + requestAction);
                request.Method = "POST";
                request.ServicePoint.ConnectionLimit = 10;  // define limite para 10 conexões
                request.Timeout       = this.timeout;       // define o timeout
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;
                requestStream         = request.GetRequestStream();
                requestStream.Write(postData, 0, postData.Length);
                response       = (HttpWebResponse)request.GetResponse();
                status         = response.StatusCode;
                responseStream = response.GetResponseStream();
                Byte[] buffer = new Byte[response.ContentLength];
                responseStream.Read(buffer, 0, (int)response.ContentLength);
                responseData = Encoding.UTF8.GetString(buffer);
            }
            catch (Exception exception)
            {
                lastException = exception;
            }
            finally
            {
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (response != null)
                {
                    ((IDisposable)response).Dispose(); response.Close();
                }
                if (requestStream != null)
                {
                    requestStream.Close();
                }
                request = null; // permite que o garbage collector elimine o objeto
            }

            if (lastException != null)
            {
                if (listener != null)
                {
                    listener.NotifyObject(lastException.Message);
                }
                return(false);
            }

            if (status != HttpStatusCode.OK)
            {
                if (listener != null)
                {
                    listener.NotifyObject("Falha no envio. Status = " + status);
                }
                return(false);
            }

            return(true);
        }