|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.cleancode.net.UrlConnectionMgr
public class UrlConnectionMgr
UrlConnectionMgr retrieves a URL from the World Wide Web and provides a formatted breakdown of its components. One may request individual component data regarding:
UrlConnectionMgr
, one should first call
sendCookies
if there are any cookies to send.
Then, invoke connect()
to open the URL connection, and finally call
getComponent(int)
or getContent()
as needed. The
getComponent
method is used to retrieve a component.
Data is returned
as a two-dimensional array, suitable for pasting into a table.
The getContent
method is used to retrieve the actual contents
of the web page (or other appropriate entity).
There are a few utility methods which are static. buildCookie(java.lang.String, java.lang.String)
and parseCookie(java.lang.String)
are complementary functions for interfacing
with cookies. getBasicURL(java.lang.String)
may be used to strip off the query
portion of a URL. getGeneralWidths()
, getGeneralNames()
,
getCookieWidths()
and getCookieNames()
are support methods for setting up tables containing the returned data.
URL
,
URLConnection
Nested Class Summary | |
---|---|
static class |
UrlConnectionMgr.Test
A standalone test class. |
Field Summary | |
---|---|
static int |
CONNECTION_DATA
Selects info about the network connection. |
static int |
COOKIE_DATA
Selects info about the transaction cookies. |
static int |
HEADER_DATA
Selects info about the transaction header. |
static int |
HTML_DATA
Selects info about the web page. |
static int |
URL_DATA
Selects info about the URL. |
static String |
VERSION
Current version of this class. |
Constructor Summary | |
---|---|
UrlConnectionMgr(String urlString)
Constructs a UrlConnectionMgr for the specified URL string. |
Method Summary | |
---|---|
static String |
buildCookie(String name,
String value)
Construct a properly formatted cookie for transmission. |
void |
connect()
Opens a communications link to the resource referenced by this URL, if such a connection has not already been established. |
static String |
getBasicURL(String urlString)
Extracts the basic URL (without the query portion) from a complete URL. |
String[][] |
getComponent(int choice)
Returns a portion of a network transaction. |
String |
getContent()
Returns the contents of a URL, typically an HTML document. |
static String[] |
getCookieNames()
Returns column names for cookie URL data. |
static float[] |
getCookieWidths()
Returns relative column widths for cookie URL data. |
static String[] |
getGeneralNames()
Returns column names for general URL data. |
static float[] |
getGeneralWidths()
Returns relative column widths for general URL data. |
static boolean |
isCookie(String s)
Returns true if the string could be a cookie (recognized by the presence of the cookie assignment operator and lack of line feed/carriage returns). |
static String[] |
parseCookie(String cookie)
Parse a received cookie into its components. |
String |
post(String postData)
Does an HTTP POST on a single line of text. |
String |
post(String[] postData)
Does an HTTP POST on multiple lines of text. |
void |
sendCookie(String cookie)
Send a single cookie through a URLConnection . |
void |
sendCookies(String[] cookies)
Send multiple cookies through a URLConnection . |
void |
sendUserAgent(String agent)
Send a user-agent request field through a URLConnection . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int CONNECTION_DATA
getComponent(int)
which will return:
URL as known, input allowed, output allowed, user interaction, and uses cache.
If the protocol is HTTP, these will additionally be returned:
HTTP request method, HTTP response message, and HTTP response code.
public static final int URL_DATA
getComponent(int)
which will return:
authority, host, file, path, port, query, anchor, userinfo, and protocol.
public static final int HEADER_DATA
getComponent(int)
which will return:
HTTP response, date, server, content-type, content-length, set-cookie,
cache-control, accept-ranges, keep-alive, connection, expires, and others.
All of these are optional so any given web document
may or may not have a particular one.
public static final int COOKIE_DATA
getComponent(int)
which will return:
a list of cookies.
parseCookie
,
Constant Field Valuespublic static final int HTML_DATA
getComponent(int)
which will return:
meta tags, links, document title, document type definition, and include files.
SimpleHtmlParse
,
Constant Field Valuespublic static final String VERSION
Constructor Detail |
---|
public UrlConnectionMgr(String urlString) throws IOException
urlString
- a String representing the URL to connect to
IOException
- if an I/O error occurs while opening the connectionMethod Detail |
---|
public String post(String[] postData) throws IOException
postData
- array of text lines to POST
IOException
- if problems reading or writing on the connection.public String post(String postData) throws IOException
postData
- text line to POST
IOException
- if problems reading or writing on the connection.public void connect() throws IOException
URLConnection
.
IOException
- if an I/O error occurs while opening the connectionpublic String getContent() throws IOException
IOException
- if an I/O error occurs while reading from the input streampublic String[][] getComponent(int choice) throws IOException
choice
parameter may be one of:
CONNECTION_DATA
, URL_DATA
, HEADER_DATA
,
COOKIE_DATA
, or HTML_DATA
.
Any other values will return an empty string.
choice
- specifies the portion of the network transaction
IOException
- if an I/O error occurs while reading URL datapublic void sendCookies(String[] cookies)
URLConnection
.
cookies
- an array of properly formatted cookies
The method buildCookie
may be used to create cookies.public void sendCookie(String cookie)
URLConnection
.
cookie
- a properly formatted cookie
The method buildCookie
may be used to create cookies.public void sendUserAgent(String agent)
URLConnection
.
agent
- a properly formatted user-agentpublic static String buildCookie(String name, String value)
name
- name of the cookie parametervalue
- value of the cookie parameter
public static String[] parseCookie(String cookie)
name
- name of the cookie;
value
- value of the cookie;
path
- specifies the subset of URLs in a domain for which the cookie is valid;
expiry
- specifies a date string that defines the valid life time of that cookie.
domain
- Internet domain name of the host from which the URL will be fetched;
secure
- if present, cookie will only be transmitted if the communications channel with the host is a secure one.
name
and value
are always present;
other attributes are optional.
cookie
- a properly formatted cookie
public static String getBasicURL(String urlString)
urlString
- a String representing the URL to connect to
public static String[] getGeneralNames()
public static float[] getGeneralWidths()
getGeneralNames()
public static String[] getCookieNames()
parseCookie(java.lang.String)
public static float[] getCookieWidths()
parseCookie(java.lang.String)
,
getGeneralNames()
public static boolean isCookie(String s)
s
- String to test for "cookie-ness"
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
CleanCode Java Libraries | Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 |