To send an XML request to the UPS OnLine Tools server, an HTTP (Hypertext Transfer Protocol) with a request method of "post" is performed. For additional information on HTTP protocol, go to http://www.rfc-editor.org/rfc.html and view the following Internet RFCs:
If a Java application is used, the recommended method for performing HTTP post to the server is using Java�s HttpUrlConnection Class.
The following code example illustrates how to write data to a Java HttpUrlConnection�s output stream and read the response from the HttpUrlConnection�s input stream:
try {
// Create new URL and connect
//the value of protocol should be either HTTP or HTTPS.
URL url = new URL(protocol + "://" + hostname + "/" + prefix + "/" + service);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// POST data
OutputStream out = connection.getOutputStream();
out.write(queryString.getBytes());
// get data from URL connection and return the XML document as a String
String data = "";
try
{
data = readURLConnection(connection);
}catch (Exception e)
{
System.out.println("Error in reading URL Connection" + e.toString());
}
}catch (Exception e1)
{
System.out.println("Error sending data to server" + e1.toString());
}
private static String readURLConnection(URLConnection uc) throws Exception
{
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line = null;
int letter = 0;
while ((letter = reader.read()) != -1)
buffer.append((char) letter);
} catch (Exception e)
{
System.out.println("Cannot read from URL" + e.toString());
throw e;
} finally
{
try
{
reader.close();
} catch (IOException io)
{
System.out.println("Error closing URLReader!");
throw io;
}
}
return buffer.toString();
} |
The values of protocol, hostname, and prefix in the sample class XmlTransmitter are from the configuration file. The value of service (such as Track, AV, etc.) is the first parameter when the class XmlTransmitter is run.
Please refer to "Working with Java Sample Code" before executing or compiling and source code found within this documentation.
Copyright � 2006 United Parcel Service of America, Inc.