|
Holger's Java API |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.antelmann.net.CGI
public class CGI
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.
| 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 |
|---|
public CGI(URL cgiUrl)
throws UnsupportedOperationException
UnsupportedOperationException - if the protocol is not HTTP| Method Detail |
|---|
public InputStream getResponseStream()
public String getResponseAsString()
throws IOException
post(Map) method was called in the default platform encoding.
IOExceptionpost(Map)
public void setHeader(String key,
String value)
URLConnection.setRequestProperty(String, String)public Map<String,String> getHeaders()
public void clearHeaders()
public int post(String parameter,
String value)
throws IOException
IOException
public int post(String[] param,
String[] value)
throws IOException
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()
IOExceptiongetResponseAsString()
public int post(Map<Object,Object> parameters)
throws IOException
getResponse()
IOExceptiongetResponseAsString()
public int post(String content)
throws IOException
getResponse()
IOExceptiongetResponseAsString()
|
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||