Class HttpUtil
java.lang.Object
sunlabs.brazil.util.http.HttpUtil
The
HttpUtil class contains methods for performing simple
HTTP operations.- Version:
- 2.3
- Author:
- Colin Stevens (colin.stevens@sun.com)
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidextractQuery(String query, Dictionary table) Turns x-www-form-urlencoded form data into a dictionary.static StringextractUrlHost(String url) Get the host portion of a Url String.static StringextractUrlPath(String url) Get the path portion of a Url String.static StringextractUrlPort(String url) Get the port portion of a Url String as a string.static StringextractUrlProtocol(String url) Get the protocol portion of a Url String.static StringReturns a string containing the current time as an HTTP-formatted date.static StringformatTime(long time) Returns a string containing an HTTP-formatted date.static StringgetStatusPhrase(int code) Returns the HTTP error string associated with the integer error code.static StringhtmlEncode(String src) Converts a string into a valid HTML fragment.static longConvert a last-modified date in "standard" format into a time stamp.static StringDecodes a URL-encoded string by replacing all the "%XX" escape sequences in the string with the corresponding character.static StringMaps a string to be used in a query or post into a form that is acceptable in an URL.
-
Method Details
-
htmlEncode
Converts a string into a valid HTML fragment. Escapes the characters ", &, <, >, and all non-printables into the form&#xx;(their "decimal reference" form).- Parameters:
src- The string to convert.- Returns:
- The string with all the special characters converted to decimal reference form.
-
urlEncode
Maps a string to be used in a query or post into a form that is acceptable in an URL. Typically used when the caller wants to safely generate an HREF containing an arbitrary string that may have special characters.URL strings may not contain non-alphanumeric characters. All non-alphanumeric characters are converted to the escape sequence "%XX", where XX is the hexadecimal value of that character's code.
Note that the space character " " is NOT converted to "+". That is a common misconception. "+" represents a space only in query strings, not in the URL. "%20" is how an actual space character must be passed in an URL, and is also an acceptable way of passing a space in a query string.
- Parameters:
string- The string to convert.- Returns:
- The URL-encoded version of the given string.
-
urlDecode
Decodes a URL-encoded string by replacing all the "%XX" escape sequences in the string with the corresponding character.Malformed "%XX" sequences are silently ignored.
- Parameters:
string- The URL-encoded string.- Returns:
- The decoded version of the given string.
-
formatTime
Returns a string containing the current time as an HTTP-formatted date.- Returns:
- HTTP date string representing the current time.
-
formatTime
Returns a string containing an HTTP-formatted date.- Parameters:
time- The date to format (current time in msec).- Returns:
- HTTP date string representing the given time.
-
parseTime
Convert a last-modified date in "standard" format into a time stamp. This "inverses" formatTime.- Parameters:
time- A correctly formatted HTTP date string.- Returns:
- milliseconds since the epoch, or 0 if the conversion failed.
-
extractQuery
Turns x-www-form-urlencoded form data into a dictionary.- Parameters:
query- The x-www-form-urlencoded string. May benulltable- The dictionary to insert the form data into.
-
extractUrlProtocol
-
extractUrlHost
-
extractUrlPort
-
extractUrlPath
-
getStatusPhrase
Returns the HTTP error string associated with the integer error code. This error string can be used in HTTP responses. Unknown codes return the string "Error"- Parameters:
code- The code to look up.- Returns:
- The associated error string.
-