-------------------------------------------------------------------------------------
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Text" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Web" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Configuration" %>
<%@ import Namespace="System.Reflection" %>
<%@ import Namespace="System.Collections" %>
<%
/*
* This C# code in an ASP.net page (*.aspx) has been tested on the .NET Framework version 2.0
* In actual practice for design considerations, you would probably want to put the code in its own class
* and invoke it from this .aspx page.
*
* For support, email web.support@con-way.com
*/
string conwayXmlFunction = "ImageRequest";
string pageTitle = "Sample C# .NET code for Con-way XML " + conwayXmlFunction;
string now = DateTime.Now.ToString("yyyyMMddhhmmss");
// Con-way authentication parameters
// ATTENTION *** you must set the userId & passWd string values below
// with your registered Con-way username and password
string userId = "USERID";
string passWd = "PASSWORD";
string authType = "basic";
// Local proxy server authentication parameters
// If you are running this code behind a proxy server, set authentication values here
// string proxyUri = "";
// string proxyPort = "";
// string proxyUser = "";
// string proxyPassword = "";
// string proxyDomain = ""; // if your proxy username must be preceded by a domain, e.g. "domain/username"
// TODO: replace the directory location below with your preferred location to save image files
// On Windows, like this:
// For IIS, e.g.
string imgDir = "C:/Inetpub/wwwroot/conwayImages/";
// For Apache, e.g.
// string imgDir = "C:/Program Files/Apache Group/Apache2/htdocs/conwayImages/";
// or on Mac, like this:
// imgDir = "/Library/WebServer/Documents/conwayImages/"
// TODO: replace the URL below with your preferred location to access your image files
// The URL to access the images on your webserver
string imgUrl = "http://localhost/conwayImages/";
// The target URI for the request. Choose one of the following and comment the other - the 2nd is SSL-secured.
// Uri conwayUri = new Uri("http://www.con-way.com/webapp/imaging_app/XmlImageRetrieverServlet");
Uri conwayUri = new Uri("https://www.con-way.com/webapp/imaging_app/XmlImageRetrieverServlet");
/* Build XML Request
* In actual use, you would probably populate the Request
* parameters from data submitted via an on-line order form or database.
* For this sample, we will hard-code some dummy data.
*/
ArrayList imgReqs = new ArrayList();
Hashtable imgReqHash = new Hashtable();
// TODO: Repace PRONUMBER with your own 9 digit PRO Numbers
// Type may be BL, DR, LOA, WI. Format may be pdf, jpeg, png, tiff
// Add Image Requests
imgReqHash.Add("pro","PRONUMBER");
imgReqHash.Add("type","DR");
imgReqHash.Add("format","png");
imgReqs.Add(imgReqHash.Clone());
imgReqHash.Clear();
imgReqHash.Add("pro","PRONUMBER");
imgReqHash.Add("type","BL");
imgReqHash.Add("format","jpeg");
imgReqs.Add(imgReqHash.Clone());
imgReqHash.Clear();
// Use .NET XmlWriter class to build XML Request
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
settings.IndentChars = (" ");
StringBuilder sBuilder = new StringBuilder();
using (XmlWriter xWriter = XmlWriter.Create(sBuilder, settings)) {
// Write the XML input data.
xWriter.WriteStartElement(conwayXmlFunction);
foreach ( Hashtable imgReq in imgReqs.ToArray() ) {
xWriter.WriteStartElement("ImageKey");
xWriter.WriteElementString("PRONumber", (string) imgReq["pro"] );
xWriter.WriteElementString("ImageType", (string) imgReq["type"] );
xWriter.WriteElementString("ImageFormat", (string) imgReq["format"] );
xWriter.WriteEndElement();
}
xWriter.WriteEndElement();
xWriter.Flush();
}
// Encode the Request string and set up the POST data
string inputXml = sBuilder.ToString();
string encodedInputXml = HttpUtility.UrlEncode(inputXml);
string postData = conwayXmlFunction + "=" + encodedInputXml;
ASCIIEncoding encoding = new ASCIIEncoding();
byte [] postBuffer = encoding.GetBytes(postData);
// Set up the HTTP Request
HttpWebRequest wReq = (HttpWebRequest) WebRequest.Create(conwayUri);
wReq.ContentType="application/x-www-form-urlencoded";
wReq.ContentLength = postBuffer.Length;
wReq.Method="POST";
wReq.Timeout=10000;
wReq.KeepAlive = false;
NetworkCredential netCred = new NetworkCredential(userId, passWd);
CredentialCache myCache = new CredentialCache();
myCache.Add(conwayUri, authType, netCred);
wReq.Credentials = myCache;
wReq.PreAuthenticate = true;
// If you are running this code behind a Proxy Server, uncomment the 3 lines below.
WebProxy myProxy = new WebProxy(proxyUri + ":" + proxyPort , true);
myProxy.Credentials = new NetworkCredential(proxyUser, proxyPassword, proxyDomain);
wReq.Proxy = myProxy;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(postBuffer,0,postBuffer.Length);
HttpWebResponse wResp = (HttpWebResponse) wReq.GetResponse();
Stream respStream = wResp.GetResponseStream ();
XmlTextReader xmlReader = new XmlTextReader(respStream);
// Try using XmlReader here....
XmlDocument xmlResponse = new XmlDocument();
xmlResponse.Load(xmlReader);
// The entire XML Response string
string outputXml = xmlResponse.InnerXml;
// Get special element data, in this case the encoded images
XmlNodeList imgNodeList = xmlResponse.GetElementsByTagName("Image");
ArrayList urlList = new ArrayList(); // to store the decoded image URLs
foreach ( XmlNode imgNode in imgNodeList ) {
// dataList.Add(FromBase64(dataNode.InnerText));
// Convert the Base64 UUEncoded input into binary output.
string pro = imgNode["PRONumber"].InnerText;
string type = imgNode["ImageType"].InnerText;
string format = imgNode["ImageFormat"].InnerText;
string fileName = pro + type + now + "." + format;
byte[] binaryData;
try {
binaryData = System.Convert.FromBase64String(imgNode["ImageData"].InnerText);
} catch (System.ArgumentNullException) {
System.Console.WriteLine("Base 64 string is null.");
return;
} catch (System.FormatException) {
System.Console.WriteLine("Base 64 string length is not " +
"4 or is not an even multiple of 4." );
return;
}
// Write out the decoded data.
System.IO.FileStream outFile;
try {
outFile = new System.IO.FileStream(imgDir + fileName,
System.IO.FileMode.Create,
System.IO.FileAccess.Write);
outFile.Write(binaryData, 0, binaryData.Length);
outFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message);
Response.Write(exp.Message);
}
// Add the URLs to the array
urlList.Add(imgUrl + fileName);
}
// Clean up resources
reqStream.Close();
xmlReader.Close();
respStream.Close();
wResp.Close();
%>
<%=pageTitle%>
<%=pageTitle%>
Here is the XML input:
<%=inputXml%>
Here is the XML output:
<%=outputXml%>
Here are the images extracted from the XML Response:
<% foreach (string url in urlList) { %>
<% } %>
-------------------------------------------------------------------------------------