------------------------------------------------------------------------------------- <%@ 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 - Service Center Details 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 our Web Customer Support at web.support@Con-way.com */ String title = "Service Center Details"; String requestType = "ServiceCenterDetailsRequest"; String requestUrl = "https://www.Con-way.com/webapp/servicecenter_app/ServiceCenterDetailsServlet"; // 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. */ String postalCode = "19348"; String xmlRequest = "<ServiceCenterDetailsRequest testmode=\"Y\">" + "<PostalCode country=\"US\">" + postalCode + "</PostalCode>" + "</ServiceCenterDetailsRequest>"; // Encode the Request String and set up the POST data xmlRequest = HttpUtility.UrlEncode(xmlRequest); String postData = requestType + "=" + 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 SicNodeList = xmlStatus.GetElementsByTagName("SIC"); XmlNodeList NameNodeList = xmlStatus.GetElementsByTagName("Name"); ArrayList sicList = new ArrayList(); for (int i = 0; i < SicNodeList.Count; i++) { sicList.Add("<br><b>Service Center SIC: </b> " + SicNodeList.Item(i).InnerText + "<b><br>Location: </b> " + NameNodeList.Item(i).InnerText ); } %> <html> <head><title>Sample .NET code for Con-way XML <%=title%></title></head> <body> <h2 align="center">Sample .NET code for Con-way XML <%=title%></h2> <%="Testing .NET execution ... Today is " + today%> <hr> <br> <b>Here is the <%=title%> XML output:</b> <br> <code><%=respString%></code> <br><br> <b>Here is the data extracted from the XML tag:</b> <% for (int i = 0; i < sicList.Count; i++) { Response.Write( (String) sicList[i]); } %> </html> <% // Clean up resources reqStream.Close(); xmlReader.Close(); respStream.Close(); wResp.Close(); %> -------------------------------------------------------------------------------------