public abstract class HttpMethodBase extends Object implements HttpMethod
At minimum, subclasses will need to override:
getName()
to return the approriate name for this method
When a method requires additional request headers, subclasses will typically want to override:
addRequestHeaders(HttpState,HttpConnection)
to write those headers
When a method expects specific response headers, subclasses may want to override:
processResponseHeaders(HttpState,HttpConnection)
to handle those headers
Constructor and Description |
---|
HttpMethodBase()
No-arg constructor.
|
HttpMethodBase(String uri)
Constructor specifying a URI.
|
Modifier and Type | Method and Description |
---|---|
void |
abort()
Aborts the execution of this method.
|
void |
addRequestHeader(Header header)
Adds the specified request header, NOT overwriting any previous value.
|
void |
addRequestHeader(String headerName,
String headerValue)
Adds the specified request header, NOT overwriting any previous value.
|
void |
addResponseFooter(Header footer)
Use this method internally to add footers.
|
int |
execute(HttpState state,
HttpConnection conn)
Executes this method using the specified
HttpConnection and
HttpState . |
String |
getAuthenticationRealm()
Deprecated.
use #getHostAuthState()
|
boolean |
getDoAuthentication()
Returns true if the HTTP method should automatically handle HTTP
authentication challenges (status code 401, etc.), false otherwise
|
HttpVersion |
getEffectiveVersion()
Returns the HTTP version used with this method (may be null
if undefined, that is, the method has not been executed)
|
boolean |
getFollowRedirects()
Returns true if the HTTP method should automatically follow HTTP redirects
(status code 302, etc.), false otherwise.
|
AuthState |
getHostAuthState()
Returns the target host
authentication state |
HostConfiguration |
getHostConfiguration()
Deprecated.
no longer applicable
|
MethodRetryHandler |
getMethodRetryHandler()
Deprecated.
use
HttpMethodParams |
abstract String |
getName()
Obtains the name of the HTTP method as used in the HTTP request line,
for example "GET" or "POST".
|
HttpMethodParams |
getParams()
Returns
HTTP protocol parameters associated with this method. |
String |
getPath()
Gets the path of this HTTP method.
|
String |
getProxyAuthenticationRealm()
Deprecated.
use #getProxyAuthState()
|
AuthState |
getProxyAuthState()
Returns the proxy
authentication state |
String |
getQueryString()
Gets the query string of this HTTP method.
|
int |
getRecoverableExceptionCount()
Deprecated.
no longer used
Returns the number of "recoverable" exceptions thrown and handled, to
allow for monitoring the quality of the connection.
|
String |
getRequestCharSet()
Returns the character encoding of the request from the Content-Type header.
|
Header |
getRequestHeader(String headerName)
Returns the specified request header.
|
Header[] |
getRequestHeaders()
Returns an array of the requests headers that the HTTP method currently has
|
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.
|
byte[] |
getResponseBody(int maxlen)
Returns the response body of the HTTP method, if any, as an array of bytes.
|
InputStream |
getResponseBodyAsStream()
Returns the response body of the HTTP method, if any, as an
InputStream . |
String |
getResponseBodyAsString()
Returns the response body of the HTTP method, if any, as a
String . |
String |
getResponseBodyAsString(int maxlen)
Returns the response body of the HTTP method, if any, as a
String . |
String |
getResponseCharSet()
Returns the character encoding of the response from the Content-Type header.
|
long |
getResponseContentLength()
Return the length (in bytes) of the response body, as specified in a
Content-Length header.
|
Header |
getResponseFooter(String footerName)
Gets the response footer associated with the given name.
|
Header[] |
getResponseFooters()
Returns an array of the response footers that the HTTP method currently has
in the order in which they were read.
|
Header |
getResponseHeader(String headerName)
Gets the response header associated with the given name.
|
Header[] |
getResponseHeaders()
Returns an array of the response headers that the HTTP method currently has
in the order in which they were read.
|
Header[] |
getResponseHeaders(String headerName)
Returns the response headers with the given name.
|
int |
getStatusCode()
Returns the response status code.
|
StatusLine |
getStatusLine()
Provides access to the response status line.
|
String |
getStatusText()
Returns the status text (or "reason phrase") associated with the latest
response.
|
URI |
getURI()
Returns the URI of the HTTP method
|
boolean |
hasBeenUsed()
|
boolean |
isAborted()
Tests whether the execution of this method has been aborted
|
boolean |
isHttp11()
Deprecated.
|
boolean |
isRequestSent()
Returns true if the HTTP has been transmitted to the target
server in its entirety, false otherwise.
|
boolean |
isStrictMode()
Deprecated.
Use
HttpParams.setParameter(String, Object)
to exercise a more granular control over HTTP protocol strictness. |
void |
recycle()
Deprecated.
no longer supported and will be removed in the future
version of HttpClient
|
void |
releaseConnection()
Releases the connection being used by this HTTP method.
|
void |
removeRequestHeader(Header header)
Removes the given request header.
|
void |
removeRequestHeader(String headerName)
Remove the request header associated with the given name.
|
void |
setDoAuthentication(boolean doAuthentication)
Sets whether or not the HTTP method should automatically handle HTTP
authentication challenges (status code 401, etc.)
|
void |
setFollowRedirects(boolean followRedirects)
Sets whether or not the HTTP method should automatically follow HTTP redirects
(status code 302, etc.)
|
void |
setHostConfiguration(HostConfiguration hostconfig)
Deprecated.
no longer applicable
|
void |
setHttp11(boolean http11)
Deprecated.
|
void |
setMethodRetryHandler(MethodRetryHandler handler)
Deprecated.
use
HttpMethodParams |
void |
setParams(HttpMethodParams params)
Assigns
HTTP protocol parameters for this method. |
void |
setPath(String path)
Sets the path of the HTTP method.
|
void |
setQueryString(NameValuePair[] params)
Sets the query string of this HTTP method.
|
void |
setQueryString(String queryString)
Sets the query string of this HTTP method.
|
void |
setRequestHeader(Header header)
Sets the specified request header, overwriting any previous value.
|
void |
setRequestHeader(String headerName,
String headerValue)
Set the specified request header, overwriting any previous value.
|
void |
setStrictMode(boolean strictMode)
Deprecated.
Use
HttpParams.setParameter(String, Object)
to exercise a more granular control over HTTP protocol strictness. |
void |
setURI(URI uri)
Sets the URI for this method.
|
boolean |
validate()
Returns true the method is ready to execute, false otherwise.
|
public HttpMethodBase()
public HttpMethodBase(String uri) throws IllegalArgumentException, IllegalStateException
uri
- either an absolute or relative URI. The URI is expected
to be URL-encodedIllegalArgumentException
- when URI is invalidIllegalStateException
- when protocol of the absolute URI is not recognisedpublic abstract String getName()
getName
in interface HttpMethod
public URI getURI() throws URIException
getURI
in interface HttpMethod
URIException
- If the URI cannot be created.HttpMethod.getURI()
public void setURI(URI uri) throws URIException
setURI
in interface HttpMethod
uri
- URI to be setURIException
- if a URI cannot be setpublic void setFollowRedirects(boolean followRedirects)
setFollowRedirects
in interface HttpMethod
followRedirects
- true if the method will automatically follow redirects,
false otherwise.public boolean getFollowRedirects()
getFollowRedirects
in interface HttpMethod
public void setHttp11(boolean http11)
HttpMethodParams.setVersion(HttpVersion)
http11
- true to use HTTP/1.1, false to use 1.0public boolean getDoAuthentication()
getDoAuthentication
in interface HttpMethod
HttpMethod.setDoAuthentication(boolean)
public void setDoAuthentication(boolean doAuthentication)
setDoAuthentication
in interface HttpMethod
doAuthentication
- true to process authentication challenges
authomatically, false otherwise.HttpMethod.getDoAuthentication()
public boolean isHttp11()
HttpMethodParams.getVersion()
public void setPath(String path)
setPath
in interface HttpMethod
path
- the path of the HTTP method. The path is expected
to be URL-encodedpublic void addRequestHeader(Header header)
addRequestHeader
in interface HttpMethod
header
- the header to add to the requestHttpMethod.addRequestHeader(String,String)
,
HttpMethod.getRequestHeader(String)
,
HttpMethod.removeRequestHeader(String)
public void addResponseFooter(Header footer)
addResponseFooter
in interface HttpMethod
footer
- The footer to add.public String getPath()
getPath
in interface HttpMethod
public void setQueryString(String queryString)
setQueryString
in interface HttpMethod
queryString
- the query stringEncodingUtil.formUrlEncode(NameValuePair[], String)
public void setQueryString(NameValuePair[] params)
setQueryString
in interface HttpMethod
params
- an array of NameValuePair
s to add as query string
parameters. The name/value pairs will be automcatically
URL encodedEncodingUtil.formUrlEncode(NameValuePair[], String)
,
setQueryString(String)
public String getQueryString()
getQueryString
in interface HttpMethod
HttpMethod.setQueryString(NameValuePair[])
,
HttpMethod.setQueryString(String)
public void setRequestHeader(String headerName, String headerValue)
setRequestHeader
in interface HttpMethod
headerName
- the header's nameheaderValue
- the header's valueHttpMethod.setRequestHeader(Header)
,
HttpMethod.getRequestHeader(String)
,
HttpMethod.removeRequestHeader(String)
public void setRequestHeader(Header header)
setRequestHeader
in interface HttpMethod
header
- the headerHttpMethod.setRequestHeader(String,String)
,
HttpMethod.getRequestHeader(String)
,
HttpMethod.removeRequestHeader(String)
public Header getRequestHeader(String headerName)
getRequestHeader
in interface HttpMethod
headerName
- The name of the header to be returned.public Header[] getRequestHeaders()
getRequestHeaders
in interface HttpMethod
HttpMethod.addRequestHeader(Header)
,
HttpMethod.addRequestHeader(String,String)
public Header[] getRequestHeaders(String headerName)
HttpMethod
getRequestHeaders
in interface HttpMethod
headerName
- the name of the headers to be returned.HttpMethod.getRequestHeaders(java.lang.String)
public Header[] getResponseHeaders(String headerName)
HttpMethod
getResponseHeaders
in interface HttpMethod
headerName
- the name of the headers to be returned.HttpMethod.getResponseHeaders(java.lang.String)
public int getStatusCode()
getStatusCode
in interface HttpMethod
public StatusLine getStatusLine()
getStatusLine
in interface HttpMethod
public Header[] getResponseHeaders()
getResponseHeaders
in interface HttpMethod
public Header getResponseHeader(String headerName)
getResponseHeader
in interface HttpMethod
headerName
- the header name to matchpublic long getResponseContentLength()
Return -1 when the content-length is unknown.
public byte[] getResponseBody() throws IOException
getResponseBody
in interface HttpMethod
IOException
- If an I/O (transport) problem occurs while obtaining the
response body.public byte[] getResponseBody(int maxlen) throws IOException
If the response is large this method involves lots of array copying and many object
allocations, which makes it unsuitable for high-performance / low-footprint applications.
Those applications should use getResponseBodyAsStream()
.
maxlen
- the maximum content length to accept (number of bytes).IOException
- If an I/O (transport) problem occurs while obtaining the
response body.public InputStream getResponseBodyAsStream() throws IOException
InputStream
.
If response body is not available, returns null. If the response has been
buffered this method returns a new stream object on every call. If the response
has not been buffered the returned stream can only be read once.getResponseBodyAsStream
in interface HttpMethod
null
.IOException
- If an I/O (transport) problem occurs while obtaining the
response body.public String getResponseBodyAsString() throws IOException
String
.
If response body is not available or cannot be read, returns null
The string conversion on the data is done using the character encoding specified
in Content-Type header. Buffers the response and this method can be
called several times yielding the same result each time.
Note: This will cause the entire response body to be buffered in memory. A
malicious server may easily exhaust all the VM memory. It is strongly
recommended, to use getResponseAsStream if the content length of the response
is unknown or resonably large.getResponseBodyAsString
in interface HttpMethod
null
.IOException
- If an I/O (transport) problem occurs while obtaining the
response body.public String getResponseBodyAsString(int maxlen) throws IOException
String
.
If response body is not available or cannot be read, returns null
The string conversion on the data is done using the character encoding specified
in Content-Type header. Buffers the response and this method can be
called several times yielding the same result each time.
Note: This will cause the entire response body to be buffered in memory. This method is
safe if the content length of the response is unknown, because the amount of memory used
is limited.
If the response is large this method involves lots of array copying and many object
allocations, which makes it unsuitable for high-performance / low-footprint applications.
Those applications should use getResponseBodyAsStream()
.
maxlen
- the maximum content length to accept (number of bytes). Note that,
depending on the encoding, this is not equal to the number of characters.null
.IOException
- If an I/O (transport) problem occurs while obtaining the
response body.public Header[] getResponseFooters()
getResponseFooters
in interface HttpMethod
public Header getResponseFooter(String footerName)
getResponseFooter
in interface HttpMethod
footerName
- the footer name to matchpublic String getStatusText()
getStatusText
in interface HttpMethod
public void setStrictMode(boolean strictMode)
HttpParams.setParameter(String, Object)
to exercise a more granular control over HTTP protocol strictness.setStrictMode
in interface HttpMethod
strictMode
- true for strict mode, false otherwiseHttpMethod.isStrictMode()
public boolean isStrictMode()
HttpParams.setParameter(String, Object)
to exercise a more granular control over HTTP protocol strictness.HttpMethod
isStrictMode
in interface HttpMethod
HttpMethod.setStrictMode(boolean)
public void addRequestHeader(String headerName, String headerValue)
addRequestHeader
in interface HttpMethod
headerName
- the header's nameheaderValue
- the header's valueHttpMethod.addRequestHeader(Header)
,
HttpMethod.getRequestHeader(String)
,
HttpMethod.removeRequestHeader(String)
public int execute(HttpState state, HttpConnection conn) throws HttpException, IOException
HttpConnection
and
HttpState
.execute
in interface HttpMethod
state
- state
information to associate with this
request. Must be non-null.conn
- the connection
to used to execute
this HTTP method. Must be non-null.IOException
- if an I/O (transport) error occursHttpException
- if a protocol exception occurs.public void abort()
abort
in interface HttpMethod
HttpMethod.execute(HttpState, HttpConnection)
public boolean hasBeenUsed()
hasBeenUsed
in interface HttpMethod
public void recycle()
recycle
in interface HttpMethod
releaseConnection()
public void releaseConnection()
releaseConnection
in interface HttpMethod
public void removeRequestHeader(String headerName)
removeRequestHeader
in interface HttpMethod
headerName
- the header namepublic void removeRequestHeader(Header header)
removeRequestHeader
in interface HttpMethod
header
- the headerpublic boolean validate()
validate
in interface HttpMethod
public HttpMethodParams getParams()
HTTP protocol parameters
associated with this method.getParams
in interface HttpMethod
HttpMethodParams
public void setParams(HttpMethodParams params)
HTTP protocol parameters
for this method.setParams
in interface HttpMethod
HttpMethodParams
public HttpVersion getEffectiveVersion()
public String getProxyAuthenticationRealm()
public String getAuthenticationRealm()
public String getRequestCharSet()
public String getResponseCharSet()
public int getRecoverableExceptionCount()
public HostConfiguration getHostConfiguration()
host configuration
.getHostConfiguration
in interface HttpMethod
public void setHostConfiguration(HostConfiguration hostconfig)
host configuration
.hostconfig
- The hostConfiguration to setpublic MethodRetryHandler getMethodRetryHandler()
HttpMethodParams
retry handler
for this HTTP methodpublic void setMethodRetryHandler(MethodRetryHandler handler)
HttpMethodParams
retry handler
for this HTTP methodhandler
- the methodRetryHandler to use when this method executedpublic AuthState getHostAuthState()
authentication state
getHostAuthState
in interface HttpMethod
public AuthState getProxyAuthState()
authentication state
getProxyAuthState
in interface HttpMethod
public boolean isAborted()
public boolean isRequestSent()
isRequestSent
in interface HttpMethod
Copyright © 2010 - 2020 Adobe. All Rights Reserved