USPS Rating

 

To perform USPS rating, you have to set basic information related to USPS shipper. First of all, make an instance of USPSRateRequest and set needed data for authorization.

 

USPSRateRequest uspsRateRequest = new USPSRateRequest();

uspsRateRequest.Password = "password";

uspsRateRequest.UserId = "user id";

 

Set required information about package. The most important information is shown below. Top level variables include: service, sender address and destination address. Create an USPS package and set needed information concerning this shipment.

 

USPSPackage package = new USPSPackage();

package.Pounds = 10; 

package.Container = USPSPackageName.Oversize; 

package.Service = USPSServiceCode.All; 

package.ZipOrigination = "33131";

package.ZipDestination = "99501";

package.Size = USPSPackageSize.Regular; 

package.Machinable = USPSBool.True; 

 

Then add package to request object. 

 

uspsRateRequest.Packages.Add(package); 

 

Now send request. Create rate provider object and call Check(...) method. 

 

USPSRateProvider rate = new USPSRateProvider();

USPSRateResponse response = rate.Check(uspsRateRequest, false);

 

Now you can go through each package in response object and get any needed information. For example, what you need are only postage costs.

 

foreach (USPSRatedPackage uspsPackage in response.Packages)

    if (uspsPackage.Error != null)

    {

        string errorDescription = uspsPackage.Error.Description;

        string errorNumber = uspsPackage.Error.Number;

    }

    else

    {

        foreach (USPSPostage postage in uspsPackage.Postages)

        {

            decimal cost = postage.Rate;

        }

    }