﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace Acquirer.Core
{
    public class CustomHttpRequest
    {
        public static string Request(string xml, string url)
        {
            try
            {
                var request = WebRequest.Create(url);
                request.Method = WebRequestMethods.Http.Post;
                request.ContentType = "application/x-www-form-urlencoded";

                var postData = String.Format("mensagem={0}", xml);
                var byteBuffer = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteBuffer.Length;

                var requestStream = request.GetRequestStream();
                requestStream.Write(byteBuffer, 0, byteBuffer.Length);

                requestStream.Close();

                var httpWebResponse = (HttpWebResponse)request.GetResponse();

                var streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.Default);
                var dados = streamReader.ReadToEnd();

                return dados;
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }
    }
}
