예제 #1
0
        /// <summary>
        /// Get the city and state by proving the zip code.
        /// </summary>
        /// <param name="address">Address object</param>
        /// <returns>Address Object</returns>
        public Address GetCityState(Address address)
        {
            try
            {
                //The address must contain a city and state
                if (address.Zip == null || address.Zip.Length < 1)
                {
                    throw new USPSManagerException("You must supply a zipcode for a city/state lookup request.");
                }

                string citystateurl = "?API=CityStateLookup&XML=<CityStateLookupRequest USERID=\"{0}\"><ZipCode ID= \"{1}\"><Zip5>{2}</Zip5></ZipCode></CityStateLookupRequest>";
                string url          = GetURL() + citystateurl;
                url = String.Format(url, _userid, address.ID.ToString(), address.Zip);
                string addressxml = web.DownloadString(url);
                if (addressxml.Contains("<Error>"))
                {
                    int    idx1    = addressxml.IndexOf("<Description>") + 13;
                    int    idx2    = addressxml.IndexOf("</Description>");
                    int    l       = addressxml.Length;
                    string errDesc = addressxml.Substring(idx1, idx2 - idx1);
                    throw new USPSManagerException(errDesc);
                }

                return(Address.FromXml(addressxml));
            }
            catch (WebException ex)
            {
                throw new USPSManagerException(ex);
            }
        }
예제 #2
0
        /// <summary>
        /// Get the zip code by providing an Address object with a city and state
        /// </summary>
        /// <param name="address">Address Object</param>
        /// <returns>Address Object</returns>
        public Address GetZipcode(Address address)
        {
            try
            {
                //The address must contain a city and state
                if (address.City == null || address.City.Length < 1 || address.State == null || address.State.Length < 1)
                {
                    throw new USPSManagerException("You must supply a city and state for a zipcode lookup request.");
                }

                string zipcodeurl = "?API=ZipCodeLookup&XML=<ZipCodeLookupRequest USERID=\"{0}\"><Address ID=\"{1}\"><Address1>{2}</Address1><Address2>{3}</Address2><City>{4}</City><State>{5}</State></Address></ZipCodeLookupRequest>";
                string url        = GetURL() + zipcodeurl;
                url = String.Format(url, _userid, address.ID.ToString(), address.Address1, address.Address2, address.City, address.State, address.Zip, address.ZipPlus4);
                string addressxml = web.DownloadString(url);
                if (addressxml.Contains("<Error>"))
                {
                    int    idx1    = addressxml.IndexOf("<Description>") + 13;
                    int    idx2    = addressxml.IndexOf("</Description>");
                    int    l       = addressxml.Length;
                    string errDesc = addressxml.Substring(idx1, idx2 - idx1);
                    throw new USPSManagerException(errDesc);
                }

                return(Address.FromXml(addressxml));
            }
            catch (WebException ex)
            {
                throw new USPSManagerException(ex);
            }
        }
예제 #3
0
        /// <summary>
        /// Validate a single address
        /// </summary>
        /// <param name="address">Address object to be validated</param>
        /// <returns>Validated Address</returns>
        public Address ValidateAddress(Address address)
        {
            try
            {
                string validateUrl = "?API=Verify&XML=<AddressValidateRequest USERID=\"{0}\"><Address ID=\"{1}\"><Address1>{2}</Address1><Address2>{3}</Address2><City>{4}</City><State>{5}</State><Zip5>{6}</Zip5><Zip4>{7}</Zip4></Address></AddressValidateRequest>";
                string url         = GetURL() + validateUrl;
                url = String.Format(url, _userid, address.ID.ToString(), address.Address1, address.Address2, address.City, address.State, address.Zip, address.ZipPlus4);
                string addressxml = web.DownloadString(url);
                if (addressxml.Contains("<Error>"))
                {
                    int    idx1    = addressxml.IndexOf("<Description>") + 13;
                    int    idx2    = addressxml.IndexOf("</Description>");
                    int    l       = addressxml.Length;
                    string errDesc = addressxml.Substring(idx1, idx2 - idx1);
                    throw new USPSManagerException(errDesc);
                }

                return(Address.FromXml(addressxml));
            }
            catch (WebException ex)
            {
                throw new USPSManagerException(ex);
            }
        }