org.jscience.net
Class CGI

java.lang.Object
  extended by org.jscience.net.CGI

public class CGI
extends java.lang.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

Constructor Summary
CGI(java.net.URL cgiUrl)
          requires a URL based on the HTTP protocol specifying the script you are posting to
 
Method Summary
 void clearHeaders()
           
 java.util.Map<java.lang.String,java.lang.String> getHeaders()
           
 java.lang.String getResponse()
          can be used to retrieve the full text of the last server response after the post(Map) method was called.
 int post(java.util.Map<java.lang.Object,java.lang.Object> parameters)
          posts the given parameters to the instance's URL via POST.
 int post(java.lang.String content)
          posts the encoded string directly to the URL per HTTP POST.
 int post(java.lang.String[] param, java.lang.String[] value)
          posts the given parameter/value pairs to the embedded URL in the exact specified order.
 int post(java.lang.String parameter, java.lang.String value)
          convenience method to post a single parameter/value pair
 void setHeader(java.lang.String key, java.lang.String value)
          allows to add additional header information to the request or overwrite 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(java.net.URL cgiUrl)
    throws java.lang.UnsupportedOperationException
requires a URL based on the HTTP protocol specifying the script you are posting to

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

getResponse

public java.lang.String getResponse()
can be used to retrieve the full text of the last server response after the post(Map) method was called.

See Also:
post(Map)

setHeader

public void setHeader(java.lang.String key,
                      java.lang.String value)
allows to add additional header information to the request or overwrite 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 java.util.Map<java.lang.String,java.lang.String> getHeaders()

clearHeaders

public void clearHeaders()

post

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

Throws:
java.io.IOException

post

public int post(java.lang.String[] param,
                java.lang.String[] value)
         throws java.io.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:
java.io.IOException
See Also:
getResponse()

post

public int post(java.util.Map<java.lang.Object,java.lang.Object> parameters)
         throws java.io.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:
java.io.IOException
See Also:
getResponse()

post

public int post(java.lang.String content)
         throws java.io.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:
java.io.IOException
See Also:
getResponse()