The recommended method for performing HTTP posts to the UPS XML OnLine Tools server is using Java’s HttpUrlConnection Class. Following is a code example that illustrates the basic use of this class.
try { // Create new URL and connect URL url = new URL(protocol + "://" + hostname + prefix + service); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Setup HTTP POST parameters connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); // Get POST data from input file (XML Document) String queryString = readInputFile(infile); // POST data OutputStream out = connection.getOutputStream(); try { out.write(queryString.getBytes()); out.close(); // Get Response HTTP Header parameters if (connection.getResponseCode() == 302) { String data = ""; try { data = readURLConnection(connection); } catch (Exception e) { // exception raised because HTTP status of 400/500 occur, do nothing } try { writeOutputFile(data, outfile); } catch (Exception e) { System.out.println("Error writing output file " + outfile); } } catch (Exception e) { System.out.println("Error sending data to server"); } } catch (Exception e) { System.out.println(e); }
The above example shows how to write data to a Java HttpUrlConnection’s output stream and read the response from the HttpUrlConnection’s readURLConnection() method. It is recommended that you follow the examples provided by your particular XML parser package or SSL package when implementing that respective code. A simple DOM Parsing example is provided in this sample.
The source as well as a few other examples helpful for integrating with the UPS XML OnLine Tools are included with this documentation. The source files are located in <root>/SigTrackingExamples/source/ (where root is the install directory of this documentation).
[Welcome] [Register] [Licensing] [Building] [Request] [Response] [References]