|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.barracudamvc.plankton.http.HttpRequester
This class encapsulates access to/from a URL via both POST and GET methods. To use, simply set the URL, the method (POST/GET), and the params. If you're using get, the params are optional (they can be included as part of the URL). Also note that you can pass a username and password if you need to do basic authentication. This class also now supports cookies, thanks to Shawn Wilson [shawnw@atmreports.com] - look at the sample code down in the main method for an example of how to use it (basically, you just use the requestor to access a URL, thereby getting the cookie, and then you re-use the requestor to access any other URLs which depend on that cookie). Refer to the source for this class (main method) to see an example of how you would use this class for both POST and GET methods:
Field Summary | |
protected boolean |
acceptCookies
|
protected boolean |
authenticate
|
protected URLConnection |
conn
|
protected List |
cookies
|
protected Map |
cookieTimes
|
static String |
GET
|
protected Map |
hdrs
|
protected BufferedReader |
in
|
protected InputStream |
inStream
|
protected boolean |
manageCookies
|
protected String |
method
|
protected HttpOutputWriter |
outputWriter
|
protected OutputStream |
outStream
|
protected String |
password
|
static String |
POST
|
protected Map |
props
|
protected URL |
url
|
protected String |
user
|
Constructor Summary | |
HttpRequester()
|
Method Summary | |
void |
addCookie(javax.servlet.http.Cookie cookie)
|
void |
addHeader(String key,
String value)
Add a header |
void |
clearCookies()
Clear any cookies this client knows about. |
void |
clearHeaders()
Clear any headers this client knows about. |
void |
connect()
Connect to the URL |
void |
disconnect()
Disconnect from the URL. |
boolean |
getAcceptCookies()
Determine whether or not we are accepting cookies. |
List |
getCookies()
Return a read-only list of cookies this client is sending to the server |
Map |
getHeaders()
Return a read-only list of headers this client is sending to the server (will not include cookies) |
InputStream |
getInputStream()
Get the underlying input stream |
boolean |
getManageCookies()
Find out whether we are managing cookies or not |
String |
getMethod()
Get the method we're using for this HttpRequest object |
OutputStream |
getOutputStream()
Get the underlying output stream |
HttpOutputWriter |
getOutputWriter()
Return the output writer. |
Map |
getParams()
Return the HashMap object for this HttpRequest. |
protected String |
getPassword()
Get the password |
URL |
getUrl()
Get the URL for the HttpRequest object |
URLConnection |
getURLConnection()
Return the connection. |
String |
getUser()
Get the user name |
static void |
main(String[] args)
|
String |
readLine()
Read responses from the URL |
boolean |
removeCookie(javax.servlet.http.Cookie cookie)
|
String |
removeHeader(String key)
remove a header |
void |
setAcceptCookies(boolean accept)
Set whether or not to accept cookies from the server. |
void |
setManageCookies(boolean manage)
Set whether to automatically manage cookies or not. |
void |
setMethod(String imethod)
Set the method we wish to use. |
void |
setOutputWriter(HttpOutputWriter ioutputWriter)
Set the output writer to be used for posting data |
void |
setParams(Map iprops)
Set the parmeters we wish to pass to the URL as name-value pairs. |
void |
setPassword(String ipwd)
Set the password (if we need to authenticate in order to make the connection) |
void |
setRequest(String iurl,
String imethod,
Map iprops)
Set the Request. |
void |
setRequest(String iurl,
String imethod,
Map iprops,
HttpOutputWriter ioutputWriter)
Set the Request. |
void |
setRequest(String iurl,
String imethod,
Map iprops,
String iuser,
String ipwd,
HttpOutputWriter ioutputWriter)
Set the Request. |
void |
setRequest(URL iurl,
String imethod,
Map iprops)
Set the Request. |
void |
setRequest(URL iurl,
String imethod,
Map iprops,
HttpOutputWriter ioutputWriter)
Set the Request. |
void |
setRequest(URL iurl,
String imethod,
Map iprops,
String iuser,
String ipwd,
HttpOutputWriter ioutputWriter)
Set the Request. |
void |
setUrl(String iurl)
Set the URL we wish to access |
void |
setUrl(URL iurl)
Set the URL we wish to access |
void |
setUser(String iuser)
Set the user (if we need to authenticate in order to make the connection) |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String POST
public static final String GET
protected URL url
protected String method
protected Map props
protected Map hdrs
protected HttpOutputWriter outputWriter
protected String user
protected String password
protected boolean authenticate
protected boolean acceptCookies
protected boolean manageCookies
protected List cookies
protected Map cookieTimes
protected OutputStream outStream
protected InputStream inStream
protected BufferedReader in
protected URLConnection conn
Constructor Detail |
public HttpRequester()
Method Detail |
public void setRequest(String iurl, String imethod, Map iprops) throws MalformedURLException
iurl
- the URL we wish to accessimethod
- the method we wish to use (either GET or POST)iprops
- the Map contains our key-value URL parameter pairs.
If the value is a Set, the resulting URL will contain a key-value
mapping for each entry in the Set.
MalformedURLException
public void setRequest(URL iurl, String imethod, Map iprops) throws MalformedURLException
iurl
- the URL we wish to accessimethod
- the method we wish to use (either GET or POST)iprops
- the Map contains our key-value URL parameter pairs.
If the value is a Set, the resulting URL will contain a key-value
mapping for each entry in the Set.
MalformedURLException
public void setRequest(String iurl, String imethod, Map iprops, HttpOutputWriter ioutputWriter) throws MalformedURLException
iurl
- the URL we wish to accessimethod
- the method we wish to use (either GET or POST)iprops
- the Map contains our key-value URL parameter pairs.
If the value is a Set, the resulting URL will contain a key-value
mapping for each entry in the Set.ioutputWriter
- the HttpOutputWriter we wish to write to
MalformedURLException
public void setRequest(URL iurl, String imethod, Map iprops, HttpOutputWriter ioutputWriter) throws MalformedURLException
iurl
- the URL we wish to accessimethod
- the method we wish to use (either GET or POST)iprops
- the Map contains our key-value URL parameter pairs.
If the value is a Set, the resulting URL will contain a key-value
mapping for each entry in the Set.ioutputWriter
- the HttpOutputWriter we wish to write to
MalformedURLException
public void setRequest(String iurl, String imethod, Map iprops, String iuser, String ipwd, HttpOutputWriter ioutputWriter) throws MalformedURLException
iurl
- the URL we wish to accessimethod
- the method we wish to use (either GET or POST)iprops
- the Map contains our key-value URL parameter pairs.
If the value is a Set, the resulting URL will contain a key-value
mapping for each entry in the Set.iuser
- the user named required to connectipwd
- the password named required to connectioutputWriter
- the HttpOutputWriter we wish to write to
MalformedURLException
public void setRequest(URL iurl, String imethod, Map iprops, String iuser, String ipwd, HttpOutputWriter ioutputWriter) throws MalformedURLException
iurl
- the URL we wish to accessimethod
- the method we wish to use (either GET or POST)iprops
- the Map contains our key-value URL parameter pairs.
If the value is a Set, the resulting URL will contain a key-value
mapping for each entry in the Set.iuser
- the user named required to connectipwd
- the password named required to connectioutputWriter
- the HttpOutputWriter we wish to write to
MalformedURLException
public void setUrl(String iurl) throws MalformedURLException
iurl
- the URL we wish to access
MalformedURLException
public void setUrl(URL iurl)
iurl
- the URL we wish to accesspublic URL getUrl()
public void setMethod(String imethod)
imethod
- the method we wish to use (either GET or POST)public String getMethod()
public void setParams(Map iprops)
iprops
- the Map contains our key-value URL parameter pairs.
If the value is a Set, the resulting URL will contain a key-value
mapping for each entry in the Set.public Map getParams()
public Map getHeaders()
public void clearHeaders()
public void addHeader(String key, String value)
public String removeHeader(String key)
public void setUser(String iuser)
iuser
- the user namepublic String getUser()
public void setPassword(String ipwd)
ipwd
- the passwordprotected String getPassword()
public void setAcceptCookies(boolean accept)
true
.
Setting this value to false
after cookies
have already been obtained does not clear the current
cookies, it simply will not accept any new cookies.
clearCookies()
public boolean getAcceptCookies()
public List getCookies()
cookies
, or null
if
the server has not set any cookies in the clientCookie
public void clearCookies()
public void addCookie(javax.servlet.http.Cookie cookie)
public boolean removeCookie(javax.servlet.http.Cookie cookie)
public void setManageCookies(boolean manage)
public boolean getManageCookies()
public void setOutputWriter(HttpOutputWriter ioutputWriter)
ioutputWriter
- the HttpOutputWriterpublic HttpOutputWriter getOutputWriter()
public URLConnection getURLConnection()
public void connect() throws ConnectException, IOException
ConnectException
IOException
public String readLine() throws ConnectException, IOException
ConnectException
IOException
public OutputStream getOutputStream() throws ConnectException
ConnectException
public InputStream getInputStream() throws ConnectException
ConnectException
public void disconnect()
public static void main(String[] args)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |