Example #1
0
        public ValidateResult Validate(Address address)
        {
            // Convert input address data to query string
            string querystring = string.Empty;
            querystring += (address.Line1 == null) ? string.Empty : "Line1=" + address.Line1.Replace(" ", "+");
            querystring += (address.Line2 == null) ? string.Empty : "&Line2=" + address.Line2.Replace(" ", "+");
            querystring += (address.Line3 == null) ? string.Empty : "&Line3=" + address.Line3.Replace(" ", "+");
            querystring += (address.City == null) ? string.Empty : "&City=" + address.City.Replace(" ", "+");
            querystring += (address.Region == null) ? string.Empty : "&Region=" + address.Region.Replace(" ", "+");
            querystring += (address.PostalCode == null) ? string.Empty : "&PostalCode=" + address.PostalCode.Replace(" ", "+");
            querystring += (address.Country == null) ? string.Empty : "&Country=" + address.Country.Replace(" ", "+");

            // Call the service
            Uri webAddress = new Uri(svcURL + "address/validate.xml?" + querystring);
            HttpWebRequest request = WebRequest.Create(webAddress) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(accountNum + ":" + license)));
            request.Method = "GET";

            ValidateResult result = new ValidateResult();
            try
            {
                WebResponse response = request.GetResponse();
                XmlSerializer r = new XmlSerializer(result.GetType());
                result = (ValidateResult)r.Deserialize(response.GetResponseStream());
                address = result.Address; // If the address was validated, take the validated address.
            }
            catch (WebException ex)
            {
                Stream responseStream = ((HttpWebResponse)ex.Response).GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                string responseString = reader.ReadToEnd();

                // The service returns some error messages in JSON for authentication/unhandled errors.
                if (responseString.StartsWith("{")  || responseString.StartsWith("["))
                {
                    result = new ValidateResult();
                    result.ResultCode = SeverityLevel.Error;
                    Message msg = new Message();
                    msg.Severity = result.ResultCode;
                    msg.Summary = "The request was unable to be successfully serviced, please try again or contact Customer Service.";
                    msg.Source = "Avalara.Web.REST";
                    if (!((HttpWebResponse)ex.Response).StatusCode.Equals(HttpStatusCode.InternalServerError))
                    {
                        msg.Summary = "The user or account could not be authenticated.";
                        msg.Source = "Avalara.Web.Authorization"; 
                    }

                    result.Messages = new Message[1] { msg };
                }
                else
                {
                    XmlSerializer r = new XmlSerializer(result.GetType());
                    byte[] temp = Encoding.ASCII.GetBytes(responseString);
                    MemoryStream stream = new MemoryStream(temp);
                    result = (ValidateResult)r.Deserialize(stream); // Inelegant, but the deserializer only takes streams, and we already read ours out.
                }
            }

            return result;
        }
        public static ValidateResult Validate(Address addr, string AcctNum, string LicKey, string CompanyCode, string webaddr)
        {
            //Convert input address data to query string
            string querystring = "";

            if (addr.Line1 != null)
            {
                querystring = querystring + "Line1=" + addr.Line1.Replace(" ", "+");
            }
            if (addr.Line2 != null)
            {
                querystring = querystring + "&Line2=" + addr.Line2.Replace(" ", "+");
            }
            if (addr.Line3 != null)
            {
                querystring = querystring + "&Line3=" + addr.Line3.Replace(" ", "+");
            }
            if (addr.City != null)
            {
                querystring = querystring + "&City=" + addr.City.Replace(" ", "+");
            }
            if (addr.Region != null)
            {
                querystring = querystring + "&Region=" + addr.Region.Replace(" ", "+");
            }
            if (addr.PostalCode != null)
            {
                querystring = querystring + "&PostalCode=" + addr.PostalCode.Replace(" ", "+");
            }
            if (addr.Country != null)
            {
                querystring = querystring + "&Country=" + addr.Country.Replace(" ", "+");
            }


            //Call the service
            Uri            address = new Uri(webaddr + "address/validate.xml?" + querystring);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(AcctNum + ":" + LicKey)));
            request.Method = "GET";

            ValidateResult result = new ValidateResult();

            try
            {
                WebResponse   response = request.GetResponse();
                XmlSerializer r        = new XmlSerializer(result.GetType());
                result = (ValidateResult)r.Deserialize(response.GetResponseStream());
                addr   = result.Address; //If the address was validated, take the validated address.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " on address object");
            }
            Console.WriteLine(result.ResultCode.ToString());
            return(result);
        }
        public static ValidateResult Validate(Address addr, string AcctNum, string LicKey, string CompanyCode, string webaddr)
        {
            //Convert input address data to query string
            string querystring = "";
            if (addr.Line1 != null) { querystring = querystring + "Line1=" + addr.Line1.Replace(" ", "+"); }
            if (addr.Line2 != null) { querystring = querystring + "&Line2=" + addr.Line2.Replace(" ", "+"); }
            if (addr.Line3 != null) { querystring = querystring + "&Line3=" + addr.Line3.Replace(" ", "+"); }
            if (addr.City != null) { querystring = querystring + "&City=" + addr.City.Replace(" ", "+"); }
            if (addr.Region != null) { querystring = querystring + "&Region=" + addr.Region.Replace(" ", "+"); }
            if (addr.PostalCode != null) { querystring = querystring + "&PostalCode=" + addr.PostalCode.Replace(" ", "+"); }
            if (addr.Country != null) { querystring = querystring + "&Country=" + addr.Country.Replace(" ", "+"); }

            //Call the service
            Uri address = new Uri(webaddr + "address/validate.xml?" + querystring);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(AcctNum + ":" + LicKey)));
            request.Method = "GET";

            ValidateResult result = new ValidateResult();
            try
            {
                WebResponse response = request.GetResponse();
                XmlSerializer r = new XmlSerializer(result.GetType());
                result = (ValidateResult)r.Deserialize(response.GetResponseStream());
                addr = result.Address; //If the address was validated, take the validated address.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " on address object");
            }
            Console.WriteLine(result.ResultCode.ToString());
            return result;
        }
        public static ValidateResult Validate(Address addr, string AcctNum, string LicKey, string webaddr)
        {
            //Convert input address data to query string
            string querystring = "";

            if (addr.Line1 != null)
            {
                querystring = querystring + "Line1=" + addr.Line1.Replace(" ", "+");
            }
            if (addr.Line2 != null)
            {
                querystring = querystring + "&Line2=" + addr.Line2.Replace(" ", "+");
            }
            if (addr.Line3 != null)
            {
                querystring = querystring + "&Line3=" + addr.Line3.Replace(" ", "+");
            }
            if (addr.City != null)
            {
                querystring = querystring + "&City=" + addr.City.Replace(" ", "+");
            }
            if (addr.Region != null)
            {
                querystring = querystring + "&Region=" + addr.Region.Replace(" ", "+");
            }
            if (addr.PostalCode != null)
            {
                querystring = querystring + "&PostalCode=" + addr.PostalCode.Replace(" ", "+");
            }
            if (addr.Country != null)
            {
                querystring = querystring + "&Country=" + addr.Country.Replace(" ", "+");
            }


            //Call the service
            Uri            address = new Uri(webaddr + "address/validate.xml?" + querystring);
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(AcctNum + ":" + LicKey)));
            request.Method = "GET";

            ValidateResult result = new ValidateResult();

            try
            {
                WebResponse   response = request.GetResponse();
                XmlSerializer r        = new XmlSerializer(result.GetType());
                result = (ValidateResult)r.Deserialize(response.GetResponseStream());
                addr   = result.Address; //If the address was validated, take the validated address.
            }
            catch (WebException ex)
            {
                Stream       responseStream = ((HttpWebResponse)ex.Response).GetResponseStream();
                StreamReader reader         = new StreamReader(responseStream);
                String       responseString = reader.ReadToEnd();
                if (responseString.StartsWith("{") || responseString.StartsWith("["))  //The service returns some error messages in JSON for authentication/unhandled errors.
                {
                    result            = new ValidateResult();
                    result.ResultCode = SeverityLevel.Error;
                    Message msg = new Message();
                    msg.Severity = result.ResultCode;
                    msg.Summary  = "The request was unable to be successfully serviced, please try again or contact Customer Service.";
                    msg.Source   = "Avalara.Web.REST";
                    if (!((HttpWebResponse)ex.Response).StatusCode.Equals(HttpStatusCode.InternalServerError))
                    {
                        msg.Summary = "The user or account could not be authenticated.";
                        msg.Source  = "Avalara.Web.Authorization";
                    }
                    result.Messages = new Message[1] {
                        msg
                    };
                }
                else
                {
                    XmlSerializer r      = new XmlSerializer(result.GetType());
                    byte[]        temp   = Encoding.ASCII.GetBytes(responseString);
                    MemoryStream  stream = new MemoryStream(temp);
                    result = (ValidateResult)r.Deserialize(stream); //Inelegant, but the deserializer only takes streams, and we already read ours out.
                }
            }
            return(result);
        }