|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.net.URLPost
public class URLPost
Class designed to communicate with a HTTP server by posting arguments.
When dealing with HTTP server's one may find themselves in a position where they need to "POST" arguments to the server in a fashion similar to the way in which a web browser might post data from a form submission.
This class provides a lot of help in this regard. Basically, you use it as follows:
Create the object.
Add the parameter name/value pairs you want to
post (you don't need to encode them, the necessary encoding will be done for
you).
Open a connection - the parameters you have specified will
be posted to the server.
InputStream from the returned
URLConnection and process the server's response.
InputStream once
you are done reading from it.
The following example demonstrates how one might do this:
import com.ccg.io.*;
import com.ccg.net.*;
import java.io.*;
import java.net.*;
public class Foo {
static public void main(String args[]) {
try {
URLPost post = new URLPost();
if ((args.length % 2) != 1) {
System.err.println("usage: java com.ccg.net.URLPost URL "
+ "[KEY0 VAL0 [KEY1 VAL1 ...]]");
}
post.setURL(new URL(args[0]));
for (int i = 1; i < args.length; i += 2) {
post.addParameter(args[i], args[i + 1]);
}
System.err.println("URL:" + post.getURL() + " parameters:" + post);
InputStream in = post.open().getInputStream();
Copy.streamToStream(in, System.out);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
URLCat| Field Summary | |
|---|---|
static String |
MIME_TYPE_SERIALIZED_OBJECT
The mime type for serialized Java objects. |
| Constructor Summary | |
|---|---|
URLPost()
Initializes object with no information. |
|
| Method Summary | |
|---|---|
void |
addParameter(Object name,
Object val)
Add a single name/value pair. |
void |
addParameters(Dictionary ht)
Add a entire table of parameters. |
void |
addParameters(LookupKeyed ht)
Add a entire table of parameters. |
void |
clearParameters()
Remove all of the NAME/VALUE pairs that will be POSTed. |
URL |
getURL()
Get the URL which we will open and post
parameters to. |
boolean |
getUseCaches()
Indicates if we should use cached results if available. |
URLConnection |
open()
Open a connection to the URL and
POST parameters. |
URLConnection |
open(URL url)
Open a connection to a URL and POST
parameters. |
void |
removeParameter(Object key)
Remove a NAME/VALUE pair. |
void |
setURL(URL val)
Set the URL which we will open and post
parameters to. |
void |
setUseCaches(boolean val)
Set whether we should use cached results if available. |
String |
toString()
Return the string representation of the object. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final String MIME_TYPE_SERIALIZED_OBJECT
While browsing the web one day, I came across something that indicated that there was a MIME type assigned for serialized java objects which had the form:
application / x - java - serialized - object
This constant can be used to refer to this MIME type (if you use this constant and a different MIME type is found - you won't need to recompile your code).
As a side note, it looks like a file extension of "ser" is commonly used for serialized java objects.
| Constructor Detail |
|---|
public URLPost()
After constructing the object in this form, one will want to use one of
the add methods to add name/value pairs to be
posted to the server.
addParameter(java.lang.Object, java.lang.Object),
addParameters(java.util.Dictionary)| Method Detail |
|---|
public void addParameters(Dictionary ht)
This method can be used to load an entire table of parameters (such as a
standard Properties object might contain). All of the
key/value pairs from the passed table will be added.
table - The table of name/value pairs to addaddParameter(java.lang.Object, java.lang.Object)public void addParameters(LookupKeyed ht)
This method can be used to load an entire table of parameters (as
maintained by the generic LookupKeyed object. All of
the key/value pairs from the passed table will be added.
table - The table of name/value pairs to addaddParameter(java.lang.Object, java.lang.Object)
public void addParameter(Object name,
Object val)
This method adds a single NAME/VALUE pair to the the list of arguments to post. The NAME/VALUE pair should NOT be URL encoded (we will do that when we post the arguments to the server). For example, the following three calls:
urlpost.addParameter("ld", "Paul Blankenbaker");
urlpost.addParameter("first", "(hi)");
urlpost.addParameter("fred", "");
Would result in the following encoded parameters being POSTed to the
server when the connection is opened:
"ld=Paul+Blankenbaker&first=%28hi%29&fred="
name - The NAME of the NAME/VALUE pair that will be POSTedarg2 - The VALUE of the NAME/VALUE pair that will be POSTedremoveParameter(java.lang.Object),
clearParameters(),
addParameters(Dictionary),
addParameters(LookupKeyed)public void removeParameter(Object key)
This method can be used to remove a NAME/VALUE pair from the list of parameters that will be posted to the server.
name - The NAME of the parameter to remove from the list of NAME/VALUE pairs to
POST to the server.addParameter(java.lang.Object, java.lang.Object)public void clearParameters()
This method clears ALL of the NAME/VALUE parameter pairs that will be
POSTed the next time a connection is opened. Most likely
you will want to add parameters after using this
method.
addParameter(java.lang.Object, java.lang.Object)public void setURL(URL val)
URL which we will open and post
parameters to.
val - New URL value to assign.getURL()public URL getURL()
URL which we will open and post
parameters to.
setURL(java.net.URL)
public URLConnection open(URL url)
throws IOException
connection to a URL and POST
parameters.
This method changes the URL associated with the object,
attempts to establish a connection and POSTs the parameters that have
been added.
url - The URL to connect to (must not be null).
get the InputStream from.
IOException - If there is a problem establishing the connection.open()
public URLConnection open()
throws IOException
connection to the URL and
POST parameters.
This method attempts to establish a connection to the current
URL and POSTs the parameters that have been
added.
get the InputStream from.
IOException - If there is a problem establishing the connection.setURL(java.net.URL),
open(URL)public void setUseCaches(boolean val)
val - New boolean value to assign.getUseCaches()public boolean getUseCaches()
setUseCaches(boolean)public String toString()
This method returns the string representation of the object which is a list of all of the NAME/VALUE pairs that will be posted to the server (they are URLEncoded in the returned string and ready to send).
toString in class ObjectaddParameter(java.lang.Object, java.lang.Object)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||