예제 #1
0
        public RateResponse GetRate(Package package)
        {
            try
            {
                string url = "?API=RateV2&XML=<RateV2Request USERID=\"{0}\"><Package ID=\"0\"><Service>{1}</Service><ZipOrigination>{2}</ZipOrigination><ZipDestination>{3}</ZipDestination><Pounds>{4}</Pounds><Ounces>{5}</Ounces><Container>{6}</Container><Size>{7}</Size></Package></RateV2Request>";

                int    lb        = package.WeightInOunces / 16;
                int    oz        = package.WeightInOunces % 16;
                string container = package.PackageType.ToString().Replace("_", " ");
                if (container == "None")
                {
                    url = url.Replace("<Container>{6}</Container>", "");
                }
                string fromZip = package.FromAddress.Zip;
                if (package.OriginZipcode != null && package.OriginZipcode.Length > 0)
                {
                    fromZip = package.OriginZipcode;
                }


                url = GetURL() + url;
                url = String.Format(url, _userid, package.ServiceType.ToString(), fromZip, package.ToAddress.Zip, lb.ToString(), oz.ToString(), container, package.PackageSize.ToString().Replace("_", " "));
                string xml = web.DownloadString(url);
                if (xml.Contains("<Error>"))
                {
                    int    idx1    = xml.IndexOf("<Description>") + 13;
                    int    idx2    = xml.IndexOf("</Description>");
                    int    l       = xml.Length;
                    string errDesc = xml.Substring(idx1, idx2 - idx1);
                    throw new USPSManagerException(errDesc);
                }

                return(RateResponse.FromXml(xml));
            }
            catch (WebException ex)
            {
                throw new USPSManagerException(ex);
            }
        }
예제 #2
0
        public static RateResponse FromXml(string xml)
        {
            RateResponse r    = new RateResponse();
            int          idx1 = 0;
            int          idx2 = 0;

            if (xml.Contains("<ZipOrigination>"))
            {
                idx1        = xml.IndexOf("<ZipOrigination>") + 17;
                idx2        = xml.IndexOf("</ZipOrigination>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<ZipDestination>"))
            {
                idx1        = xml.IndexOf("<ZipDestination>") + 16;
                idx2        = xml.IndexOf("</ZipDestination>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Pounds>"))
            {
                idx1        = xml.IndexOf("<Pounds>") + 8;
                idx2        = xml.IndexOf("</Pounds>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Ounces>"))
            {
                idx1        = xml.IndexOf("<Ounces>") + 8;
                idx2        = xml.IndexOf("</Ounces>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Container>"))
            {
                idx1        = xml.IndexOf("<Container>") + 11;
                idx2        = xml.IndexOf("</Container>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Size>"))
            {
                idx1        = xml.IndexOf("<Size>") + 6;
                idx2        = xml.IndexOf("</Size>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            if (xml.Contains("<Zone>"))
            {
                idx1        = xml.IndexOf("<Zone>") + 6;
                idx2        = xml.IndexOf("</Zone>");
                r.OriginZip = xml.Substring(idx1, idx2 - idx1);
            }

            int lastidx = 0;

            while (xml.IndexOf("<MailService>", lastidx) > -1)
            {
                Postage p = new Postage();
                idx1          = xml.IndexOf("<MailService>", lastidx) + 13;
                idx2          = xml.IndexOf("</MailService>", lastidx + 13);
                p.MailService = xml.Substring(idx1, idx2 - idx1);

                idx1     = xml.IndexOf("<Rate>", lastidx) + 6;
                idx2     = xml.IndexOf("</Rate>", lastidx + 13);
                p.Amount = Decimal.Parse(xml.Substring(idx1, idx2 - idx1));
                r.Postage.Add(p);
                lastidx = idx2;
            }
            return(r);
        }