Beispiel #1
0
        private static void ScheduleShipment()
        {
            try
            {
                ShipService shpSvc = new ShipService();
                ShipmentRequest shipmentRequest = new ShipmentRequest();


                // security
                UPSSecurity upss = new UPSSecurity();
                UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
                upssSvcAccessToken.AccessLicenseNumber = "4CF6E9703C30E4B6";
                upss.ServiceAccessToken = upssSvcAccessToken;
                UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
                upssUsrNameToken.Username = "******";
                upssUsrNameToken.Password = "******";
                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 = "New shipment ...";

                // payment
                PaymentInfoType paymentInfo = new PaymentInfoType();
                ShipmentChargeType shpmentCharge = new ShipmentChargeType();
                BillShipperType billShipper = new BillShipperType();
                billShipper.AccountNumber = "1YA077";
                shpmentCharge.BillShipper = billShipper;
                shpmentCharge.Type = "01";
                ShipmentChargeType[] shpmentChargeArray = { shpmentCharge };
                paymentInfo.ShipmentCharge = shpmentChargeArray;
                shipment.PaymentInformation = paymentInfo;

                // shipper
                ShipperType shipper = new ShipperType();
                shipper.ShipperNumber = "1YA077";
                ShipWSSample.ShipWebReference.ShipAddressType shipperAddress =
                    new ShipWSSample.ShipWebReference.ShipAddressType();
                String[] addressLine = { "88 Foster Crescent" };
                shipperAddress.AddressLine = addressLine;
                shipperAddress.City = "Mississauga";
                shipperAddress.PostalCode = "L5R4A2";
                shipperAddress.StateProvinceCode = "ON";
                shipperAddress.CountryCode = "CA";
                shipper.Address = shipperAddress;
                shipper.Name = "CATO";
                shipper.AttentionName = "Ingram Micro - Mississauga";
                ShipPhoneType shipperPhone = new ShipPhoneType();
                shipperPhone.Number = "1234567890";
                shipper.Phone = shipperPhone;
                shipment.Shipper = shipper;

                // ship from
                ShipFromType shipFrom = new ShipFromType();
                ShipWSSample.ShipWebReference.ShipAddressType shipFromAddress =
                    new ShipWSSample.ShipWebReference.ShipAddressType();
                String[] shipFromAddressLine = { "135 Liberty St. Suite 101" };
                shipFromAddress.AddressLine = addressLine;
                shipFromAddress.City = "Toronto";
                shipFromAddress.PostalCode = "M6K1A7";
                shipFromAddress.StateProvinceCode = "ON";
                shipFromAddress.CountryCode = "CA";
                shipFrom.Address = shipFromAddress;
                shipFrom.AttentionName = "Mr. Andy Feng";
                shipFrom.Name = "Kobo Inc.";
                shipment.ShipFrom = shipFrom;

                // ship to
                ShipToType shipTo = new ShipToType();
                ShipToAddressType shipToAddress = new ShipToAddressType();
                String[] addressLine1 = { "403 Burr Oak Drive" };
                shipToAddress.AddressLine = addressLine1;
                shipToAddress.City = "Oswego";
                shipToAddress.PostalCode = "60543";
                shipToAddress.StateProvinceCode = "IL";
                shipToAddress.CountryCode = "US";
                shipTo.Address = shipToAddress;
                shipTo.AttentionName = "Mr. John Smith";
                shipTo.Name = "DEF Associates";
                ShipPhoneType shipToPhone = new ShipPhoneType();
                shipToPhone.Number = "(905) 123-1234";
                shipTo.Phone = shipToPhone;
                shipment.ShipTo = shipTo;

                //service
                ServiceType service = new ServiceType();
                //service.Code = "01"; // next day air
                service.Code = "11";//ups standard
                shipment.Service = service;


                // package
                PackageType package = new PackageType();
                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;
                // package 2
                PackageType package2 = new PackageType();
                PackageWeightType packageWeight2 = new PackageWeightType();
                packageWeight2.Weight = "9";
                ShipUnitOfMeasurementType uom2 = new ShipUnitOfMeasurementType();
                uom2.Code = "LBS";
                packageWeight2.UnitOfMeasurement = uom2;
                package2.PackageWeight = packageWeight2;
                PackagingType packType2 = new PackagingType();
                packType2.Code = "02";
                package2.Packaging = packType2;

                PackageType[] pkgArray = { package, package2 };
                shipment.Package = pkgArray;

                // add delivery confirmation for package level
                //PackageServiceOptionsType packageServiceOptions = new PackageServiceOptionsType();
                //package.PackageServiceOptions = packageServiceOptions;
                //package2.PackageServiceOptions = packageServiceOptions;
                //DeliveryConfirmationType deliveryConfirmation = new DeliveryConfirmationType();
                ////Service DCIS Type, The type of confirmation required upon delivery of the package. 
                //deliveryConfirmation.DCISType = "2"; //Delivery Confirmation Signature Required
                //// The delivery confirmation control number that confirms the package's delivery.
                ////deliveryConfirmation.DCISNumber = "xxxxxxxx";
                //packageServiceOptions.DeliveryConfirmation = deliveryConfirmation;

                // label
                LabelSpecificationType labelSpec = new LabelSpecificationType();
                LabelStockSizeType labelStockSize = new LabelStockSizeType();
                labelStockSize.Height = "1";
                labelStockSize.Width = "1";
                labelSpec.LabelStockSize = labelStockSize;
                LabelImageFormatType labelImageFormat = new LabelImageFormatType();
                //// for zpl
                //labelStockSize.Height = "6";
                //labelStockSize.Width = "4";
                //labelImageFormat.Code = "ZPL";
                // for gif
                labelImageFormat.Code = "GIF";
                labelSpec.LabelImageFormat = labelImageFormat;
                shipmentRequest.LabelSpecification = labelSpec;

                shipmentRequest.Shipment = shipment;

                // label
                ShipmentTypeShipmentServiceOptions options = new ShipmentTypeShipmentServiceOptions();
                //options.LabelDelivery = new LabelDeliveryType();
                shipment.ShipmentServiceOptions = options;

                // international form for paperless invoice
                InternationalFormType internationalForm = new InternationalFormType();
                internationalForm.AdditionalDocumentIndicator = null;
                internationalForm.ReasonForExport = "SALE";
                internationalForm.ExportDate = DateTime.Now.ToString("yyyyMMdd");
                internationalForm.ExportingCarrier = "UPS";
                internationalForm.InvoiceDate = DateTime.Now.ToString("yyyyMMdd");
                internationalForm.CurrencyCode = "CAD";
                internationalForm.FormType = new String[] { "01" };
                internationalForm.Contacts = new ContactType()
                {
                    SoldTo = new SoldToType()
                    {
                        Name = "andy",
                        Address = new AddressType() { AddressLine = new String[] { "box 40" }, City = "Tornnn", CountryCode = "US", PostalCode = "M1S2S5", StateProvinceCode = "ON" }
                    }
                };

                internationalForm.Product = new ProductType[]
                {
                    new ProductType() {ProducerInfo = "product1", Description = new String[] {"1233"},Unit = new UnitType() {Number = "1", UnitOfMeasurement = new UnitOfMeasurementType() {Code = "BG"},  Value = "1"}, OriginCountryCode = "CA", CommodityCode = "123456", NumberOfPackagesPerCommodity = "1", ProductWeight = new ProductWeightType() {UnitOfMeasurement = new UnitOfMeasurementType() {Code = "LBS"}, Weight = "12"}}
                };
                options.InternationalForms = internationalForm;
                // delivery confirmation for shipment level
                DeliveryConfirmationType deliveryConfirmation = new DeliveryConfirmationType();
                deliveryConfirmation.DCISType = "2";
                options.DeliveryConfirmation = deliveryConfirmation;

                Console.WriteLine(shipmentRequest);
                System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

                // response
                ShipmentResponse shipmentResponse = shpSvc.ProcessShipment(shipmentRequest);

                Console.WriteLine("The transaction was a " + shipmentResponse.Response.ResponseStatus.Description);
                
                // output label base64 characters
                string byteFileName = @"c:\Users\afeng\Pictures\label.txt";
                using (FileStream fs = new FileStream(byteFileName, FileMode.Create, FileAccess.ReadWrite))
                {
                    using (BinaryWriter sw = new BinaryWriter(fs))
                    {
                        sw.Write(shipmentResponse.ShipmentResults.PackageResults.FirstOrDefault().ShippingLabel.GraphicImage);
                    }
                }

                // output label image
                string filename = @"c:\Users\afeng\Pictures\label.gif";
                byte[] byteLabel;

                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
                {
                    using (BinaryWriter writer = new BinaryWriter(fs))
                    {
                        byteLabel =
                            Convert.FromBase64String(
                                shipmentResponse.ShipmentResults.PackageResults.FirstOrDefault().ShippingLabel.GraphicImage);
                        writer.Write(byteLabel);
                    }
                }


                // output label html page
                string htmlFilename = @"c:\Users\afeng\Pictures\label.html";
                byte[] htmlByteLabel;

                using (FileStream fs = new FileStream(htmlFilename, FileMode.Create, FileAccess.ReadWrite))
                {
                    using (BinaryWriter writer = new BinaryWriter(fs))
                    {
                        htmlByteLabel =
                            Convert.FromBase64String(
                                shipmentResponse.ShipmentResults.PackageResults.FirstOrDefault().ShippingLabel.HTMLImage);
                        writer.Write(htmlByteLabel);
                    }
                }

                // binary serialize
                Stream stream = File.Open(@"c:\Users\afeng\Pictures\data.dat", FileMode.Create);
                BinaryFormatter bformatter = new BinaryFormatter();
                Console.WriteLine("Writing binary serialize Information");
                bformatter.Serialize(stream, shipmentResponse);
                stream.Close();

                // xml serialize
                Stream requetStream = File.Open(@"c:\Users\afeng\Pictures\request.xml", FileMode.Create);
                XmlSerializer requestSerializer = new XmlSerializer(shipment.GetType());
                Console.WriteLine("Writing xml serialize Information");
                requestSerializer.Serialize(requetStream, shipment);
                requetStream.Close();
                Stream responseStream = File.Open(@"c:\Users\afeng\Pictures\response.xml", FileMode.Create);
                XmlSerializer serializer = new XmlSerializer(shipmentResponse.GetType());
                Console.WriteLine("Writing xml serialize Information");
                serializer.Serialize(responseStream, shipmentResponse);
                responseStream.Close();

                // resize picture
                int width = (int)(8.25 * 72);
                int height = (int)(4.25 * 72);

                // resize picture 1
                Bitmap imgIn = new Bitmap(filename);
                double y = imgIn.Height;
                double x = imgIn.Width;
                double factor = 1;
                if (width > 0)
                {
                    factor = width / x;
                }
                else if (height > 0)
                {
                    factor = height / y;
                }
                System.IO.MemoryStream outStream = new System.IO.MemoryStream();
                Bitmap imgOut = new Bitmap((int)(x * factor), (int)(y * factor));

                // Set DPI of image (xDpi, yDpi)
                //imgOut.SetResolution(72, 72);
                imgOut.SetResolution(96, 96);

                Graphics g = Graphics.FromImage(imgOut);
                g.Clear(Color.White);
                g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)),
                    new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);

                imgOut.Save(outStream, ImageFormat.Gif);
                string filename2 = @"c:\Users\afeng\Pictures\label2.gif";
                FileStream fs2 = new FileStream(filename2, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter writer2 = new BinaryWriter(fs2);
                writer2.Write(outStream.ToArray());
                writer2.Close();


                // resize picture 2
                //Creates a new Bitmap as the size of the window
                Bitmap bmp = new Bitmap(width, height);
                //Creates a new graphics to handle the image that is coming from the stream
                Graphics g2 = Graphics.FromImage((Image)bmp);
                g2.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g2.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                //Resizes the image from the stream to fit our windows
                Bitmap imgIn2 = new Bitmap(filename);
                g2.DrawImage(imgIn2, 0, 0, width, height);
                string filename3 = @"c:\Users\afeng\Pictures\label3.gif";
                FileStream fs3 = new FileStream(filename3, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter writer3 = new BinaryWriter(fs3);
                System.IO.MemoryStream outStream2 = new System.IO.MemoryStream();
                bmp.Save(outStream2, ImageFormat.Gif);
                writer3.Write(outStream2.ToArray());
                writer3.Close();

                // resize picture 3
                Bitmap newImage = new Bitmap(width, height);
                using (Graphics gr = Graphics.FromImage(newImage))
                {
                    gr.SmoothingMode = SmoothingMode.HighQuality;
                    //gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    gr.InterpolationMode = InterpolationMode.NearestNeighbor;
                    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    gr.DrawImage(new Bitmap(filename), new Rectangle(0, 0, width, height));
                    string filename4 = @"c:\Users\afeng\Pictures\label4.gif";
                    FileStream fs4 = new FileStream(filename4, FileMode.Create, FileAccess.ReadWrite);
                    //newImage.Save(filename4, System.Drawing.Imaging.ImageFormat.Gif);
                    BinaryWriter writer4 = new BinaryWriter(fs4);
                    System.IO.MemoryStream outStream4 = new System.IO.MemoryStream();
                    newImage.Save(outStream4, ImageFormat.Gif);
                    writer4.Write(outStream4.ToArray());
                    writer4.Close();
                }

                // resize picture 4
                ImageHandler ih = new ImageHandler();
                string filename5 = @"c:\Users\afeng\Pictures\label5.jpg";
                MemoryStream stream5 = new MemoryStream();
                stream5.Write(byteLabel, 0, byteLabel.Length);
                //Bitmap b = new Bitmap(filename);
                Bitmap b = new Bitmap(stream5);
                ih.Save(b, width, height, 100, filename5);


                Console.WriteLine("The 1Z number of the new shipment is " +
                                  shipmentResponse.ShipmentResults.ShipmentIdentificationNumber);
                Console.ReadKey();
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Console.WriteLine("");
                Console.WriteLine("---------Ship Web Service returns error----------------");
                Console.WriteLine("---------\"Hard\" is user error \"Transient\" is system error----------------");
                Console.WriteLine("SoapException Message= " + ex.Message);
                Console.WriteLine("");
                Console.WriteLine("SoapException Category:Code:Message= " + ex.Detail.LastChild.InnerText);
                Console.WriteLine("");
                Console.WriteLine("SoapException XML String for all= " + ex.Detail.LastChild.OuterXml);
                Console.WriteLine("");
                Console.WriteLine("SoapException StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
                Console.WriteLine("");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Console.WriteLine("");
                Console.WriteLine("--------------------");
                Console.WriteLine("CommunicationException= " + ex.Message);
                Console.WriteLine("CommunicationException-StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
                Console.WriteLine("");
            }
            catch (Exception ex)
            {
                Console.WriteLine("");
                Console.WriteLine("-------------------------");
                Console.WriteLine(" General Exception= " + ex.Message);
                Console.WriteLine(" General Exception-StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
            }
            finally
            {
                Console.ReadKey();
            }
        }
Beispiel #2
0
        static void Main()
        {
            try
            {
                ShipService shpSvc = new ShipService();
                ShipmentRequest shipmentRequest = new ShipmentRequest();
                UPSSecurity upss = new UPSSecurity();
                UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
                upssSvcAccessToken.AccessLicenseNumber = "Your Access License";
                upss.ServiceAccessToken = upssSvcAccessToken;
                UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
                upssUsrNameToken.Username = "******";
                upssUsrNameToken.Password = "******";
                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";
                ShipperType shipper = new ShipperType();
                shipper.ShipperNumber = "Your Shipper Number";
                PaymentInfoType paymentInfo = new PaymentInfoType();
                ShipmentChargeType shpmentCharge = new ShipmentChargeType();
                BillShipperType billShipper = new BillShipperType();
                billShipper.AccountNumber = "Your Account Number";
                shpmentCharge.BillShipper = billShipper;
                shpmentCharge.Type = "01";
                ShipmentChargeType[] shpmentChargeArray = { shpmentCharge };
                paymentInfo.ShipmentCharge = shpmentChargeArray;
                shipment.PaymentInformation = paymentInfo;
                ShipWSSample.ShipWebReference.ShipAddressType shipperAddress = new ShipWSSample.ShipWebReference.ShipAddressType();
                String[] addressLine = { "480 Parkton Plaza" };
                shipperAddress.AddressLine = addressLine;
                shipperAddress.City = "Timonium";
                shipperAddress.PostalCode = "21093";
                shipperAddress.StateProvinceCode = "MD";
                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();
                ShipWSSample.ShipWebReference.ShipAddressType shipFromAddress = new ShipWSSample.ShipWebReference.ShipAddressType();
                String[] shipFromAddressLine = { "Ship From Street" };
                shipFromAddress.AddressLine = addressLine;
                shipFromAddress.City = "Timonium";
                shipFromAddress.PostalCode = "21093";
                shipFromAddress.StateProvinceCode = "MD";
                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.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 = "01";
                shipment.Service = service;
                PackageType package = new PackageType();
                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();
                LabelStockSizeType labelStockSize = new LabelStockSizeType();
                labelStockSize.Height = "6";
                labelStockSize.Width = "4";
                labelSpec.LabelStockSize = labelStockSize;
                LabelImageFormatType labelImageFormat = new LabelImageFormatType();
                labelImageFormat.Code = "SPL";
                labelSpec.LabelImageFormat = labelImageFormat;
                shipmentRequest.LabelSpecification = labelSpec;
                shipmentRequest.Shipment = shipment;
                Console.WriteLine(shipmentRequest);
                System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
                ShipmentResponse shipmentResponse = shpSvc.ProcessShipment(shipmentRequest);
                Console.WriteLine("The transaction was a " + shipmentResponse.Response.ResponseStatus.Description);
                Console.WriteLine("The 1Z number of the new shipment is " + shipmentResponse.ShipmentResults.ShipmentIdentificationNumber);
                Console.ReadKey();
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Console.WriteLine("");
                Console.WriteLine("---------Ship Web Service returns error----------------");
                Console.WriteLine("---------\"Hard\" is user error \"Transient\" is system error----------------");
                Console.WriteLine("SoapException Message= " + ex.Message);
                Console.WriteLine("");
                Console.WriteLine("SoapException Category:Code:Message= " + ex.Detail.LastChild.InnerText);
                Console.WriteLine("");
                Console.WriteLine("SoapException XML String for all= " + ex.Detail.LastChild.OuterXml);
                Console.WriteLine("");
                Console.WriteLine("SoapException StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
                Console.WriteLine("");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Console.WriteLine("");
                Console.WriteLine("--------------------");
                Console.WriteLine("CommunicationException= " + ex.Message);
                Console.WriteLine("CommunicationException-StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
                Console.WriteLine("");

            }
            catch (Exception ex)
            {
                Console.WriteLine("");
                Console.WriteLine("-------------------------");
                Console.WriteLine(" General Exception= " + ex.Message);
                Console.WriteLine(" General Exception-StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");

            }
            finally
            {
                Console.ReadKey();
            }

        }