|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.net.URLPostMultiPartForm
public class URLPostMultiPartForm
Class designed to communicate with a HTTP server like a multi-part form.
WARNING: This class has not been fully tested as it was originally created to work with servlets - and then this programmer discovered that servlet engines don't like to deal with parameters posted in this manner (at least not the simple servlet tester).
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 multi part form submission (a form that uploads a file).
This class provides a lot of help in this regard. Basically, you use it as follows:
Open a connection.
string(s)
or as a file uploads.
get the input stream used to read the data with
(if you are expecting results back from the server).
close the connection.
Here is an example program demonstrating how to use this class
import com.ccg.io.*;
import com.ccg.net.*;
import java.io.*;
import java.net.*;
public class Foo {
static public void main(String args[]) {
try {
if (args.length == 0) {
System.err.println("usage: java Foo URL");
System.exit(1);
}
URLPostMultiPartForm post = new URLPostMultiPartForm();
System.err.println("Opening connection");
post.open(new URL(args[0]),true);
System.err.println("Writing parameter info");
post.writeStringParam("firstName","Paul");
post.writeStringParam("lastName","Bl ankenbaker");
post.writeFileParam("upload",new File("/home/pkb/.signature"));
post.writeStringParam("age","37");
System.err.println("Retrieving information");
Copy.streamToStream(post.getInputStream(),System.out);
System.err.println("Closing connection");
post.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
URLPost| Constructor Summary | |
|---|---|
URLPostMultiPartForm()
|
|
| Method Summary | |
|---|---|
IOException |
close()
Closes all of the open connections. |
InputStream |
getInputStream()
|
URL |
getURL()
Get the URL we communicate with |
void |
open(URL url,
boolean doInput)
Opens a URL connection. |
protected void |
writeEndParam()
|
void |
writeFileParam(String name,
File file)
Write a entire FILE to the server. |
protected PrintWriter |
writeStartParam(String name)
|
void |
writeStringParam(String name,
String value)
Write a single string parameter NAME/VALUE pair to the server. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public URLPostMultiPartForm()
| Method Detail |
|---|
public void open(URL url,
boolean doInput)
throws IOException
This method creates a connection to the specified URL. After the connection has been opened your can write the parameters you need to send to the server and then optionally get the InputStream to read the server's response.
url - The URL of the server to communicate with.doInput - Set to true if you will be reading data returned from the
server, set to false if you are only putting data to the
server.
IOException - If unable to open a connection.writeStringParam(java.lang.String, java.lang.String),
writeFileParam(java.lang.String, java.io.File),
getInputStream()public IOException close()
After you are done communicating, you can use this method to make sure that any open connections are closed.
IOException if there is a problem closing
the connection.open(java.net.URL, boolean)
public void writeStringParam(String name,
String value)
throws IOException
Use this method after opening a connection to
write a single NAME/VALUE pair to the server.
name - Name of the parameter to send.value - Value of the parameter to send.
IOException - If there is a problem sending the NAME/VALUE pair.writeFileParam(java.lang.String, java.io.File)
public void writeFileParam(String name,
File file)
throws IOException
Use this method after opening a connection to
write the contents of an entire file to the server (with an
associated NAME).
name - Name of the parameter that the server will see.file - The local file to send.
IOException - If there is a problem sending the NAME/VALUE pair.writeStringParam(java.lang.String, java.lang.String)
public InputStream getInputStream()
throws IOException
IOException
protected void writeEndParam()
throws IOException
IOException
protected PrintWriter writeStartParam(String name)
throws IOException
IOExceptionpublic URL getURL()
open(java.net.URL, boolean)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||