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 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
Dim xmlDocAsStr as String = "..." ' 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 !
Dim responseXml as String = sb.ToString()
The following is a C# code snippet
string xmlDocAsStr = "..."; // xmlDocAsStr is the submit XML document as a String
// 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;
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();
string responseXml = sb.ToString();
©(2002, 2003-2007) Tech Data Corporation. All Rights Reserved. | Tech Data proprietary and confidential |