Title Image



Sample Application Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;

namespace GetImageWSTest
{
    class Program
    {
        /// Summary
        /// A quick sample console program using the GetSaiaImageUtil class methods. You must provide
        /// a valid user id, password, Saia Pro number, and local path.
        
        /// param name="args">No arguments required
        static void Main(string[] args)
        {
            int errNum = 0;
            string Msg = string.Empty;

            GetImageEx.GetSaiaImageUtil getImage = new GetImageEx.GetSaiaImageUtil();

            // To Do - Provide user id, password, and pro number
            XmlDocument xdoc = getImage.GetSaiaImage("09911122233", "userid", "password");

            if (getImage.ErrorOccurred(xdoc, ref errNum, ref Msg))
                Console.WriteLine(errNum.ToString() + " - " + Msg);
            else
            {
                GetImageEx.ShipmentImage[] shipments;
                if (getImage.GetImageList(xdoc, out shipments))
                {
                    foreach (GetImageEx.ShipmentImage shpimg in shipments)
                    {
                        if (shpimg.imagestream != null)
                        {
                            Image img = Image.FromStream(shpimg.imagestream);
                            // To Do - provide a path and unique filename structure to store images in
                            img.Save("c:\\temp\\09911122233" + shpimg.DocType + shpimg.Pagenum.ToString() +
                                     "." + shpimg.ImageType);
                        }
                    }
                }
                else
                    Console.WriteLine("999 - Image retrieval failed.");
            }
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;

namespace GetImageEx
{

    // Optional data structure that stores each image returned by webservice.
    // This provides a more straight view and access to the image information.
    // Each image page is store as a ShipmentImage.  So you need to watch for 
    // Pro Number and doc type change to determine document boundraries.
    // The pages will load in Pro, document type, and page order. If the page
    // number restarts within doc type Saia captured the same document type 
    // multiple times.
    public struct ShipmentImage
    {
        public string DocType;
        public string DocDesc;
        public int Pagenum;
        public string ImageType;
        public Stream imagestream;
    }


    // This is a utility class that provides procedures and functions that handle some of the
    // complexity of accessing the web service and saving the files for the user.
    // If you would like to enhance or create your own this could just server as an exmaple. 
    // But you could just compile this class and use as is.
    public class GetSaiaImageUtil
    {
        /// summary
        /// Attempts to retrieve images from the webservice for a specific shipment by Saia Pro number.
        /// 
        /// param name="SaiaProNumber">11 digit Pro Number.  The leading zero's are not required.
        /// param name="SaiaSecureUserID">Your Saia secure internet it.  If you do not have one go to www.saia.com and use the link in the upper right hand corner to request an id.
        /// param name="password">The password to your saia secure user id.
        /// returns An xml document containing error information or the image download information.
        public XmlDocument GetSaiaImage(string SaiaProNumber, string SaiaSecureUserID, string password)
        {
            XmlDocument xdoc = new XmlDocument();
            try
            {
                string GetImageUrl = "http://www.saiasecure.com/irsec/getimginfo1.aspx?refnumber=" + SaiaProNumber;

                WebRequest webreq = WebRequest.Create(GetImageUrl);
                webreq.Method = "POST";
                webreq.ContentType = "application/x-www-form-urlencoded";

                webreq.Headers.Add("Authorization", Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(SaiaSecureUserID + ":" + password)));

                StreamWriter sw = new StreamWriter(webreq.GetRequestStream());
                sw.WriteLine(webreq.Headers.AllKeys);
                sw.Close();

                WebResponse webrep = webreq.GetResponse();
                StreamReader sr = new StreamReader(webrep.GetResponseStream());
                string response = sr.ReadToEnd();
                sr.Close();

                xdoc.LoadXml(response);
            }
            catch (System.Exception)
            {
                xdoc.LoadXml("");
            }
            return xdoc;
        }

        /// summary
        /// Pass the xml document returned by the GetSaiaImage routine and this will return a boolean
        /// indicating if an error occurred along with the error number and description.
        /// 
        /// param name="xdoc">XML document returned by GetSaiaImage
        /// param name="errorNumber">Returned error number if an error occurred
        /// param name="errorMessage">Returned error description if an error occurred
        /// returns>True if an error occurred and false if no error occurred.
        public bool ErrorOccurred(XmlDocument xdoc, ref int errorNumber, ref string errorMessage)
        {
            if (!xdoc.HasChildNodes)
            {
                errorNumber = 99;
                errorMessage = "No results were returned by web service. Host may be unavailable.";
                return true;
            }
            else
            {
                XmlNode nd = xdoc.SelectSingleNode("ErrorInfo");
                if (nd == null)
                    return false;
                else
                {
                    nd = nd.SelectSingleNode("Error");
                    int.TryParse(nd.Attributes["errorCode"].Value, out errorNumber);
                    errorMessage = nd.Attributes["errorMessage"].Value;
                    return true;
                }
            }
        }

        /// summary
        /// This procedure will return an array of ShipmentImage data structures contain each image
        /// page returned by the web service. It also returns in the shipmentimage data structure a
        /// stream object that can be used to display or save the image to disk.
        /// 
        /// param name="xdoc">XML the document returned  by GetSaiaImage
        /// param name="ShipmentImages">Array of image pages returned by web service
        /// returns>True if it was successful loading the array, false if an error occurred.
        public bool GetImageList(XmlDocument xdoc, out ShipmentImage[] ShipmentImages)
        {
            try
            {
                XmlNode nd = xdoc.SelectSingleNode("Info");
                if (nd == null)
                {
                    ShipmentImages = null;
                    return false;
                }
                else
                {
                    ShipmentImages = new ShipmentImage[nd.ChildNodes.Count];
                    int idx = 0;
                    XmlNode imgInfo = nd.FirstChild;
                    while (imgInfo != null)
                    {
                        ShipmentImages[idx].DocType = imgInfo.Attributes["docType"].Value;
                        ShipmentImages[idx].DocDesc = imgInfo.Attributes["docTypeName"].Value;
                        int.TryParse(imgInfo.Attributes["imagePageNum"].Value, out ShipmentImages[idx].Pagenum);
                        ShipmentImages[idx].ImageType = imgInfo.Attributes["imageFormat"].Value;

                        WebRequest req = WebRequest.Create(imgInfo.Attributes["imageUrl"].Value);
                        WebResponse rsp = req.GetResponse();

                        ShipmentImages[idx].imagestream = rsp.GetResponseStream();
                        idx++;
                        imgInfo = imgInfo.NextSibling;
                    }
                }
                return true;
            }
            catch (System.Exception)
            {
                ShipmentImages = null;
                return false;
            }
        }
    }
}