/** * */ package org.cipres.ws; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; /** * This the client program to send multipart message to the web server using HttpClient * * You can also use apache's TCPMon to monitor the TCP traffic between client and server * + From the Admin tab: * + Listen Port # 3333 (acts as the proxy, all traffic redirects to this port) * + Listener: * + Target Hostname: lcchan-linux.sdsc.edu * + Target Port #: 8888 * + sent the Listener port 3333 in your client program (i.e. this program) to watch the traffic packages * * @author lcchan * */ public class SendHttpPost { /** * @param args */ public static void main(String[] args) { String url = "http://lcchan-linux.sdsc.edu:8888/cipres-web/submit"; //String url = "http://localhost:3333"; HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); BufferedReader reader = null; try { // Send multiple data to servlet File file1 = new File("/tmp/input/test.1"); File file2 = new File("/tmp/input/test.2"); Part[] parts = { new StringPart("email","lcchan@sdsc.edu"), new StringPart("analysis", "MP"), new FilePart(file1.getName(), file1), new FilePart(file2.getName(), file2) }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); client.executeMethod(method); // read response from servlet System.out.println("Beginning status from response "); System.out.println("status code: " + method.getStatusCode()); System.out.println("status line: " + method.getStatusLine()); System.out.println("status text: " + method.getStatusText()); System.out.println("body : " + method.getResponseBodyAsString()); System.out.println(" end of servlet response"); // if multiple lines have been sent InputStream in = method.getResponseBodyAsStream(); reader = new BufferedReader(new InputStreamReader(in)); String line; StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line); buffer.append("\n"); } System.out.println("response from servlet...." + buffer.toString()); } catch (Exception ex) { System.out.println("++++ POST Exception: " + ex.toString()); } finally { // clean up method.releaseConnection(); try {reader.close();} catch(Exception ex){} } } }