org.apache.commons.httpclient

Interface HttpMethod

public interface HttpMethod

HttpMethod interface represents a request to be sent via a HTTP connection and a corresponding response.

Since: 1.0

Version: $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $

Author: Remy Maucherat Rod Waldhoff Jeff Dever Mike Bowler Oleg Kalnichevski

Method Summary
voidabort()
Aborts the execution of the HTTP method.
voidaddRequestHeader(String headerName, String headerValue)
Adds the specified request header, not overwriting any previous value.
voidaddRequestHeader(Header header)
Adds the specified request header, not overwriting any previous value.
voidaddResponseFooter(Header footer)
Add a footer to this method's response.
intexecute(HttpState state, HttpConnection connection)
Executes this method using the specified HttpConnection and HttpState.
booleangetDoAuthentication()
Returns true if the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.), false otherwise
booleangetFollowRedirects()
Returns true if the HTTP method should automatically follow HTTP redirects (status code 302, etc.), false otherwise.
AuthStategetHostAuthState()
Returns the target host authentication state
HostConfigurationgetHostConfiguration()
Gets the host configuration for this method.
StringgetName()
Obtains the name of the HTTP method as used in the HTTP request line, for example "GET" or "POST".
HttpMethodParamsgetParams()
Returns HTTP protocol parameters associated with this method.
StringgetPath()
Returns the path of the HTTP method.
AuthStategetProxyAuthState()
Returns the proxy authentication state
StringgetQueryString()
Returns the query string of this HTTP method.
HeadergetRequestHeader(String headerName)
Gets the request header with the given name.
Header[]getRequestHeaders()
Returns the current request headers for this HTTP method.
Header[]getRequestHeaders(String headerName)
Returns the request headers with the given name.
byte[]getResponseBody()
Returns the response body of the HTTP method, if any, as an array of bytes.
InputStreamgetResponseBodyAsStream()
Returns the response body of the HTTP method, if any, as an InputStream.
StringgetResponseBodyAsString()
Returns the response body of the HTTP method, if any, as a String.
HeadergetResponseFooter(String footerName)
Return the specified response footer.
Header[]getResponseFooters()
Returns the response footers from the most recent execution of this request.
HeadergetResponseHeader(String headerName)
Returns the specified response header.
Header[]getResponseHeaders()
Returns the response headers from the most recent execution of this request.
Header[]getResponseHeaders(String headerName)
Returns the response headers with the given name.
intgetStatusCode()
Returns the status code associated with the latest response.
StatusLinegetStatusLine()
Returns the Status-Line from the most recent response for this method, or null if the method has not been executed.
StringgetStatusText()
Returns the status text (or "reason phrase") associated with the latest response.
URIgetURI()
Returns the URI for this method.
booleanhasBeenUsed()
Returns true if the HTTP method has been already executed, but not recycled.
booleanisRequestSent()
Returns true if the HTTP has been transmitted to the target server in its entirety, false otherwise.
booleanisStrictMode()
Returns the value of the strict mode flag.
voidrecycle()
Recycles the HTTP method so that it can be used again.
voidreleaseConnection()
Releases the connection being used by this HTTP method.
voidremoveRequestHeader(String headerName)
Removes all request headers with the given name.
voidremoveRequestHeader(Header header)
Removes the given request header.
voidsetDoAuthentication(boolean doAuthentication)
Sets whether or not the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.)
voidsetFollowRedirects(boolean followRedirects)
Sets whether or not the HTTP method should automatically follow HTTP redirects (status code 302, etc.)
voidsetParams(HttpMethodParams params)
Assigns HTTP protocol parameters for this method.
voidsetPath(String path)
Sets the path of the HTTP method.
voidsetQueryString(String queryString)
Sets the query string of the HTTP method.
voidsetQueryString(NameValuePair[] params)
Sets the query string of this HTTP method.
voidsetRequestHeader(String headerName, String headerValue)
Sets the specified request header, overwriting any previous value.
voidsetRequestHeader(Header header)
Sets the specified request header, overwriting any previous value.
voidsetStrictMode(boolean strictMode)
Defines how strictly the method follows the HTTP protocol specification.
voidsetURI(URI uri)
Sets the URI for this method.
booleanvalidate()
Returns true the method is ready to execute, false otherwise.

Method Detail

abort

public void abort()
Aborts the execution of the HTTP method.

Since: 3.0

See Also:

addRequestHeader

public void addRequestHeader(String headerName, String headerValue)
Adds the specified request header, not overwriting any previous value. If the same header is added multiple times, perhaps with different values, multiple instances of that header will be sent in the HTTP request. Note that header-name matching is case insensitive.

Parameters: headerName the header's name headerValue the header's value

See Also: addRequestHeader getRequestHeader removeRequestHeader

addRequestHeader

public void addRequestHeader(Header header)
Adds the specified request header, not overwriting any previous value. If the same header is added multiple times, perhaps with different values, multiple instances of that header will be sent in the HTTP request. Note that header-name matching is case insensitive.

Parameters: header the header

See Also: addRequestHeader getRequestHeader removeRequestHeader

addResponseFooter

public void addResponseFooter(Header footer)
Add a footer to this method's response.

Note: This method is for internal use only and should not be called by external clients.

Parameters: footer the footer to add

Since: 2.0

execute

public int execute(HttpState state, HttpConnection connection)
Executes this method using the specified HttpConnection and HttpState.

Parameters: state the state information to associate with this method connection the connection used to execute this HTTP method

Returns: the integer status code if one was obtained, or -1

Throws: IOException If an I/O (transport) error occurs. Some transport exceptions can be recovered from. HttpException If a protocol exception occurs. Usually protocol exceptions cannot be recovered from.

getDoAuthentication

public boolean getDoAuthentication()
Returns true if the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.), false otherwise

Returns: true if authentication challenges will be processed automatically, false otherwise.

Since: 2.0

See Also: HttpMethod

getFollowRedirects

public boolean getFollowRedirects()
Returns true if the HTTP method should automatically follow HTTP redirects (status code 302, etc.), false otherwise.

Returns: true if the method will automatically follow HTTP redirects, false otherwise

getHostAuthState

public AuthState getHostAuthState()
Returns the target host authentication state

Returns: host authentication state

Since: 3.0

getHostConfiguration

public HostConfiguration getHostConfiguration()

Deprecated: no longer applicable

Gets the host configuration for this method. The configuration specifies the server, port, protocol, and proxy server via which this method will send its HTTP request.

Returns: the HostConfiguration or null if none is set

getName

public String getName()
Obtains the name of the HTTP method as used in the HTTP request line, for example "GET" or "POST".

Returns: the name of this method

getParams

public HttpMethodParams getParams()
Returns HTTP protocol parameters associated with this method.

Since: 3.0

See Also: HttpMethodParams

getPath

public String getPath()
Returns the path of the HTTP method. Calling this method after the request has been executed will return the actual path, following any redirects automatically handled by this HTTP method.

Returns: the path of the HTTP method, in URL encoded form

getProxyAuthState

public AuthState getProxyAuthState()
Returns the proxy authentication state

Returns: host authentication state

Since: 3.0

getQueryString

public String getQueryString()
Returns the query string of this HTTP method.

Returns: the query string in URL encoded form, without a leading '?'.

See Also: (NameValuePair[]) setQueryString

getRequestHeader

public Header getRequestHeader(String headerName)
Gets the request header with the given name. If there are multiple headers with the same name, there values will be combined with the ',' separator as specified by RFC2616. Note that header-name matching is case insensitive.

Parameters: headerName the header name

Returns: the header

getRequestHeaders

public Header[] getRequestHeaders()
Returns the current request headers for this HTTP method. The returned headers will be in the same order that they were added with addRequestHeader. If there are multiple request headers with the same name (e.g. Cookie), they will be returned as multiple entries in the array.

Returns: an array containing all of the request headers

See Also: addRequestHeader addRequestHeader

getRequestHeaders

public Header[] getRequestHeaders(String headerName)
Returns the request headers with the given name. Note that header-name matching is case insensitive.

Parameters: headerName the name of the headers to be returned.

Returns: an array of zero or more headers

Since: 3.0

getResponseBody

public byte[] getResponseBody()
Returns the response body of the HTTP method, if any, as an array of bytes. If the method has not yet been executed or the response has no body, null is returned. Note that this method does not propagate I/O exceptions. If an error occurs while reading the body, null will be returned.

Returns: The response body, or null if the body is not available.

Throws: IOException if an I/O (transport) problem occurs

getResponseBodyAsStream

public InputStream getResponseBodyAsStream()
Returns the response body of the HTTP method, if any, as an InputStream. If the response had no body or the method has not yet been executed, null is returned. Additionally, null may be returned if HttpMethod has been called or if this method was called previously and the resulting stream was closed.

Returns: The response body, or null if it is not available

Throws: IOException if an I/O (transport) problem occurs

getResponseBodyAsString

public String getResponseBodyAsString()
Returns the response body of the HTTP method, if any, as a String. If response body is not available or cannot be read, null is returned. The raw bytes in the body are converted to a String using the character encoding specified in the response's Content-Type header, or ISO-8859-1 if the response did not specify a character set.

Note that this method does not propagate I/O exceptions. If an error occurs while reading the body, null will be returned.

Returns: The response body converted to a String, or null if the body is not available.

Throws: IOException if an I/O (transport) problem occurs

getResponseFooter

public Header getResponseFooter(String footerName)
Return the specified response footer. Note that footer-name matching is case insensitive.

Parameters: footerName The name of the footer.

Returns: The response footer.

getResponseFooters

public Header[] getResponseFooters()
Returns the response footers from the most recent execution of this request.

Returns: an array containing the response footers in the order that they appeared in the response. If the response had no footers, an empty array will be returned.

getResponseHeader

public Header getResponseHeader(String headerName)
Returns the specified response header. Note that header-name matching is case insensitive.

Parameters: headerName The name of the header to be returned.

Returns: The specified response header. If the repsonse contained multiple instances of the header, its values will be combined using the ',' separator as specified by RFC2616.

getResponseHeaders

public Header[] getResponseHeaders()
Returns the response headers from the most recent execution of this request.

Returns: A newly-created array containing all of the response headers, in the order in which they appeared in the response.

getResponseHeaders

public Header[] getResponseHeaders(String headerName)
Returns the response headers with the given name. Note that header-name matching is case insensitive.

Parameters: headerName the name of the headers to be returned.

Returns: an array of zero or more headers

Since: 3.0

getStatusCode

public int getStatusCode()
Returns the status code associated with the latest response.

Returns: The status code from the most recent execution of this method. If the method has not yet been executed, the result is undefined.

getStatusLine

public StatusLine getStatusLine()
Returns the Status-Line from the most recent response for this method, or null if the method has not been executed.

Returns: the status line, or null if the method has not been executed

Since: 2.0

getStatusText

public String getStatusText()
Returns the status text (or "reason phrase") associated with the latest response.

Returns: The status text from the most recent execution of this method. If the method has not yet been executed, the result is undefined.

getURI

public URI getURI()
Returns the URI for this method. The URI will be absolute if the host configuration has been set and relative otherwise.

Returns: the URI for this method

Throws: URIException if a URI cannot be constructed

hasBeenUsed

public boolean hasBeenUsed()
Returns true if the HTTP method has been already executed, but not recycled.

Returns: true if the method has been executed, false otherwise

isRequestSent

public boolean isRequestSent()
Returns true if the HTTP has been transmitted to the target server in its entirety, false otherwise. This flag can be useful for recovery logic. If the request has not been transmitted in its entirety, it is safe to retry the failed method.

Returns: true if the request has been sent, false otherwise

isStrictMode

public boolean isStrictMode()

Deprecated: Use HttpParams to exercise a more granular control over HTTP protocol strictness.

Returns the value of the strict mode flag.

Returns: true if strict mode is enabled, false otherwise

See Also: HttpMethod

recycle

public void recycle()

Deprecated: no longer supported and will be removed in the future version of HttpClient

Recycles the HTTP method so that it can be used again. Note that all of the instance variables will be reset once this method has been called. This method will also release the connection being used by this HTTP method.

See Also:

releaseConnection

public void releaseConnection()
Releases the connection being used by this HTTP method. In particular the connection is used to read the response (if there is one) and will be held until the response has been read. If the connection can be reused by other HTTP methods it is NOT closed at this point.

After this method is called, HttpMethod will return null, and HttpMethod and HttpMethod may return null.

removeRequestHeader

public void removeRequestHeader(String headerName)
Removes all request headers with the given name. Note that header-name matching is case insensitive.

Parameters: headerName the header name

removeRequestHeader

public void removeRequestHeader(Header header)
Removes the given request header.

Parameters: header the header

Since: 3.0

setDoAuthentication

public void setDoAuthentication(boolean doAuthentication)
Sets whether or not the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.)

Parameters: doAuthentication true to process authentication challenges automatically, false otherwise.

Since: 2.0

See Also: getDoAuthentication

setFollowRedirects

public void setFollowRedirects(boolean followRedirects)
Sets whether or not the HTTP method should automatically follow HTTP redirects (status code 302, etc.)

Parameters: followRedirects true if the method will automatically follow redirects, false otherwise.

setParams

public void setParams(HttpMethodParams params)
Assigns HTTP protocol parameters for this method.

Since: 3.0

See Also: HttpMethodParams

setPath

public void setPath(String path)
Sets the path of the HTTP method. It is responsibility of the caller to ensure that the path is properly encoded (URL safe).

Parameters: path The path of the HTTP method. The path is expected to be URL encoded.

setQueryString

public void setQueryString(String queryString)
Sets the query string of the HTTP method. It is responsibility of the caller to ensure that the path is properly encoded (URL safe). The string must not include an initial '?' character.

Parameters: queryString the query to be used in the request, with no leading '?' character

See Also: getQueryString (NameValuePair[])

setQueryString

public void setQueryString(NameValuePair[] params)
Sets the query string of this HTTP method. The pairs are encoded as UTF-8 characters. To use a different charset the parameters can be encoded manually using EncodingUtil and set as a single String.

Parameters: params An array of NameValuePairs to use as the query string. The name/value pairs will be automatically URL encoded and should not have been encoded previously.

See Also: getQueryString setQueryString (NameValuePair[], String)

setRequestHeader

public void setRequestHeader(String headerName, String headerValue)
Sets the specified request header, overwriting any previous value. Note that header-name matching is case insensitive.

Parameters: headerName the header's name headerValue the header's value

See Also: setRequestHeader getRequestHeader removeRequestHeader

setRequestHeader

public void setRequestHeader(Header header)
Sets the specified request header, overwriting any previous value. Note that header-name matching is case insensitive.

Parameters: header the header to be set

See Also: setRequestHeader getRequestHeader removeRequestHeader

setStrictMode

public void setStrictMode(boolean strictMode)

Deprecated: Use HttpParams to exercise a more granular control over HTTP protocol strictness.

Defines how strictly the method follows the HTTP protocol specification. (See RFC 2616 and other relevant RFCs.) In the strict mode the method precisely implements the requirements of the specification, whereas in non-strict mode it attempts to mimic the exact behaviour of commonly used HTTP agents, which many HTTP servers expect.

Parameters: strictMode true for strict mode, false otherwise

See Also: isStrictMode

setURI

public void setURI(URI uri)
Sets the URI for this method.

Parameters: uri URI to be set

Throws: URIException if a URI cannot be set

Since: 3.0

validate

public boolean validate()
Returns true the method is ready to execute, false otherwise.

Returns: true if the method is ready to execute, false otherwise.

Copyright (c) 1999-2005 - Apache Software Foundation