This section will not cover the programmatic creation of the submit XML document, there are many utilities available in a variety of languages that facilitate this processing. Nor will this section cover the accessing and navigation through the response XML document, again, many utilities and practices are available.
Once the submit document is created the following steps should be taken by your automated process:
- If necessary, convert the XML document to a String representation
- Establish an HTTP connection to Tech Data's XML service
- Set the HTTP's header property "Content-Type" to a value of "text/xml"
- Set the HTTP's header property "Content-Length" to the exact length of your submit XML document's string representation
- Post the XML document, as a string, through the HTTP connection from step 2 above
- Obtain a response stream from the HTTP connection
- Receive the response XML document as a string
- The Tech Data DTD files
- XML file to send
- The below HTML file
<html> <head> <script language="javascript"> function GetResponse() { var objForm = document.forms["frmXML"] var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" if (!(xmlDoc.loadXML(objForm.Request.value))) { alert("XML Load Failed: " + xmlDoc.parseError.reason); return; } var urlStr = "Tech Data's URL" // urlStr is Tech Data's URL as a String var xmlRecv = new ActiveXObject("Microsoft.XMLHTTP"); xmlRecv.open("POST", urlStr, false); // send the submit XML document xmlRecv.setRequestHeader("Content-Type", "text/xml"); xmlRecv.send(xmlDoc.xml); var xmlDoc2 = new ActiveXObject("Microsoft.XMLDOM") xmlDoc2.async="false" xmlResult = xmlRecv.responseXML // receive the response XML document
objForm.Response.value = xmlResult.xml ;
// objForm.Response.value contains the response XML document as a String } </script> </head> <body> <center> <form name="frmXML"> Enter XML Request Below:<br> <textarea name="Request" cols=60 rows=10></textarea><br> <input type="button" value="Get Response" onClick="GetResponse();"> <hr>XML Response Appears Below:<br> <textarea name="Response" cols=60 rows=10></textarea> </form> </center> </body> </html>
The following java code snippet was written under JDK 1.4.2
java.net.URL url = new java.net.URL( urlStr ); // urlStr is Tech Data's URL as a String
String inputLine = null;
java.net.HttpURLConnection con = ( java.net.HttpURLConnection )url.openConnection();
con.setUseCaches( false );
con.setDoOutput( true );
con.setDoInput( true );
con.setRequestProperty( "Content-Type", "text/xml");
// xmlDocAsStr is the submit XML document as a String
con.setRequestProperty( "Content-Length", "" + xmlDocAsStr.getBytes().length );
PrintStream out = new PrintStream( con.getOutputStream() );
out.print( sendText ); // send the submit xml document
out.close();
BufferedReader buffReader = new BufferedReader( new InputStreamReader( con.getInputStream() ) );
StringBuffer sb = new StringBuffer();
while ( ( inputLine = buffReader.readLine() ) != null) {
sb.append( inputLine ); // receive the response xml document
}// the response XML document is now in sb as a String
out.close(); // ALWAYS close your connection !
con = null; // This cleanup is best performed in a 'finally' code block
out = null;
con = null;
The following VB.Net code snippet was written in version 1.1
' xmlDocAsStr is the submit XML document as a String
Dim byteArray(xmlDocAsStr.Length - 1) As Byte
Dim objUTF8Encoding As New UTF8Encoding
byteArray = objUTF8Encoding.GetBytes(xmlDocAsStr)
' urlStr is Tech Data's URL as a String
Dim con As CType(WebRequest.Create(urlStr), HttpWebRequest)
con.Method = "POST"
con.ContentType = "text/xml"
con.ContentLength = byteArray.Length
Dim strmRequest As con.GetRequestStream()
strmRequest.Write(byteArray, 0, byteArray.Length) ' send the submit xml document
strmRequest.Close()
Dim srResponse as New StreamReader(con.GetResponse().GetResponseStream(), Encoding.ASCII)Dim sb as srResponse.ReadToEnd() ' receive the response xml document
' the response XML document is now in sb as a String
srResponse.Close() ' ALWAYS close your connection !
The following is a C# code snippet
// urlStr is Tech Data's URL as a String
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create( urlStr );
myRequest.Method = "POST";
myRequest.ContentType = "text/xml; charset=utf-8";
myRequest.ContentLength = xmlDocAsStr.Length; // xmlDocAsStr is the submit XML document as a String
Stream sendStream = myRequest.GetRequestStream();
byte[] data = Encoding.UTF8.GetBytes(xmlDocAsStr);sendStream.Write(data , 0, data.Length); // send the submit xml document
sendStream.Close(); // ALWAYS close your connection !
WebResponse myResponse = myRequest.GetResponse();
StreamReaderstreamReader = new StreamReader(myResponse.GetResponseStream());
StringBuilder sb = new StringBuilder();
while (streamReader.Peek() >= 0){
sb.Append( streamReader.ReadLine() ); // receive the response xml document
}// the response XML document is now in sb as a String
streamReader.Close(); // ALWAYS close your connection !
myResponse.Close();
© 2015 Tech Data Corporation. All Rights Reserved. | Tech Data proprietary and confidential |