HTTP Posting from Java

Contents

FormPoster.java


001: import java.net.*;
002: import java.io.*;
003: 
004: 
005: public class FormPoster {
006: 
007:   private URL url;
008:   private QueryString query = new QueryString(); 
009:    
010:   public FormPoster (URL url) throws IllegalArgumentException {
011:     if (!url.getProtocol().toLowerCase().startsWith("http")) {
012:       throw new IllegalArgumentException("Posting only works for http URLs");   
013:     }    
014:     this.url = url;
015:   }
016:   
017:   public void add(String name, String value) {
018:     query.add(name, value);    
019:   }
020:   
021:   public URL getURL() {
022:     return this.url; 
023:   }
024: 
025:   public InputStream post() {
026: 
027:       try {
028:     // open the connection and prepare it to POST
029:     URLConnection uc = url.openConnection();
030:     System.out.println("do output: " + uc.getDoOutput());
031:     System.out.println("do input: " + uc.getDoInput());
032:     uc.setDoOutput(true);
033:     System.out.println("do output: " + uc.getDoOutput());
034:     System.out.println("do input: " + uc.getDoInput());
035:     OutputStream raw = uc.getOutputStream();
036:     OutputStreamWriter out = new OutputStreamWriter(
037:     new BufferedOutputStream(raw), "UTF-8");
038:     //OutputStreamWriter out = new OutputStreamWriter(raw,"UTF-8");
039: 
040:     // The POST line, the Content-type header,
041:     // and the Content-length headers are sent by the URLConnection.
042:     // We just need to send the data
043:     System.out.println("QueryString: " + query);
044:     out.write(query.toString());
045:     out.write("\r\n");
046:     out.flush();
047:     out.close();
048:     // Return the response
049:     InputStream in = uc.getInputStream();
050:     return in;
051:     }
052:     catch (IOException e) {
053:         e.printStackTrace();
054:         return null;
055:     }
056: 
057: 
058: 
059:   }
060: 
061: 
062:   public static void main(String args[]) {
063: 
064:     URL url;
065: 
066:     if (args.length > 0) {
067:       try {
068:         url = new URL(args[0]);
069:       }
070:       catch (MalformedURLException e) {
071:         System.err.println(e);
072:         System.err.println("Usage: java FormPoster url");
073:         return;
074:       }
075:     }
076:     else {
077:       try {
078:         url = new URL("https://johnloomis.org/python/showfields.py");
079:       }
080:       catch (MalformedURLException e) { // shouldn't happen
081:         System.err.println(e);
082:         return; 
083:       }
084:     }
085: 
086:     FormPoster poster = new FormPoster(url);
087:     poster.add("name", "John Loomis");
088:     poster.add("class", "ECE 538");
089:     
090:     try {
091:       InputStream in = poster.post();
092:       in = new BufferedInputStream(in);
093:       // Read the response
094:       InputStreamReader r = new InputStreamReader(in);
095:       int c;
096:       while((c = r.read()) != -1) {
097:         System.out.print((char) c);
098:       }
099:       System.out.println();
100:       in.close();
101:     }
102:     catch (IOException e) {
103:       System.err.println(e);
104:       e.printStackTrace();   
105:     }
106: 
107:   }
108: 
109: }


QueryString.java


01: import java.net.URLEncoder;
02: 
03: public class QueryString {
04: 
05:   private String query, enc;
06: 
07:   public QueryString(Object name, Object value) {
08:       try {
09:           query = URLEncoder.encode(name.toString(),enc) + "=" + 
10:                   URLEncoder.encode(value.toString(),enc);
11:       }
12:       catch (java.io.UnsupportedEncodingException e) {
13:           e.printStackTrace();
14:       }
15:   }
16:   
17:   public QueryString() {
18:       query = "";
19:       enc = "UTF-8";
20:   }
21:   
22:   public synchronized void add(Object name, Object value) {
23: 
24:       try {
25:           if (!query.trim().equals("")) query += "&" ; 
26:           query += URLEncoder.encode(name.toString(),enc) + "=" + 
27:                    URLEncoder.encode(value.toString(),enc);
28:       }
29:       catch (java.io.UnsupportedEncodingException e) {
30:           e.printStackTrace();
31:       }
32:   
33:   }
34:   
35:   public String toString() {
36:     return query;
37:   }
38: 
39: }


Results

C:\prog\java_web>java FormPoster
do output: false
do input: true
do output: true
do input: true
QueryString: name=John+Loomis&class=ECE+538
<html>
<head><title>Show fields from CGI query string</title></head>
<body>
<p><table border>
<tr><th>Name<th>Value
<tr><td>name<td>John Loomis
<tr><td>class<td>ECE 538

</table>
</body></html>


Maintained by John Loomis, updated Wed Feb 20 16:08:57 2008