/// <summary>
        /// Sends the specified API request.
        /// </summary>
        /// <param name="apiRequest">The API request.</param>
        /// <returns></returns>
        public ANetApiResponse Send(ANetApiRequest apiRequest) {

            //Authenticate it
            AuthenticateRequest(apiRequest);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_serviceUrl);
            webRequest.Method = "POST";
            webRequest.ContentType = "text/xml";
            webRequest.KeepAlive = true;

            // Serialize the request
            var type = apiRequest.GetType();
            var serializer = new XmlSerializer(type);
            XmlWriter writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8);
            serializer.Serialize(writer, apiRequest);
            writer.Close();


            // Get the response
            WebResponse webResponse = webRequest.GetResponse();

            // Load the response from the API server into an XmlDocument.
            _xmlDoc = new XmlDocument();
            _xmlDoc.Load(XmlReader.Create(webResponse.GetResponseStream()));


            var response = DecideResponse(_xmlDoc);
            CheckForErrors(response);
            return response;
        }
        /// <summary>
        /// Sends the specified API request.
        /// </summary>
        /// <param name="apiRequest">The API request.</param>
        /// <returns></returns>
        public ANetApiResponse Send(ANetApiRequest apiRequest) {

            //Authenticate it
            AuthenticateRequest(apiRequest);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_serviceUrl);
            webRequest.Method = "POST";
            webRequest.ContentType = "text/xml";
            webRequest.KeepAlive = true;

            //set the http connection timeout 
            var httpConnectionTimeout = AuthorizeNet.Environment.getIntProperty(Constants.HttpConnectionTimeout);
            webRequest.Timeout = (httpConnectionTimeout != 0 ? httpConnectionTimeout : Constants.HttpConnectionDefaultTimeout);

            //set the time out to read/write from stream
            var httpReadWriteTimeout = AuthorizeNet.Environment.getIntProperty(Constants.HttpReadWriteTimeout);
            webRequest.ReadWriteTimeout = (httpReadWriteTimeout != 0 ? httpReadWriteTimeout : Constants.HttpReadWriteDefaultTimeout);

            // Serialize the request
            var type = apiRequest.GetType();
            var serializer = new XmlSerializer(type);
            XmlWriter writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8);
            serializer.Serialize(writer, apiRequest);
            writer.Close();


            // Get the response
            WebResponse webResponse = webRequest.GetResponse();

            // Load the response from the API server into an XmlDocument.
            _xmlDoc = new XmlDocument();
            _xmlDoc.Load(XmlReader.Create(webResponse.GetResponseStream()));


            var response = DecideResponse(_xmlDoc);
            CheckForErrors(response);
            return response;
        }
 /// <summary>
 /// Adds authentication information to the request.
 /// </summary>
 /// <param name="request">The request.</param>
 void AuthenticateRequest(ANetApiRequest request) {
     request.merchantAuthentication = new merchantAuthenticationType();
     request.merchantAuthentication.name = _apiLogin;
     request.merchantAuthentication.Item = _transactionKey;
     request.merchantAuthentication.ItemElementName = ItemChoiceType.transactionKey;
 }
 /// <summary>
 /// Adds authentication information to the request.
 /// </summary>
 /// <param name="request">The request.</param>
 void AuthenticateRequest(ANetApiRequest request)
 {
     request.merchantAuthentication = new merchantAuthenticationType();
     request.merchantAuthentication.name = _apiLogin;
     request.merchantAuthentication.transactionKey = _transactionKey;
 }