不是有效的Base-64字符串,因为它包含非基本64字符或非法字符

时间:2017-08-31 11:20:43

标签: c# xml base64

我的字符串xml如下我没有放置完整的字符串,因为它太长了

目前我的代码是

    public Package GetDeliveryConfirmationLabel(Package package)
    {
        string labeldate = package.ShipDate.ToShortDateString();
        if (package.ShipDate.ToShortDateString() == DateTime.Now.ToShortDateString()) 
            labeldate = "";
           string url= "https://secure.shippingapis.com/ShippingAPI.dll?API=PriorityMailIntlCertify&XML= <PriorityMailIntlCertifyRequest USERID=\"XXXXX\"> <Option></Option> <Revision>2</Revision> <ImageParameters> <ImageParameter>4X6LABEL</ImageParameter> </ImageParameters> <FromFirstName>Garth</FromFirstName> <FromMiddleInitial>A</FromMiddleInitial> <FromLastName>Brooks</FromLastName> <FromFirm>Garths Firm</FromFirm> <FromAddress1>radlab</FromAddress1> <FromAddress2>6406 Ivy Lane</FromAddress2> <FromUrbanization>Garys Urbanization</FromUrbanization> <FromCity>Greenbelt</FromCity> <FromState>MD</FromState> <FromZip5>20770</FromZip5> <FromZip4>1234</FromZip4> <FromPhone>3019187658</FromPhone> <FromCustomsReference> From Customs Ref.</FromCustomsReference> <ToName></ToName> <ToFirstName>Reza</ToFirstName> <ToLastName>Dianat</ToLastName> <ToFirm>HP</ToFirm> <ToAddress1>HP</ToAddress1> <ToAddress2>5th floor</ToAddress2> <ToAddress3>6406 Flower Lane</ToAddress3> <ToCity>Greenbelt</ToCity> <ToProvince>Md</ToProvince> <ToCountry>Canada</ToCountry> <ToPostalCode>20770</ToPostalCode> <ToPOBoxFlag>N</ToPOBoxFlag> <ToPhone>5555555555</ToPhone> <ToFax>3012929999</ToFax> <ToEmail>b@aol.com</ToEmail> <ToCustomsReference>Import Reference</ToCustomsReference> <NonDeliveryOption>Return</NonDeliveryOption> <Container>MDFLATRATEBOX</Container> <ShippingContents> <ItemDetail> <Description>Description 1</Description> <Quantity>1</Quantity> <Value>1.11</Value> <NetPounds>1</NetPounds> <NetOunces>1</NetOunces> <HSTariffNumber>123456789123</HSTariffNumber> <CountryOfOrigin>Brazil</CountryOfOrigin> </ItemDetail> <ItemDetail> <Description>Description 2</Description> <Quantity>2</Quantity> <Value>2.22</Value> <NetPounds></NetPounds> <NetOunces>2</NetOunces> <HSTariffNumber>234567</HSTariffNumber> <CountryOfOrigin>Switzerland</CountryOfOrigin> </ItemDetail> <ItemDetail> <Description>Description 3</Description> <Quantity>3</Quantity> <Value>3.33</Value> <NetPounds></NetPounds> <NetOunces>3</NetOunces> <HSTariffNumber>123456789123</HSTariffNumber> <CountryOfOrigin>Brazil</CountryOfOrigin> </ItemDetail> <ItemDetail> <Description>Description 4</Description> <Quantity>4</Quantity> <Value>4.44</Value> <NetPounds></NetPounds> <NetOunces>4</NetOunces> <HSTariffNumber>234567234567</HSTariffNumber> <CountryOfOrigin>Switzerland</CountryOfOrigin> </ItemDetail> </ShippingContents> <Insured>N</Insured> <InsuredNumber>90123</InsuredNumber> <InsuredAmount>99.90</InsuredAmount> <GrossPounds>3</GrossPounds> <GrossOunces>8</GrossOunces> <ContentType>Documents</ContentType> <ContentTypeOther>and Other</ContentTypeOther> <Agreement>Y</Agreement> <Comments>PriorityMailIntl Comments</Comments> <LicenseNumber>Lic 123</LicenseNumber> <CertificateNumber>Cert456</CertificateNumber> <InvoiceNumber>Inv890</InvoiceNumber> <ImageType>TIF</ImageType> <ImageLayout>TRIMONEPERFILE</ImageLayout> <CustomerRefNo>Cust Ref123</CustomerRefNo> <POZipCode>20770</POZipCode> <LabelDate></LabelDate> <HoldForManifest>N</HoldForManifest> <EELPFC>802.11B</EELPFC> <CommercialPrice></CommercialPrice> <Size></Size> <Length></Length> <Width></Width> <Height></Height> <Girth></Girth> <ExtraServices> <ExtraService></ExtraService> </ExtraServices> </PriorityMailIntlCertifyRequest>"; 

         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);
            package.Error = errDesc;
            //throw new USPSManagerException(errDesc);
        }
        else
        {
            int i1 = xml.IndexOf("<LabelImage>") + "<LabelImage>".Length;
            int i2 = xml.IndexOf("</LabelImage>");
            package.ShippingLabel = Convert.FromBase64String(xml.Substring(i1, i2 - i1));

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xml);
            XmlNodeList nodeList = xmldoc.GetElementsByTagName("LabelImage");
            string _DeliveryConfirmationNumber = string.Empty;
            foreach (XmlNode node in nodeList)
            {
                _DeliveryConfirmationNumber = node.InnerText;
            }
            package.ReferenceNumber = _DeliveryConfirmationNumber;
        }
        return package;
    }   

在实现此代码时,我需要生成文件,但其错误看起来像

图片错误即将出现enter image description here

1 个答案:

答案 0 :(得分:1)

问题是您没有采取返回响应的正确部分(您不能查找Range("A2,E2").Copy ws.Range("B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1).PasteSpecial xlPasteValues 标记,而是Range("A2,E2").Copy With ws.Range("B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1) .PasteSpecial xlPasteValues, , False, False .PasteSpecial xlPasteFormats, , False, False End With )。试试这个:

PriorityMailIntlCertifyResponse

修改

根据@Fildor评论,最好使用XmlDocument来获取图像:

LabelImage