|
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 (Common Gateway Interface) 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.addHeader("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.getResponseAsString());
} catch (IOException e) { e.printStackTrace(); }
Note that you do not need to 'escape' the values, as this is done
by the post(Map) method.
| Nested Class Summary | |
|---|---|
static class |
CGI.MultipartHandler
|
| Constructor Summary | |
|---|---|
CGI(String cgiURLString)
|
|
CGI(String cgiURLString,
String charset)
|
|
CGI(URL cgiUrl)
requires a URL based on the HTTP protocol specifying the script you are posting to |
|
CGI(URL cgiURL,
String charset)
|
|
| Method Summary | |
|---|---|
void |
addHeader(String key,
String value)
allows to add additional header information to the request. |
void |
clearAuthorization()
|
void |
clearHeaders()
|
String |
decode(String str)
|
String |
encode(Object str)
|
String |
encodeAsURLParameter(Map<?,?> parameters)
|
String |
encodeAsURLParameter(String[][] parameterValues)
|
int |
get()
|
int |
get(Map<?,?> parameters)
|
int |
get(String content)
|
int |
get(String[][] parameterValues)
|
int |
get(String parameter,
String value)
|
String |
getAuthorizationUser()
|
String |
getCharset()
returns the character set used for encoding (defaults to 'UTF-8') |
int |
getConnectTimeout()
|
String |
getErrorAsString()
|
List<SimpleEntry<String,String>> |
getHeaders()
|
byte[] |
getLastError()
|
Map<String,List<String>> |
getLastHeader()
|
byte[] |
getLastResponse()
|
int |
getReadTimeout()
|
String |
getResponseAsString()
convenience method: returns the last response as a String using the current character set encoding |
SSLHelper |
getSslHelper()
|
boolean |
isSendContentLength()
|
int |
post()
|
int |
post(Handler<HttpURLConnection,IOException> handler)
|
int |
post(InputStream stream)
|
int |
post(Map<?,?> parameters)
posts the given parameters to the instance's URL via POST. |
int |
post(String content)
|
int |
post(String[][] parameterValues)
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 |
removeHeader(String key)
|
void |
setAuthorization(String user,
char[] password)
|
void |
setCharset(String charset)
allows to change the character set used for encoding |
void |
setConnectTimeout(int connectTimeout)
|
void |
setHeader(String key,
String value)
|
void |
setReadTimeout(int readTimeout)
|
void |
setSendContentLength(boolean sendContentLength)
|
void |
setSslHelper(SSLHelper sslHelper)
|
int |
submit(String method,
Handler<HttpURLConnection,IOException> handler)
allows to provide a custom way to handle the streaming to the output by callback. |
int |
submit(String method,
InputStream content)
streams the content to the URL per HTTP: POST, GET or HEADER. |
int |
submit(String method,
String content)
posts the encoded string directly to the URL per HTTP: POST, GET or HEADER. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public CGI(String cgiURLString)
throws MalformedURLException,
UnsupportedOperationException
MalformedURLException
UnsupportedOperationException
public CGI(URL cgiUrl)
throws UnsupportedOperationException
UnsupportedOperationException - if the protocol is not HTTP
public CGI(String cgiURLString,
String charset)
throws MalformedURLException
MalformedURLException
public CGI(URL cgiURL,
String charset)
| Method Detail |
|---|
public String getCharset()
public void setCharset(String charset)
public SSLHelper getSslHelper()
public void setSslHelper(SSLHelper sslHelper)
public void setAuthorization(String user,
char[] password)
public String getAuthorizationUser()
public void clearAuthorization()
public int getConnectTimeout()
public void setConnectTimeout(int connectTimeout)
public int getReadTimeout()
public void setReadTimeout(int readTimeout)
public boolean isSendContentLength()
public void setSendContentLength(boolean sendContentLength)
public String getResponseAsString()
setCharset(String)public byte[] getLastResponse()
public byte[] getLastError()
public Map<String,List<String>> getLastHeader()
public String getErrorAsString()
public void setHeader(String key,
String value)
public void removeHeader(String key)
public void addHeader(String key,
String value)
URLConnection.setRequestProperty(String, String)public List<SimpleEntry<String,String>> getHeaders()
public void clearHeaders()
public int get()
throws IOException
IOException
public int post()
throws IOException
IOException
public int get(String parameter,
String value)
throws IOException
IOException
public int post(String parameter,
String value)
throws IOException
IOException
public int post(String[][] parameterValues)
throws IOException
The value entries must not already 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 get(String[][] parameterValues)
throws IOException
IOException
public int post(InputStream stream)
throws IOException
IOException
public int post(Handler<HttpURLConnection,IOException> handler)
throws IOException
IOExceptionCGI.MultipartHandler
public int post(Map<?,?> parameters)
throws IOException
getResponse()
IOExceptiongetResponseAsString()
public int get(Map<?,?> parameters)
throws IOException
IOExceptionpublic String encodeAsURLParameter(String[][] parameterValues)
public String encodeAsURLParameter(Map<?,?> parameters)
public String encode(Object str)
public String decode(String str)
public int post(String content)
throws IOException
IOException
public int get(String content)
throws IOException
IOException
public int submit(String method,
String content)
throws IOException
getResponse()
IOExceptiongetLastResponse()
public int submit(String method,
InputStream content)
throws IOException
getResponse()
IOExceptiongetLastResponse()
public int submit(String method,
Handler<HttpURLConnection,IOException> handler)
throws IOException
handler - the handler is called right before the connect happens
IOExceptiongetLastResponse()
|
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||