Usage: java electronicmanifesting.URLConClient [Filename]
package electronicmanifesting; import java.io.*; import java.util.*; import java.net.*; import java.security.*; import com.sun.net.ssl.internal.www.protocol.https.*; import java.security.Security; import javax.net.ssl.*; public class URLConClient { public URLConClient() { super(); } /** * Starts the application. * @param args an array of command-line arguments * @throws Exception IOException(e) */ public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("Usage: java electronicmanifesting.URLConClient Filename"); return; } try{ // Certification Environment String pld_url="https://www.pld-certify.ups.com/hapld/tos/kdwhapltos "; String CRLF = "\r\n"; String HTTPversion = "HTTP/1.1"; // For SPF5.x - SPF7 uploads use HTTP/1.0 File f=new File(args[0]); long fileLength=f.length(); String Seperator = "--BOUNDARY" + CRLF; String requestEnd = CRLF + CRLF + "--BOUNDARY--" + CRLF;Build Message
according to the "Example of a Request Message" in the Electronic Manifesting documentation
/** * First Data Segment (Construction of Data-String and Header) * The "Content-type" of the first data segment is application/x-www-form-urlencoded. * According to this definition the following Values must be URL encoded. */ String userId=java.net.URLEncoder.encode("PLDDSTEST"); String password=java.net.URLEncoder.encode("PLDDSTEST"); String versionNumber=java.net.URLEncoder.encode("V4R1"); String responseType="application/x-ups-pld"; String appVersion=java.net.URLEncoder.encode("1.0"); String acceptUPSLicenseAgreement=java.net.URLEncoder.encode("Yes"); String FirstDataSegment = "AppVersion=" + appVersion + "&AcceptUPSLicenseAgreement=" + acceptUPSLicenseAgreement + "&ResponseType=" + responseType + "&VersionNumber=" + versionNumber+ "&UserId=" + userId + "&Password=" + password; String FirstDataSegmentHeader = "Content-type: application/x-www-form-urlencoded" + CRLF + "Content-length: " + FirstDataSegment.length() + CRLF + CRLF; /** * Second Data Segment (Construction of Data-String and Header) * The "Content-type" of the first data segment is application/x-ups-binary. * According to this definition the following Values must NOT be URL encoded. */ /** * The SecondDataSegment is the Content of the PLD File */ String SecondDataSegmentHeader = "Content-type: application/x-ups-binary" + CRLF + "Content-length: " + fileLength + CRLF + CRLF; /** * Main Header * The "Content-type" of Main Header is "multipart/mixed; boundary=BOUNDARY". * According to this definition the following Values must NOT be URL encoded. */ String MainHeader= "URL " + pld_url + HTTPversion + CRLF + "Content-type: multipart/mixed; boundary=BOUNDARY" + CRLF + //------------------------------------------------------------- // calculation of the Content-length according to the // "Example of a Request Message" in the Electronic Manifesting // documentation "Content-length: " + (Seperator.length() + FirstDataSegmentHeader.length() + FirstDataSegment.length() + CRLF.length() + CRLF.length() + Seperator.length() + SecondDataSegmentHeader.length() + fileLength + requestEnd.length()) //-------------------------------------------------------------- + CRLF + CRLF; // not belonging to the calculationHTTPS Post
/** * Establish a SSL URLConnection with the JSSE Package * and send Outputstream to UPS */ System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); URL url=new URL(pld_url); URLConnection connection=url.openConnection(); connection.setAllowUserInteraction(true); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Use the Content-type "multipart/mixed; boundary=BOUNDARY" in the MainHeader connection.setRequestProperty("Content-type", "multipart/mixed; boundary=BOUNDARY"); // new BufferedWriter out Writer out=new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); /** * Send first Part (MainHeader, FirstDataSegment, FirstDataSegmentHeader * and SecondDataSegmentHeader) to the Outputstream */ out.write(MainHeader); System.out.print(MainHeader); out.write(Seperator); System.out.print(Seperator); out.write(FirstDataSegmentHeader); System.out.print(FirstDataSegmentHeader); out.write(FirstDataSegment); System.out.print(FirstDataSegment); out.write(CRLF); System.out.print(CRLF); out.write(CRLF); System.out.print(CRLF); out.write(Seperator); System.out.print(Seperator); out.write(SecondDataSegmentHeader); System.out.print(SecondDataSegmentHeader); /** * Send SecondDataSegment: * Open PLD File(args[0]) with FileReader and * send content to the Outputstream */ try { int i=0; FileReader fr=new FileReader(f); while (i!= -1) { i=fr.read(); if (i!= -1) { out.write((char)i); System.out.print((char)i); } } fr.close(); } catch (IOException ioe) { System.out.println("IOException!"); } /** * Send requestEnd to the Outputstream */ out.write(requestEnd); System.out.println(requestEnd); /** * Send all left buffered Data to the Outputstream */ out.flush();Read Response
/** * read InputStream from the URLConnection and show UPS Response */ String inputLine; BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream())); // get ContentType and ContentLength from the MainHeader of the UPS Response System.out.println("Content-Type: " + connection.getContentType()); System.out.println("Content-Length: " + connection.getContentLength()); System.out.println("\r\n"); // Printout of the InputStream while((inputLine=in.readLine())!=null) { System.out.println(inputLine); } in.close(); } catch (IOException e) { System.err.println("Exception trying to make URL connection"); System.err.println(e); } } }
Copyright © 2003 United Parcel Service Deutschland Inc. & Co. OHG