c# - UPS API - LabelLinksIndicator - 未设置对象实例的对象引用

标签 c# web-services ups

下面是一些用于生成 UPS 运输标签的代码。它基于 UPS 开发工具包中提供的 .net 示例。我的问题是下面以粗体突出显示的行。它抛出错误,如下所示“System.NullReferenceException:对象引用未设置到对象的实例。”。

我所知道的是,如果您希望 UPS API 返回指向标签的链接,则此 LabelLinksIndicator 是必需的。否则它会返回 cargo 的详细信息但没有标签。 UPS 文件的相关部分在我的问题底部。

我需要做什么来克服这个错误?从文档中我不清楚我应该为 LabelLinksIndicator 传递什么值。我试过传递一个 1、true 和一个空字符串。在每种情况下都出现相同的错误。错误信息似乎表明有一个对象我需要实例化我当前不是。但是,我不知道要实例化什么。遗憾的是,网络和 UPS.com 上的可用信息很少。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Foo.Web.UPSWebReference;


namespace Foo.Web.Auth
{
    public partial class UPS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                try
                {
                    ShipService shpSvc = new ShipService();
                    ShipmentRequest shipmentRequest = new ShipmentRequest();
                    UPSSecurity upss = new UPSSecurity();
                    UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
                    upssSvcAccessToken.AccessLicenseNumber = "foo";
                    upss.ServiceAccessToken = upssSvcAccessToken;
                    UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
                    upssUsrNameToken.Username = "foo";
                    upssUsrNameToken.Password = "foo";
                    upss.UsernameToken = upssUsrNameToken;
                    shpSvc.UPSSecurityValue = upss;
                    RequestType request = new RequestType();
                    String[] requestOption = { "nonvalidate" };
                    request.RequestOption = requestOption;
                    shipmentRequest.Request = request;
                    ShipmentType shipment = new ShipmentType();
                    shipment.Description = "Ship webservice example";
                    ReturnServiceType rtn = new ReturnServiceType();
                    rtn.Code = "8";
                    rtn.Description = "Description";

                    ShipperType shipper = new ShipperType();
                    shipper.ShipperNumber = "foo";
                    PaymentInfoType paymentInfo = new PaymentInfoType();
                    ShipmentChargeType shpmentCharge = new ShipmentChargeType();
                    BillShipperType billShipper = new BillShipperType();
                    billShipper.AccountNumber = "foo";
                    shpmentCharge.BillShipper = billShipper;
                    shpmentCharge.Type = "01";
                    ShipmentChargeType[] shpmentChargeArray = { shpmentCharge };
                    paymentInfo.ShipmentCharge = shpmentChargeArray;
                    shipment.PaymentInformation = paymentInfo;
                    foo.Web.UPSWebReference.ShipAddressType shipperAddress = new foo.Web.UPSWebReference.ShipAddressType();
                    String[] addressLine = { "301 166th Street" };
                    shipperAddress.AddressLine = addressLine;
                    shipperAddress.City = "Jersey City";
                    shipperAddress.PostalCode = "07310";
                    shipperAddress.StateProvinceCode = "NJ";
                    shipperAddress.CountryCode = "US";
                    shipperAddress.AddressLine = addressLine;
                    shipper.Address = shipperAddress;
                    shipper.Name = "ABC Associates";
                    shipper.AttentionName = "ABC Associates";
                    ShipPhoneType shipperPhone = new ShipPhoneType();
                    shipperPhone.Number = "1234567890";
                    shipper.Phone = shipperPhone;
                    shipment.Shipper = shipper;
                    ShipFromType shipFrom = new ShipFromType();
                    foo.Web.UPSWebReference.ShipAddressType shipFromAddress = new foo.Web.UPSWebReference.ShipAddressType();
                    String[] shipFromAddressLine = { "100 82nd Street" };
                    shipFromAddress.AddressLine = addressLine;
                    shipFromAddress.City = "New York";
                    shipFromAddress.PostalCode = "10024";
                    shipFromAddress.StateProvinceCode = "NY";
                    shipFromAddress.CountryCode = "US";
                    shipFrom.Address = shipFromAddress;
                    shipFrom.AttentionName = "Mr.ABC";
                    shipFrom.Name = "ABC Associates";
                    shipment.ShipFrom = shipFrom;
                    ShipToType shipTo = new ShipToType();
                    ShipToAddressType shipToAddress = new ShipToAddressType();
                    String[] addressLine1 = { "Some Street" };
                    shipToAddress.AddressLine = addressLine1;
                    shipToAddress.City = "Roswell";
                    shipToAddress.PostalCode = "30076";
                    shipToAddress.StateProvinceCode = "GA";
                    shipToAddress.CountryCode = "US";
                    shipTo.Address = shipToAddress;
                    shipTo.Address.ResidentialAddressIndicator = "1"; //dan
                    shipTo.AttentionName = "DEF";
                    shipTo.Name = "DEF Associates";
                    ShipPhoneType shipToPhone = new ShipPhoneType();
                    shipToPhone.Number = "1234567890";
                    shipTo.Phone = shipToPhone;
                    shipment.ShipTo = shipTo;
                    ServiceType service = new ServiceType();
                    service.Code = "03"; 
                    service.Description = "Ground";
                    shipment.Service = service;
                    PackageType package = new PackageType();
                    package.Description = "Deliver to Warehouse";
                    PackageWeightType packageWeight = new PackageWeightType();
                    packageWeight.Weight = "10";
                    ShipUnitOfMeasurementType uom = new ShipUnitOfMeasurementType();
                    uom.Code = "LBS";
                    packageWeight.UnitOfMeasurement = uom;
                    package.PackageWeight = packageWeight;
                    PackagingType packType = new PackagingType();
                    packType.Code = "02";
                    package.Packaging = packType;

                    PackageType[] pkgArray = { package };
                    shipment.Package = pkgArray;
                    LabelSpecificationType labelSpec = new LabelSpecificationType();

                    LabelImageFormatType labelImageFormat = new LabelImageFormatType();
                    labelImageFormat.Code = "GIF";
                    labelSpec.LabelImageFormat = labelImageFormat;
                    string userAgent = Request.UserAgent; 
                    labelSpec.HTTPUserAgent = userAgent; 
                    shipmentRequest.LabelSpecification = labelSpec;

                    **shipmentRequest.Shipment.ShipmentServiceOptions.LabelDelivery.LabelLinksIndicator = "1";**

                    shipmentRequest.Shipment = shipment;


                    System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

                    ShipmentResponse shipmentResponse = shpSvc.ProcessShipment(shipmentRequest);



                    Response.Redirect(shipmentResponse.ShipmentResults.LabelURL.ToString());

enter image description here

最佳答案

我想出了问题所在。我首先需要新建一个实例

ShipmentTypeShipmentServiceOptions shipServOpt = new ShipmentTypeServiceOptions();

LabelDeliveryType labelDel = new LabelDeliveryType();

然后设置 LabelLinksIndicator 元素

labeldel.LabelLinksIndicator = "";

然后将选项分配给我的装运实例

shipment.ShipmentServiceOptions = shipServOpt;

请注意,ShipmentServiceOptionsType 与 ShipmentTypeServiceOptions 不同。这把我绊倒了一段时间。

亲爱的 UPS,如果您正在阅读本文,请考虑改进您的文档并提供一组更完整的代码示例。

关于c# - UPS API - LabelLinksIndicator - 未设置对象实例的对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7153524/

相关文章:

web-services - 语音识别(网络)服务?

java - CXF + wsdl2java + 认证

c# - (PartialView) 传入字典的模型项是 'Customer' 类型,但是这个字典需要一个 'UserProfile' 类型的模型项

c# - ASP.NET TextRenderer.DrawText 糟糕的文本图像

c# - 什么时候去对象池?

node.js - 帐号缺失或无效 UPS 运输 API 错误代码 (9500523)

linux - 读取 USB UPS 状态的 Bash 脚本

c# - Automapper 在 Profile 类中注入(inject) DbContext

mysql - 如何分离这两个过程?

java - 解码 UPS Maxicode