Exemplo n.º 1
0
        /// <summary>
        /// Sends the request.
        /// </summary>
        /// <returns>WebResponseMessage instance.</returns>
        public WebResponseMessage SendRequest()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this._urlBuilder.ToString());

            request.Accept      = this.Accept;
            request.ContentType = this.ContentType;
            request.Method      = this.Method;
            request.UserAgent   = this.UserAgent;

            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();

                WebResponseMessage result = new WebResponseMessage(response);

                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                {
                    result.Content   = streamReader.ReadToEnd();
                    result.Succeeded = true;

                    return(result);
                }
            }
            catch (Exception e)
            {
                WebException webException = e as WebException;

                if (webException != null)
                {
                    HttpWebResponse innerResponse = webException.Response as HttpWebResponse;

                    if (innerResponse != null)
                    {
                        WebResponseMessage result = new WebResponseMessage(innerResponse);

                        result.ErrorMessage = webException.ToString();
                        result.Succeeded    = false;

                        StreamReader streamReader = null;

                        try
                        {
                            streamReader   = new StreamReader(innerResponse.GetResponseStream());
                            result.Content = streamReader.ReadToEnd();
                        }
                        catch
                        {
                        }
                        finally
                        {
                            if (streamReader != null)
                            {
                                streamReader.Dispose();
                                streamReader = null;
                            }
                        }

                        if (innerResponse != null)
                        {
                            innerResponse.Close();
                            innerResponse = null;
                        }

                        return(result);
                    }
                    else
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends the SOAP request.
        /// </summary>
        /// <param name="uri">The URI that identifies the Internet resource.</param>
        /// <param name="soapEnvelopeXml">The SOAP envelope XmlDocument.</param>
        /// <param name="username">The user name.</param>
        /// <param name="password">The password.</param>
        /// <returns>SOAP response.</returns>
        private static WebResponseMessage SendRequest(string uri, XmlDocument soapEnvelopeXml, string username, string password)
        {
            string soapAction = null;

            foreach (XmlNode childNode in soapEnvelopeXml.DocumentElement.ChildNodes)
            {
                if (childNode.LocalName.Equals("Header"))
                {
                    foreach (XmlNode item in childNode.ChildNodes)
                    {
                        if (item.LocalName.Equals("Action"))
                        {
                            soapAction = item.InnerText;
                            childNode.RemoveChild(item);
                            break;
                        }
                    }

                    break;
                }
            }

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.ContentType = HttpHeaderContentTypeValue;
            request.Accept      = HttpHeaderAcceptValue;
            request.Method      = HttpRequestMethodValue;

            if (!string.IsNullOrEmpty(soapAction))
            {
                request.Headers[HttpHeaderSOAPActionKey] = soapAction;
            }

            if (!string.IsNullOrEmpty(username))
            {
                string authorizationInfo = username + ":" + password;
                request.Headers[HttpHeaderAuthorizationKey] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authorizationInfo));
            }

            Stream stream = null;

            try
            {
                stream = request.GetRequestStream();
                soapEnvelopeXml.Save(stream);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                    stream = null;
                }
            }

            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();

                WebResponseMessage result = new WebResponseMessage(response);

                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                {
                    result.Content   = streamReader.ReadToEnd();
                    result.Succeeded = true;

                    return(result);
                }
            }
            catch (Exception e)
            {
                WebException webException = e as WebException;

                if (webException != null)
                {
                    HttpWebResponse innerResponse = webException.Response as HttpWebResponse;

                    if (innerResponse != null)
                    {
                        WebResponseMessage result = new WebResponseMessage(innerResponse);

                        result.ErrorMessage = webException.ToString();
                        result.Succeeded    = false;

                        StreamReader streamReader = null;

                        try
                        {
                            streamReader   = new StreamReader(innerResponse.GetResponseStream());
                            result.Content = streamReader.ReadToEnd();
                        }
                        catch
                        {
                        }
                        finally
                        {
                            if (streamReader != null)
                            {
                                streamReader.Dispose();
                                streamReader = null;
                            }
                        }

                        if (innerResponse != null)
                        {
                            innerResponse.Close();
                            innerResponse = null;
                        }

                        return(result);
                    }
                    else
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }