JAVA Socket Connection



class SocketClient

Usage: java electronicmanifesting.SocketClient [Filename]

package electronicmanifesting;

import java.io.*;
import java.util.*;
import java.net.*;
import java.security.*;
import javax.net.ssl.*;


public class SocketClient {

public SocketClient() {
	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.SocketClient Filename");
     return;
     }


      int port = 443; // default https port
      String host = "www.pld-certify.ups.com";// Certification Environment
      String uri = "/hapld/tos/kdwhapltos ";
      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 CRLF = "\r\n";
      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=    "POST " +  uri + HTTPversion + CRLF +
                          "Host: " + host + 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 calculation
HTTPS Post

 /**
 * Establish a SSL SocketConnection with the JSSE SSL SocketFactory
 * and send Outputstream to UPS
 */


try {
// open SSLSocketFactory
    SSLSocketFactory factory
    = (SSLSocketFactory) SSLSocketFactory.getDefault();


// open Socket
    SSLSocket socket = (SSLSocket) factory.createSocket(host, port);


// new OutputStraemWriter
    Writer out = new OutputStreamWriter(socket.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 Socket and show UPS Response
 */

    BufferedReader in = new BufferedReader(new
           InputStreamReader(socket.getInputStream()));
    int c;

    while ((c = in.read()) != -1) {
          System.out.write(c);
          }

    out.close();
    in.close();
    socket.close();// close socket

}
 catch (IOException e) {
   System.err.println(e);
   }
}



}






Copyright © 2003 United Parcel Service Deutschland Inc. & Co. OHG