Holger's
Java API

com.antelmann.net
Class CGI

java.lang.Object
  extended by com.antelmann.net.CGI

public class CGI
extends Object

The class CGI provides a convenient way to post parameters to a web-based CGI script.

Here is a complete code example that assumes that the script specified with the given URL takes two input values: 'name' and 'text':

 try {
     URL url = new URL("http://myserver/cgi-bin/myscript.cgi");
     CGI cgi = new CGI(url);
     cgi.setHeader("cookie", "my cookie value");
     HashMap params = new HashMap();
     params.put("name", "Me");
     params.put("text", "first line\nsecond line\n");
     int response = cgi.post(params);
     if (response != 200) {
         System.out.println("something went wrong");
     }
     // see the complete response the script sent back
     System.out.println(cgi.getResponse());
 } catch (IOException e) { e.printStackTrace(); }
 
Note that you do not need to 'escape' the values, as this is done by the post(Map) method.

Since:
5/5/2002
Author:
Holger Antelmann

Constructor Summary
CGI(URL cgiUrl)
          requires a URL based on the HTTP protocol specifying the script you are posting to
 
Method Summary
 void clearHeaders()
           
 Map<String,String> getHeaders()
           
 String getResponseAsString()
          can be used to retrieve the full text of the last server response after the post(Map) method was called in the default platform encoding.
 InputStream getResponseStream()
           
 int post(Map<Object,Object> parameters)
          posts the given parameters to the instance's URL via POST.
 int post(String content)
          posts the encoded string directly to the URL per HTTP POST.
 int post(String[] param, String[] value)
          posts the given parameter/value pairs to the embedded URL in the exact specified order.
 int post(String parameter, String value)
          convenience method to post a single parameter/value pair
 void setHeader(String key, String value)
          allows to add additional header information to the request or override existing ones.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CGI

public CGI(URL cgiUrl)
    throws UnsupportedOperationException
requires a URL based on the HTTP protocol specifying the script you are posting to

Throws:
UnsupportedOperationException - if the protocol is not HTTP
Method Detail

getResponseStream

public InputStream getResponseStream()

getResponseAsString

public String getResponseAsString()
                           throws IOException
can be used to retrieve the full text of the last server response after the post(Map) method was called in the default platform encoding.

Throws:
IOException
See Also:
post(Map)

setHeader

public void setHeader(String key,
                      String value)
allows to add additional header information to the request or override existing ones. The order of how headers are added is maintained as the Map used is a LinkedHashMap.

See Also:
URLConnection.setRequestProperty(String, String)

getHeaders

public Map<String,String> getHeaders()

clearHeaders

public void clearHeaders()

post

public int post(String parameter,
                String value)
         throws IOException
convenience method to post a single parameter/value pair

Throws:
IOException

post

public int post(String[] param,
                String[] value)
         throws IOException
posts the given parameter/value pairs to the embedded URL in the exact specified order.

The value entries must not be encoded; this is done within this method. To retrieve the full text returned by the server after you called this method, use getResponse()

Returns:
the response code from the server
Throws:
IOException
See Also:
getResponseAsString()

post

public int post(Map<Object,Object> parameters)
         throws IOException
posts the given parameters to the instance's URL via POST. The parameters map each input name to its value. Both, key and value, are used as strings; you must not 'escape' the strings (replacing spaces, \"&\"'s etc.) as this is automatically done. The method takes an Object-to-Object Map, so that Properties may be passed as parameter. To retrieve the full text returned by the server after you called this method, use getResponse()

Returns:
the response code from the server
Throws:
IOException
See Also:
getResponseAsString()

post

public int post(String content)
         throws IOException
posts the encoded string directly to the URL per HTTP POST. To retrieve the full text returned by the server after you called this method, use getResponse()

Returns:
the response code from the server
Throws:
IOException
See Also:
getResponseAsString()


(c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads