------------------------------------------------------------------------------------- <%@ 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"%> <% // This C# code in an ASP.net page (*.aspx) has been tested on Windows 2003 server // 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 this page with something // like getConwayBOL (String bolRequest); // Con-way authentication parameters // ATTENTION *** you must replace the "userId" & "passWd" String values // with your registered Con-way username and password String userId = "userId"; String passWd = "passWd"; String authType = "basic"; // The target URI for the request. Choose one of the following - the 2nd is SSL-secured. // Uri conwayUri = new Uri("http://www.Con-way.com/XMLj/X-BOL"); Uri conwayUri = new Uri("https://www.Con-way.com/XMLj/X-BOL"); //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 Bill of Lading Request XML String. * In actual use, you would probably populate the BOL Request * parameters (Weights, Classes, Zip Codes, etc.) 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 bolRequest="<BOLrequest testmode=\"Y\">" + "<RequesterUserId shipcode=\"S\">" + userId + "</RequesterUserId>" + "<ChargeCode>P</ChargeCode>" + "<PRONmbr></PRONmbr>" + "<CustRefNmbrs>" + "<PurchaseOrderNmbr>3338889</PurchaseOrderNmbr>" + "<PurchaseOrderNmbr>3338890</PurchaseOrderNmbr>" + "<OtherRefNmbr refcode=\"SKU\" refdesc=\"SKU Number\">3213A</OtherRefNmbr>" + "<OtherRefNmbr refcode=\"UPC\" refdesc=\"UPC number\">789283</OtherRefNmbr>" + "</CustRefNmbrs>" + "<Shipper>" + "<ShipperName>Alan Shipley</ShipperName>" + "<ShipperAddr>1234 NE Main</ShipperAddr>" + "<ShipperCity>Portland</ShipperCity>" + "<ShipperState>OR</ShipperState>" + "<ShipperZip country=\"US\">97202</ShipperZip>" + "<ShipperPhone extension=\"6055\">503.450.6055</ShipperPhone>" + "<ShipperEmail>web.support@Con-way.com</ShipperEmail>" + "</Shipper>" + "<COD>" + "<CODremitTo>" + "<CODremitToName>Albert Cod</CODremitToName>" + "<CODremitToAddr>1234 NE Main</CODremitToAddr>" + "<CODremitToCity>Portland</CODremitToCity>" + "<CODremitToState>OR</CODremitToState>" + "<CODremitToZip country=\"US\">97202</CODremitToZip>" + "</CODremitTo>" + "<CODamount pmttype=\"CustomerCheck\" chargecode=\"P\">4444.44</CODamount>" + "</COD>" + "<Consignee>" + "<ConsigneeCustNmbr>883885</ConsigneeCustNmbr>" + "<ConsigneePhone extension=\"6666\">503.450.6436</ConsigneePhone>" + "<ConsigneeEmail>web.support@Con-way.com</ConsigneeEmail>" + "</Consignee>" + "<Item>" + "<Quantity pkgtype=\"PLT\">44</Quantity>" + "<Weight unit=\"lbs\">667</Weight>" + "<Description>widget-arms</Description>" + "<CmdtyClass>775</CmdtyClass>" + "<NMFClass></NMFClass>" + "<HazMat>N</HazMat>" + "</Item>" + "<Item>" + "<Quantity pkgtype=\"PCS\">11</Quantity>" + "<Weight unit=\"lbs\">789</Weight>" + "<Description>cam-shafts</Description>" + "<CmdtyClass>100</CmdtyClass>" + "<NMFClass></NMFClass>" + "<HazMat>N</HazMat>" + "</Item>" + "<Accessorial chargecode=\"P\">GUR</Accessorial>" + "<Accessorial chargecode=\"C\">DID</Accessorial>" + "<Accessorial chargecode=\"C\">DST</Accessorial>" + "<ShippingRemarks>TEST TEST TEST</ShippingRemarks>" + "<EmergencyContact></EmergencyContact>" + "<PickupRequest>" + "<PickupDate>" + today + "</PickupDate>" + "<PickupReadyTime>4:00 pm</PickupReadyTime>" + "<DockCloseTime>7:00 pm</DockCloseTime>" + "<ContactName>Frank</ContactName>" + "<ContactCompany>Franklin Arms</ContactCompany>" + "<ContactPhone>(333)444-4321</ContactPhone>" + "</PickupRequest>" + "<SendBOLemail/>" + "</BOLrequest>"; // Encode the Request String and set up the POST data bolRequest = HttpUtility.UrlEncode(bolRequest); String postData = "BOLrequest=" + bolRequest; 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(userId, passWd); 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 xmlResponse = new XmlDocument(); xmlResponse.Load(xmlReader); //The entire XML Response String String respString = xmlResponse.InnerXml; //Here is how to get a value out of a specific XML element: String status = null; XmlNodeList statusNodes = xmlResponse.GetElementsByTagName("Status"); //If there is no Status node, there will be an error node if (statusNodes.Count > 0) { status = statusNodes.Item(0).InnerText; } else { status = xmlResponse.GetElementsByTagName("Error").Item(0).InnerText; } %> <html> <head><title>Sample .NET code for Con-way XML Bill of Lading</title></head> <body> <h2 align="center">Sample .NET code for Con-way XML Bill of Lading</h2> <%="Testing .NET execution ... Today is " + today%> <hr> <br> <b>Here is the Bill of Lading Response XML output:</b> <br> <code><%=respString%></code> <br> <!--- Print the tag value we extracted ---> <b>Here is the data extracted from an XML tag: BOL Request Status -- <%=status%></b> </body> </html> <% // Clean up resources reqStream.Close(); xmlReader.Close(); respStream.Close(); wResp.Close(); %> -------------------------------------------------------------------------------------