예제 #1
0
        public ShippingResult GetTrackingResult()
        {
            string         shippingResultInString = string.Empty;
            ShippingResult shippingResult         = new ShippingResult();

            shippingResultInString = GetTrackingInfoUSPSInString();


            MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(shippingResultInString));


            USPSTrackingResult.TrackResponse resultingMessage = new USPSTrackingResult.TrackResponse();
            USPSTrackingResultError.Error    error            = new USPSTrackingResultError.Error();

            if (memStream != null)
            {
                if (shippingResultInString.Contains("<Error>") && !shippingResultInString.Contains("<TrackResponse>"))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(USPSTrackingResultError.Error));
                    error          = (USPSTrackingResultError.Error)serializer.Deserialize(memStream);
                    shippingResult = USPSTrackingResultErrorWrap(error);
                }
                else
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(USPSTrackingResult.TrackResponse));
                    resultingMessage = (USPSTrackingResult.TrackResponse)serializer.Deserialize(memStream);
                    shippingResult   = USPSTrackingResultWrap(resultingMessage);
                }
            }

            return(shippingResult);
        }
예제 #2
0
        private ShippingResult USPSTrackingResultWrap(USPSTrackingResult.TrackResponse resultingMessage)
        {
            ShippingResult shippingResult = new ShippingResult();

            shippingResult.ServiceType   = resultingMessage.Items[0].Class;
            shippingResult.StatusCode    = resultingMessage.Items[0].StatusCategory;
            shippingResult.Status        = resultingMessage.Items[0].Status;
            shippingResult.StatusSummary = resultingMessage.Items[0].StatusSummary;

            if (resultingMessage.Items[0].Error != null)
            {
                if (!string.IsNullOrEmpty(resultingMessage.Items[0].Error.Number.ToString()))
                {
                    shippingResult.Delivered     = false;
                    shippingResult.StatusCode    = "Error";
                    shippingResult.Status        = resultingMessage.Items[0].Error.Description;
                    shippingResult.StatusSummary = resultingMessage.Items[0].Error.Description;
                    shippingResult.Message       = resultingMessage.Items[0].Error.Description;
                }
            }

            if (!string.IsNullOrEmpty(shippingResult.StatusCode))
            {
                if (shippingResult.StatusCode.ToUpper().Trim() == "DELIVERED")
                {
                    shippingResult.Delivered = true;
                    foreach (var item in resultingMessage.Items[0].TrackSummary)
                    {
                        if (item.Event.ToUpper().Contains("DELIVERED"))
                        {
                            shippingResult.DeliveredDateTime = item.EventDate + " " + item.EventTime;

                            //by CJ on Oct-05-2016, to have the signatureName.
                            shippingResult.SignatureName = string.IsNullOrEmpty(item.Name) ? "" : item.Name;

                            break;
                        }
                    }
                }
            }

            if (resultingMessage.Items[0].TrackDetail != null)
            {
                if (resultingMessage.Items[0].TrackDetail.Length > 0)
                {
                    foreach (var detail in resultingMessage.Items[0].TrackDetail)
                    {
                        ShippingResultEventDetail eventDetail = new ShippingResultEventDetail();
                        eventDetail.Event         = detail.Event;
                        eventDetail.EventDateTime = detail.EventDate + " " + detail.EventTime;
                        eventDetail.EventAddress  = detail.EventCity + " " + detail.EventState + " " + detail.EventZIPCode;
                        shippingResult.TrackingDetails.Add(eventDetail);
                    }
                }
            }

            return(shippingResult);
        }
예제 #3
0
        public List <ShippingResult> GetTrackingResult(List <string> trackingNumbers)
        {
            string shippingResultInString        = string.Empty;
            List <ShippingResult> shippingResult = new List <ShippingResult>();

            int           splitCount         = 0;
            List <string> throttledTrackings = null;

            while ((throttledTrackings = trackingNumbers.Skip(splitCount).Take(10).ToList()).Count() != 0)
            {
                if (splitCount != 0)
                {
                    System.Threading.Thread.Sleep(5000);
                }

                splitCount += 10;

                shippingResultInString = GetTrackingInfoUSPSInString(throttledTrackings);

                _memStream = new MemoryStream(Encoding.UTF8.GetBytes(shippingResultInString));


                USPSTrackingResult.TrackResponse resultingMessage = new USPSTrackingResult.TrackResponse();
                USPSTrackingResultError.Error    error            = new USPSTrackingResultError.Error();

                if (_memStream != null)
                {
                    if (shippingResultInString.Contains("<Error>") && !shippingResultInString.Contains("<TrackResponse>"))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(USPSTrackingResultError.Error));
                        error = (USPSTrackingResultError.Error)serializer.Deserialize(_memStream);
                        shippingResult.Add(USPSTrackingResultErrorWrap(error));
                        serializer = null;
                    }
                    else
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(USPSTrackingResult.TrackResponse));
                        resultingMessage = (USPSTrackingResult.TrackResponse)serializer.Deserialize(_memStream);
                        shippingResult.AddRange(USPSTrackingResultWrap(resultingMessage));
                        serializer = null;
                    }
                }
            }

            return(shippingResult);
        }