我正在尝试实现UPSP
标签生成,但我收到此错误。
**API Authorization failure. DelivConfirmCertifyV3.0Request is not a valid API name for this protocol.**
如果我在浏览器上测试,它工作正常
http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=%3CAddressValidateRequest%20USERID=%22Testuserid%22%3E%3CAddress%20ID=%221%22%3E%3CAddress1%3E%3C/Address1%3E%3CAddress2%3E10051+Orr+%26amp%3b+Day+Rd%3C/Address2%3E%3CCity%3Esanta+fe+springs%3C/City%3E%3CState%3Eca%3C/State%3E%3CZip5%3E90670%3C/Zip5%3E%3CZip4%3E%3C/Zip4%3E%3C/Address%3E%3C/AddressValidateRequest%3E
任何人都知道这个问题...
代码如下:
public Package GetDeliveryConfirmationLabel(Package package)
{
string labeldate = package.ShipDate.ToShortDateString();
if (package.ShipDate.ToShortDateString() == DateTime.Now.ToShortDateString())
labeldate = "";
string url = "?API=DelivConfirmCertifyV3.0Request&XML=<DelivConfirmCertifyV3.0Request.0Request USERID=\"{0}\"><Option>{1}</Option><ImageParameters></ImageParameters><FromName>{2}</FromName><FromFirm>{3}</FromFirm><FromAddress1>{4}</FromAddress1><FromAddress2>{5}</FromAddress2><FromCity>{6}</FromCity><FromState>{7}</FromState><FromZip5>{8}</FromZip5><FromZip4>{9}</FromZip4><ToName>{10}</ToName><ToFirm>{11}</ToFirm><ToAddress1>{12}</ToAddress1><ToAddress2>{13}</ToAddress2><ToCity>{14}</ToCity><ToState>{15}</ToState><ToZip5>{16}</ToZip5><ToZip4>{17}</ToZip4><WeightInOunces>{18}</WeightInOunces><ServiceType>{19}</ServiceType><POZipCode>{20}</POZipCode><ImageType>{21}</ImageType><LabelDate>{22}</LabelDate><CustomerRefNo>{23}</CustomerRefNo><AddressServiceRequested>{24}</AddressServiceRequested><SenderName>{25}</SenderName><SenderEMail>{26}</SenderEMail><RecipientName>{27}</RecipientName><RecipientEMail>{28}</RecipientEMail></DelivConfirmCertifyV3.0Request.0Request>";
url = GetURL() + url;
//url = String.Format(url,this._userid, (int)package.LabelType, package.FromAddress.Contact, package.FromAddress.FirmName, package.FromAddress.Address1, package.FromAddress.Address2, package.FromAddress.City, package.FromAddress.State, package.FromAddress.Zip, package.FromAddress.ZipPlus4, package.ToAddress.Contact, package.ToAddress.FirmName, package.ToAddress.Address1, package.ToAddress.Address2, package.ToAddress.City, package.ToAddress.State, package.ToAddress.Zip, package.ToAddress.ZipPlus4, package.WeightInOunces.ToString(), package.ServiceType.ToString().Replace("_", " "), package.OriginZipcode, package.LabelImageType.ToString(), labeldate, package.ReferenceNumber, package.AddressServiceRequested.ToString(), package.FromAddress.Contact, package.FromAddress.ContactEmail, package.ToAddress.Contact, package.ToAddress.ContactEmail);
url = String.Format(url, this._userid, (int)package.LabelType, package.FromAddress.Contact, package.FromAddress.FirmName, package.FromAddress.Address1, package.FromAddress.Address2, package.FromAddress.City, package.FromAddress.State, package.FromAddress.Zip, package.FromAddress.ZipPlus4, package.ToAddress.Contact, package.ToAddress.FirmName, package.ToAddress.Address1, package.ToAddress.Address2, package.ToAddress.City, package.ToAddress.State, package.ToAddress.Zip, package.ToAddress.ZipPlus4, package.WeightInOunces.ToString(), package.ServiceType.ToString().Replace("_", " "), package.OriginZipcode, package.LabelImageType.ToString(), labeldate, package.ReferenceNumber, package.AddressServiceRequested.ToString(), "", "", "", "");
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);
}
int i1 = xml.IndexOf("<DeliveryConfirmationLabel>") + 27;
int i2 = xml.IndexOf("</DeliveryConfirmationLabel>");
package.ShippingLabel = StringToUTF8ByteArray(xml.Substring(i1, i2 - i1));
return package;
}
我在
上收到错误int i1 = xml.IndexOf("<DeliveryConfirmationLabel>") + 27;
答案 0 :(得分:0)
此问题的解决方案在此stackoverflow问题上是相同的 - How to remove 'SAMPLE DO NOT MAIL' from USPS shipping API image
您可以从我对该问题的回答或直接关注此链接找到完成此步骤的步骤 - https://stackoverflow.com/a/27936025/3748701
USPS返回Base64字符串中的标签,您需要将其转换为图像。在我的应用程序中,我被要求提供图像作为可下载文件,所以我从控制器动作返回了文件结果。下面是从DB获取Base64字符串标签,将其转换为图像并提供可下载文件的操作 -
public ActionResult GetReturnShippingLabel(int orderId, bool showFull)
{
string shippingLabel = new OrderRepository().GetOrderReturnShippingLabel(orderId);
if (!string.IsNullOrEmpty(shippingLabel))
{
byte[] bytes = Convert.FromBase64String(shippingLabel);
Image image = null;
MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
ms.Write(bytes, 0, bytes.Length);
image = Image.FromStream(ms, true);
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
if (!showFull)
image = image.GetThumbnailImage(326, 570, null, IntPtr.Zero);
ImageConverter converter = new ImageConverter();
byte[] imgArray = (byte[])converter.ConvertTo(image, typeof(byte[]));
return File(imgArray.ToArray(), "image/gif");
}
else
{
return null;
}
}