-------------------------------------------------------------------------------------
<%@ 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"%>
<%
/*
Con-way XML Sample Code for .NET - Shipment Status Tracking interface
-----------------
TODO: replace the USERNAME and PASSWORD String values below
with your Con-way username and password
-----------------
This C# code in an ASP.net page (*.aspx) has been tested on Windows 2003 server
and with the ASP.NET Web Matrix testing server ( http://www.asp.net/webmatrix/ )
with the .NET Framework version 1.1. In actual practice for design considerations,
you may want to put the C# code in its own class and invoke it on your page with something
like getConwayResponse(String xmlRequest);
-----------------
Send questions to Andrew vonderLuft at vonderluft.andrew@Con-way.com
*/
String title = "Shipment Status Tracking";
String requestType = "ShipmentStatusRequest";
String requestUrl = "https://www.Con-way.com/XMLj/X-ShipmentStatus";
// Con-way authentication parameters
// TODO *** you must replace the USERNAME & PASSWORD String values
// with your registered Con-way username and password below
String username = "USERNAME";
String password = "PASSWORD";
String authType = "basic";
// The target URI for the request
Uri conwayUri = new Uri(requestUrl);
//Format Today's Date into mm/dd/yy format
string today = DateTime.Today.Month.ToString() + "/" + DateTime.Today.Day.ToString()
+ "/" + DateTime.Today.Year.ToString().Remove(0, 2);
/* Build the XML Request String.
In actual use, you would probably populate the Request
parameters from data submitted via an on-line order form or database.
You could use the .NET XmlDocument class to build the XML, and then
use the .InnerXml property to turn it into a String for the POST.
For this sample we will just hard code some dummy data.
*/
// Tracking number ArrayList - add items, replacing TRACKNUM Strings with your Tracking numbers
ArrayList trackNums = new ArrayList();
trackNums.Add("TRACKNUM");
// add more as needed
String xmlRequest="";
for (int i=0; i < trackNums.Count; i++) {
xmlRequest += "" + trackNums[i] + "";
}
xmlRequest += "";
// Encode the Request String and set up the POST data
xmlRequest = HttpUtility.UrlEncode(xmlRequest);
String postData = "ShipmentStatusRequest=" + xmlRequest;
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 myCred = new NetworkCredential(username, password);
CredentialCache myCache = new CredentialCache();
myCache.Add(conwayUri, authType, myCred);
wReq.Credentials = myCache;
wReq.PreAuthenticate = true;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(postBuffer,0,postBuffer.Length);
HttpWebResponse wResp = (HttpWebResponse) wReq.GetResponse();
Stream respStream = wResp.GetResponseStream ();
XmlTextReader xmlReader = new XmlTextReader(respStream);
XmlDocument xmlStatus = new XmlDocument();
xmlStatus.Load(xmlReader);
//The entire XML Response String
String respString = xmlStatus.InnerXml;
//Here is how to get a value out of a specific XML element:
XmlNodeList PRONmbrNodeList = xmlStatus.GetElementsByTagName("PRONmbr");
XmlNodeList StatusNodeList = xmlStatus.GetElementsByTagName("StatusMessage");
XmlNodeList DeliveredDateNodeList = xmlStatus.GetElementsByTagName("DeliveredDate");
ArrayList statusList = new ArrayList();
for (int i = 0; i < PRONmbrNodeList.Count; i++) {
statusList.Add("
PRO Number " + PRONmbrNodeList.Item(i).InnerText
+ " -- Status: " + StatusNodeList.Item(i).InnerText
+ " -- Delivered on: " + DeliveredDateNodeList.Item(i).InnerText
);
}
%>
Sample .NET code for Con-way XML <%=title%>
Sample .NET code for Con-way XML <%=title%>
<%="Testing .NET execution ... Today is " + today%>
Here is the <%=title%> XML output:
<%=respString%>
Here is the data extracted from the XML tag:
<%
for (int i = 0; i < statusList.Count; i++) {
Response.Write( (String) statusList[i]);
}
%>
<%
// Clean up resources
reqStream.Close();
xmlReader.Close();
respStream.Close();
wResp.Close();
%>
-------------------------------------------------------------------------------------