Class HttpMethodBase
- java.lang.Object
-
- org.apache.commons.httpclient.HttpMethodBase
-
- All Implemented Interfaces:
HttpMethod
- Direct Known Subclasses:
ConnectMethod,DeleteMethod,ExpectContinueMethod,GetMethod,HeadMethod,OptionsMethod,TraceMethod
public abstract class HttpMethodBase extends Object implements HttpMethod
An abstract base implementation of 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
- Version:
- $Revision: 539441 $ $Date: 2007-05-18 14:56:55 +0200 (Fri, 18 May 2007) $
- Author:
- Remy Maucherat, Rodney Waldhoff, Sean C. Sullivan, dIon Gillard, Jeff Dever, Davanum Srinivas, Ortwin Glueck, Eric Johnson, Michael Becke, Oleg Kalnichevski, Mike Bowler, Gary Gregory, Christian Kohlschuetter
-
-
Field Summary
Fields Modifier and Type Field Description protected HttpVersioneffectiveVersionHTTP protocol version used for execution of this method.protected StatusLinestatusLineThe Status-Line from the response.
-
Constructor Summary
Constructors Constructor Description HttpMethodBase()No-arg constructor.HttpMethodBase(String uri)Constructor specifying a URI.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidabort()Aborts the execution of this method.protected voidaddCookieRequestHeader(HttpState state, HttpConnection conn)Generates Cookie request headers for thosecookies that match the given host, port and path.protected voidaddHostRequestHeader(HttpState state, HttpConnection conn)Generates Host request header, as long as no Host request header already exists.protected voidaddProxyConnectionHeader(HttpState state, HttpConnection conn)Generates Proxy-Connection: Keep-Alive request header when communicating via a proxy server.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.protected voidaddRequestHeaders(HttpState state, HttpConnection conn)Generates all the required requestheaders to be submitted via the givenconnection.voidaddResponseFooter(Header footer)Use this method internally to add footers.protected voidaddUserAgentRequestHeader(HttpState state, HttpConnection conn)Generates default User-Agent request header, as long as no User-Agent request header already exists.protected voidcheckNotUsed()protected voidcheckUsed()intexecute(HttpState state, HttpConnection conn)Executes this method using the specifiedHttpConnectionandHttpState.protected static StringgenerateRequestLine(HttpConnection connection, String name, String requestPath, String query, String version)Generates HTTP request line according to the specified attributes.StringgetAuthenticationRealm()Deprecated.use #getHostAuthState()protected StringgetContentCharSet(Header contentheader)Returns the character set from the Content-Type header.booleangetDoAuthentication()Returns true if the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.), false otherwiseHttpVersiongetEffectiveVersion()Returns the HTTP version used with this method (may be null if undefined, that is, the method has not been executed)booleangetFollowRedirects()Returns true if the HTTP method should automatically follow HTTP redirects (status code 302, etc.), false otherwise.AuthStategetHostAuthState()Returns the target hostauthentication stateHostConfigurationgetHostConfiguration()Deprecated.no longer applicableMethodRetryHandlergetMethodRetryHandler()Deprecated.useHttpMethodParamsabstract StringgetName()Obtains the name of the HTTP method as used in the HTTP request line, for example "GET" or "POST".HttpMethodParamsgetParams()ReturnsHTTP protocol parametersassociated with this method.StringgetPath()Gets the path of this HTTP method.StringgetProxyAuthenticationRealm()Deprecated.use #getProxyAuthState()AuthStategetProxyAuthState()Returns the proxyauthentication stateStringgetQueryString()Gets the query string of this HTTP method.intgetRecoverableExceptionCount()Deprecated.no longer used Returns the number of "recoverable" exceptions thrown and handled, to allow for monitoring the quality of the connection.StringgetRequestCharSet()Returns the character encoding of the request from the Content-Type header.HeadergetRequestHeader(String headerName)Returns the specified request header.protected HeaderGroupgetRequestHeaderGroup()Gets theheader groupstoring the request headers.Header[]getRequestHeaders()Returns an array of the requests headers that the HTTP method currently hasHeader[]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.InputStreamgetResponseBodyAsStream()Returns the response body of the HTTP method, if any, as anInputStream.StringgetResponseBodyAsString()Returns the response body of the HTTP method, if any, as aString.StringgetResponseBodyAsString(int maxlen)Returns the response body of the HTTP method, if any, as aString.StringgetResponseCharSet()Returns the character encoding of the response from the Content-Type header.longgetResponseContentLength()Return the length (in bytes) of the response body, as specified in a Content-Length header.HeadergetResponseFooter(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.HeadergetResponseHeader(String headerName)Gets the response header associated with the given name.protected HeaderGroupgetResponseHeaderGroup()Gets theheader groupstoring the response headers.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.protected InputStreamgetResponseStream()Returns a stream from which the body of the current response may be read.protected HeaderGroupgetResponseTrailerHeaderGroup()Gets theheader groupstoring the response trailer headers as per RFC 2616 section 3.6.1.intgetStatusCode()Returns the response status code.StatusLinegetStatusLine()Provides access to the response status line.StringgetStatusText()Returns the status text (or "reason phrase") associated with the latest response.URIgetURI()Returns the URI of the HTTP methodbooleanhasBeenUsed()booleanisAborted()Tests whether the execution of this method has been abortedprotected booleanisConnectionCloseForced()Tests if the connection should be force-closed when no longer needed.booleanisHttp11()Deprecated.booleanisRequestSent()Returns true if the HTTP has been transmitted to the target server in its entirety, false otherwise.booleanisStrictMode()Deprecated.UseHttpParams.setParameter(String, Object)to exercise a more granular control over HTTP protocol strictness.protected voidprocessCookieHeaders(CookieSpec parser, Header[] headers, HttpState state, HttpConnection conn)This method processes the specified cookie headers.protected voidprocessResponseBody(HttpState state, HttpConnection conn)This method is invoked immediately afterreadResponseBody(HttpState,HttpConnection)and can be overridden by sub-classes in order to provide custom body processing.protected voidprocessResponseHeaders(HttpState state, HttpConnection conn)This method is invoked immediately afterreadResponseHeaders(HttpState,HttpConnection)and can be overridden by sub-classes in order to provide custom response headers processing.protected voidprocessStatusLine(HttpState state, HttpConnection conn)This method is invoked immediately afterreadStatusLine(HttpState,HttpConnection)and can be overridden by sub-classes in order to provide custom response status line processing.protected voidreadResponse(HttpState state, HttpConnection conn)Reads the response from the givenconnection.protected voidreadResponseBody(HttpState state, HttpConnection conn)Read the response body from the givenHttpConnection.protected voidreadResponseHeaders(HttpState state, HttpConnection conn)Reads the response headers from the givenconnection.protected voidreadStatusLine(HttpState state, HttpConnection conn)voidrecycle()Deprecated.no longer supported and will be removed in the future version of HttpClientvoidreleaseConnection()Releases the connection being used by this HTTP method.voidremoveRequestHeader(String headerName)Remove the request header associated with the given name.voidremoveRequestHeader(Header header)Removes the given request header.protected voidresponseBodyConsumed()A response has been consumed.protected voidsetConnectionCloseForced(boolean b)Sets whether or not the connection should be force-closed when no longer needed.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.)voidsetHostConfiguration(HostConfiguration hostconfig)Deprecated.no longer applicablevoidsetHttp11(boolean http11)Deprecated.voidsetMethodRetryHandler(MethodRetryHandler handler)Deprecated.useHttpMethodParamsvoidsetParams(HttpMethodParams params)AssignsHTTP protocol parametersfor this method.voidsetPath(String path)Sets the path of the HTTP method.voidsetQueryString(String queryString)Sets the query string of this HTTP method.voidsetQueryString(NameValuePair[] params)Sets the query string of this HTTP method.voidsetRequestHeader(String headerName, String headerValue)Set the specified request header, overwriting any previous value.voidsetRequestHeader(Header header)Sets the specified request header, overwriting any previous value.protected voidsetResponseStream(InputStream responseStream)Sets the response stream.voidsetStrictMode(boolean strictMode)Deprecated.UseHttpParams.setParameter(String, Object)to exercise a more granular control over HTTP protocol strictness.voidsetURI(URI uri)Sets the URI for this method.protected booleanshouldCloseConnection(HttpConnection conn)Tests if the connection should be closed after the method has been executed.booleanvalidate()Returns true the method is ready to execute, false otherwise.protected voidwriteRequest(HttpState state, HttpConnection conn)Sends the request via the givenconnection.protected booleanwriteRequestBody(HttpState state, HttpConnection conn)Writes the request body to the givenconnection.protected voidwriteRequestHeaders(HttpState state, HttpConnection conn)Writes the request headers to the givenconnection.protected voidwriteRequestLine(HttpState state, HttpConnection conn)Writes the request line to the givenconnection.
-
-
-
Field Detail
-
statusLine
protected StatusLine statusLine
The Status-Line from the response.
-
effectiveVersion
protected HttpVersion effectiveVersion
HTTP protocol version used for execution of this method.
-
-
Constructor Detail
-
HttpMethodBase
public HttpMethodBase()
No-arg constructor.
-
HttpMethodBase
public HttpMethodBase(String uri) throws IllegalArgumentException, IllegalStateException
Constructor specifying a URI. It is responsibility of the caller to ensure that URI elements (path & query parameters) are properly encoded (URL safe).- Parameters:
uri- either an absolute or relative URI. The URI is expected to be URL-encoded- Throws:
IllegalArgumentException- when URI is invalidIllegalStateException- when protocol of the absolute URI is not recognised
-
-
Method Detail
-
getName
public abstract String getName()
Obtains the name of the HTTP method as used in the HTTP request line, for example "GET" or "POST".- Specified by:
getNamein interfaceHttpMethod- Returns:
- the name of this method
-
getURI
public URI getURI() throws URIException
Returns the URI of the HTTP method- Specified by:
getURIin interfaceHttpMethod- Returns:
- The URI
- Throws:
URIException- If the URI cannot be created.- See Also:
HttpMethod.getURI()
-
setURI
public void setURI(URI uri) throws URIException
Sets the URI for this method.- Specified by:
setURIin interfaceHttpMethod- Parameters:
uri- URI to be set- Throws:
URIException- if a URI cannot be set- Since:
- 3.0
-
setFollowRedirects
public void setFollowRedirects(boolean followRedirects)
Sets whether or not the HTTP method should automatically follow HTTP redirects (status code 302, etc.)- Specified by:
setFollowRedirectsin interfaceHttpMethod- Parameters:
followRedirects- true if the method will automatically follow redirects, false otherwise.
-
getFollowRedirects
public boolean getFollowRedirects()
Returns true if the HTTP method should automatically follow HTTP redirects (status code 302, etc.), false otherwise.- Specified by:
getFollowRedirectsin interfaceHttpMethod- Returns:
- true if the method will automatically follow HTTP redirects, false otherwise.
-
setHttp11
public void setHttp11(boolean http11)
Deprecated.Sets whether version 1.1 of the HTTP protocol should be used per default.- Parameters:
http11- true to use HTTP/1.1, false to use 1.0
-
getDoAuthentication
public boolean getDoAuthentication()
Returns true if the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.), false otherwise- Specified by:
getDoAuthenticationin interfaceHttpMethod- Returns:
- true if authentication challenges will be processed automatically, false otherwise.
- Since:
- 2.0
- See Also:
HttpMethod.setDoAuthentication(boolean)
-
setDoAuthentication
public void setDoAuthentication(boolean doAuthentication)
Sets whether or not the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.)- Specified by:
setDoAuthenticationin interfaceHttpMethod- Parameters:
doAuthentication- true to process authentication challenges authomatically, false otherwise.- Since:
- 2.0
- See Also:
HttpMethod.getDoAuthentication()
-
isHttp11
public boolean isHttp11()
Deprecated.Returns true if version 1.1 of the HTTP protocol should be used per default, false if version 1.0 should be used.- Returns:
- true to use HTTP/1.1, false to use 1.0
-
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).- Specified by:
setPathin interfaceHttpMethod- Parameters:
path- the path of the HTTP method. The path is expected to be URL-encoded
-
addRequestHeader
public void addRequestHeader(Header header)
Adds the specified request header, NOT overwriting any previous value. Note that header-name matching is case insensitive.- Specified by:
addRequestHeaderin interfaceHttpMethod- Parameters:
header- the header to add to the request- See Also:
HttpMethod.addRequestHeader(String,String),HttpMethod.getRequestHeader(String),HttpMethod.removeRequestHeader(String)
-
addResponseFooter
public void addResponseFooter(Header footer)
Use this method internally to add footers.- Specified by:
addResponseFooterin interfaceHttpMethod- Parameters:
footer- The footer to add.
-
getPath
public String getPath()
Gets the path of this 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.- Specified by:
getPathin interfaceHttpMethod- Returns:
- the path to request or "/" if the path is blank.
-
setQueryString
public void setQueryString(String queryString)
Sets the query string of this HTTP method. The caller must ensure that the string is properly URL encoded. The query string should not start with the question mark character.- Specified by:
setQueryStringin interfaceHttpMethod- Parameters:
queryString- the query string- See Also:
EncodingUtil.formUrlEncode(NameValuePair[], String)
-
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.- Specified by:
setQueryStringin interfaceHttpMethod- Parameters:
params- an array ofNameValuePairs to add as query string parameters. The name/value pairs will be automcatically URL encoded- See Also:
EncodingUtil.formUrlEncode(NameValuePair[], String),setQueryString(String)
-
getQueryString
public String getQueryString()
Gets the query string of this HTTP method.- Specified by:
getQueryStringin interfaceHttpMethod- Returns:
- The query string
- See Also:
HttpMethod.setQueryString(NameValuePair[]),HttpMethod.setQueryString(String)
-
setRequestHeader
public void setRequestHeader(String headerName, String headerValue)
Set the specified request header, overwriting any previous value. Note that header-name matching is case-insensitive.- Specified by:
setRequestHeaderin interfaceHttpMethod- Parameters:
headerName- the header's nameheaderValue- the header's value- See Also:
HttpMethod.setRequestHeader(Header),HttpMethod.getRequestHeader(String),HttpMethod.removeRequestHeader(String)
-
setRequestHeader
public void setRequestHeader(Header header)
Sets the specified request header, overwriting any previous value. Note that header-name matching is case insensitive.- Specified by:
setRequestHeaderin interfaceHttpMethod- Parameters:
header- the header- See Also:
HttpMethod.setRequestHeader(String,String),HttpMethod.getRequestHeader(String),HttpMethod.removeRequestHeader(String)
-
getRequestHeader
public Header getRequestHeader(String headerName)
Returns the specified request header. Note that header-name matching is case insensitive. null will be returned if either headerName is null or there is no matching header for headerName.- Specified by:
getRequestHeaderin interfaceHttpMethod- Parameters:
headerName- The name of the header to be returned.- Returns:
- The specified request header.
- Since:
- 3.0
-
getRequestHeaders
public Header[] getRequestHeaders()
Returns an array of the requests headers that the HTTP method currently has- Specified by:
getRequestHeadersin interfaceHttpMethod- Returns:
- an array of my request headers.
- See Also:
HttpMethod.addRequestHeader(Header),HttpMethod.addRequestHeader(String,String)
-
getRequestHeaders
public Header[] getRequestHeaders(String headerName)
Description copied from interface:HttpMethodReturns the request headers with the given name. Note that header-name matching is case insensitive.- Specified by:
getRequestHeadersin interfaceHttpMethod- Parameters:
headerName- the name of the headers to be returned.- Returns:
- an array of zero or more headers
- See Also:
HttpMethod.getRequestHeaders(java.lang.String)
-
getRequestHeaderGroup
protected HeaderGroup getRequestHeaderGroup()
Gets theheader groupstoring the request headers.- Returns:
- a HeaderGroup
- Since:
- 2.0beta1
-
getResponseTrailerHeaderGroup
protected HeaderGroup getResponseTrailerHeaderGroup()
Gets theheader groupstoring the response trailer headers as per RFC 2616 section 3.6.1.- Returns:
- a HeaderGroup
- Since:
- 2.0beta1
-
getResponseHeaderGroup
protected HeaderGroup getResponseHeaderGroup()
Gets theheader groupstoring the response headers.- Returns:
- a HeaderGroup
- Since:
- 2.0beta1
-
getResponseHeaders
public Header[] getResponseHeaders(String headerName)
Description copied from interface:HttpMethodReturns the response headers with the given name. Note that header-name matching is case insensitive.- Specified by:
getResponseHeadersin interfaceHttpMethod- Parameters:
headerName- the name of the headers to be returned.- Returns:
- an array of zero or more headers
- Since:
- 3.0
- See Also:
HttpMethod.getResponseHeaders(java.lang.String)
-
getStatusCode
public int getStatusCode()
Returns the response status code.- Specified by:
getStatusCodein interfaceHttpMethod- Returns:
- the status code associated with the latest response.
-
getStatusLine
public StatusLine getStatusLine()
Provides access to the response status line.- Specified by:
getStatusLinein interfaceHttpMethod- Returns:
- the status line object from the latest response.
- Since:
- 2.0
-
getResponseHeaders
public Header[] getResponseHeaders()
Returns an array of the response headers that the HTTP method currently has in the order in which they were read.- Specified by:
getResponseHeadersin interfaceHttpMethod- Returns:
- an array of response headers.
-
getResponseHeader
public Header getResponseHeader(String headerName)
Gets the response header associated with the given name. Header name matching is case insensitive. null will be returned if either headerName is null or there is no matching header for headerName.- Specified by:
getResponseHeaderin interfaceHttpMethod- Parameters:
headerName- the header name to match- Returns:
- the matching header
-
getResponseContentLength
public long getResponseContentLength()
Return the length (in bytes) of the response body, as specified in a Content-Length header.Return -1 when the content-length is unknown.
- Returns:
- content length, if Content-Length header is available. 0 indicates that the request has no body. If Content-Length header is not present, the method returns -1.
-
getResponseBody
public byte[] getResponseBody() throws IOExceptionReturns the response body of the HTTP method, if any, as an array of bytes. If response body is not available or cannot be read, returns null. 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.- Specified by:
getResponseBodyin interfaceHttpMethod- Returns:
- The response body.
- Throws:
IOException- If an I/O (transport) problem occurs while obtaining the response body.
-
getResponseBody
public byte[] getResponseBody(int maxlen) throws IOExceptionReturns the response body of the HTTP method, if any, as an array of bytes. If response body is not available or cannot be read, returns null. 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().- Parameters:
maxlen- the maximum content length to accept (number of bytes).- Returns:
- The response body.
- Throws:
IOException- If an I/O (transport) problem occurs while obtaining the response body.
-
getResponseBodyAsStream
public InputStream getResponseBodyAsStream() throws IOException
Returns the response body of the HTTP method, if any, as anInputStream. 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.- Specified by:
getResponseBodyAsStreamin interfaceHttpMethod- Returns:
- The response body or
null. - Throws:
IOException- If an I/O (transport) problem occurs while obtaining the response body.
-
getResponseBodyAsString
public String getResponseBodyAsString() throws IOException
Returns the response body of the HTTP method, if any, as aString. 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.- Specified by:
getResponseBodyAsStringin interfaceHttpMethod- Returns:
- The response body or
null. - Throws:
IOException- If an I/O (transport) problem occurs while obtaining the response body.
-
getResponseBodyAsString
public String getResponseBodyAsString(int maxlen) throws IOException
Returns the response body of the HTTP method, if any, as aString. 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().- Parameters:
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.- Returns:
- The response body or
null. - Throws:
IOException- If an I/O (transport) problem occurs while obtaining the response body.
-
getResponseFooters
public Header[] getResponseFooters()
Returns an array of the response footers that the HTTP method currently has in the order in which they were read.- Specified by:
getResponseFootersin interfaceHttpMethod- Returns:
- an array of footers
-
getResponseFooter
public Header getResponseFooter(String footerName)
Gets the response footer associated with the given name. Footer name matching is case insensitive. null will be returned if either footerName is null or there is no matching footer for footerName or there are no footers available. If there are multiple footers with the same name, there values will be combined with the ',' separator as specified by RFC2616.- Specified by:
getResponseFooterin interfaceHttpMethod- Parameters:
footerName- the footer name to match- Returns:
- the matching footer
-
setResponseStream
protected void setResponseStream(InputStream responseStream)
Sets the response stream.- Parameters:
responseStream- The new response stream.
-
getResponseStream
protected InputStream getResponseStream()
Returns a stream from which the body of the current response may be read. If the method has not yet been executed, ifresponseBodyConsumedhas been called, or if the stream returned by a previous call has been closed,nullwill be returned.- Returns:
- the current response stream
-
getStatusText
public String getStatusText()
Returns the status text (or "reason phrase") associated with the latest response.- Specified by:
getStatusTextin interfaceHttpMethod- Returns:
- The status text.
-
setStrictMode
public void setStrictMode(boolean strictMode)
Deprecated.UseHttpParams.setParameter(String, Object)to exercise a more granular control over HTTP protocol strictness.Defines how strictly HttpClient follows the HTTP protocol specification (RFC 2616 and other relevant RFCs). In the strict mode HttpClient 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.- Specified by:
setStrictModein interfaceHttpMethod- Parameters:
strictMode- true for strict mode, false otherwise- See Also:
HttpMethod.isStrictMode()
-
isStrictMode
public boolean isStrictMode()
Deprecated.UseHttpParams.setParameter(String, Object)to exercise a more granular control over HTTP protocol strictness.Description copied from interface:HttpMethodReturns the value of the strict mode flag.- Specified by:
isStrictModein interfaceHttpMethod- Returns:
- false
- See Also:
HttpMethod.setStrictMode(boolean)
-
addRequestHeader
public void addRequestHeader(String headerName, String headerValue)
Adds the specified request header, NOT overwriting any previous value. Note that header-name matching is case insensitive.- Specified by:
addRequestHeaderin interfaceHttpMethod- Parameters:
headerName- the header's nameheaderValue- the header's value- See Also:
HttpMethod.addRequestHeader(Header),HttpMethod.getRequestHeader(String),HttpMethod.removeRequestHeader(String)
-
isConnectionCloseForced
protected boolean isConnectionCloseForced()
Tests if the connection should be force-closed when no longer needed.- Returns:
trueif the connection must be closed
-
setConnectionCloseForced
protected void setConnectionCloseForced(boolean b)
Sets whether or not the connection should be force-closed when no longer needed. This value should only be set totruein abnormal circumstances, such as HTTP protocol violations.- Parameters:
b-trueif the connection must be closed,falseotherwise.
-
shouldCloseConnection
protected boolean shouldCloseConnection(HttpConnection conn)
Tests if the connection should be closed after the method has been executed. The connection will be left open when using HTTP/1.1 or if Connection: keep-alive header was sent.- Parameters:
conn- the connection in question- Returns:
- boolean true if we should close the connection.
-
execute
public int execute(HttpState state, HttpConnection conn) throws HttpException, IOException
Executes this method using the specifiedHttpConnectionandHttpState.- Specified by:
executein interfaceHttpMethod- Parameters:
state-stateinformation to associate with this request. Must be non-null.conn- theconnectionto used to execute this HTTP method. Must be non-null.- Returns:
- the integer status code if one was obtained, or -1
- Throws:
IOException- if an I/O (transport) error occursHttpException- if a protocol exception occurs.
-
abort
public void abort()
Aborts the execution of this method.- Specified by:
abortin interfaceHttpMethod- Since:
- 3.0
- See Also:
HttpMethod.execute(HttpState, HttpConnection)
-
hasBeenUsed
public boolean hasBeenUsed()
- Specified by:
hasBeenUsedin interfaceHttpMethod- Returns:
- true if the method has been executed, false otherwise
-
recycle
public void recycle()
Deprecated.no longer supported and will be removed in the future version of HttpClientRecycles 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.- Specified by:
recyclein interfaceHttpMethod- See Also:
releaseConnection()
-
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.- Specified by:
releaseConnectionin interfaceHttpMethod- Since:
- 2.0
-
removeRequestHeader
public void removeRequestHeader(String headerName)
Remove the request header associated with the given name. Note that header-name matching is case insensitive.- Specified by:
removeRequestHeaderin interfaceHttpMethod- Parameters:
headerName- the header name
-
removeRequestHeader
public void removeRequestHeader(Header header)
Removes the given request header.- Specified by:
removeRequestHeaderin interfaceHttpMethod- Parameters:
header- the header
-
validate
public boolean validate()
Returns true the method is ready to execute, false otherwise.- Specified by:
validatein interfaceHttpMethod- Returns:
- This implementation always returns true.
-
addCookieRequestHeader
protected void addCookieRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
Generates Cookie request headers for thosecookies that match the given host, port and path.- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.
-
addHostRequestHeader
protected void addHostRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
Generates Host request header, as long as no Host request header already exists.- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.
-
addProxyConnectionHeader
protected void addProxyConnectionHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
Generates Proxy-Connection: Keep-Alive request header when communicating via a proxy server.- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.
-
addRequestHeaders
protected void addRequestHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException
Generates all the required requestheaders to be submitted via the givenconnection.This implementation adds User-Agent, Host, Cookie, Authorization, Proxy-Authorization and Proxy-Connection headers, when appropriate.
Subclasses may want to override this method to to add additional headers, and may choose to invoke this implementation (via super) to add the "standard" headers.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.- See Also:
writeRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
-
addUserAgentRequestHeader
protected void addUserAgentRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
Generates default User-Agent request header, as long as no User-Agent request header already exists.- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.
-
checkNotUsed
protected void checkNotUsed() throws IllegalStateException- Throws:
IllegalStateException- if the method has been used and not recycled
-
checkUsed
protected void checkUsed() throws IllegalStateException- Throws:
IllegalStateException- if not used
-
generateRequestLine
protected static String generateRequestLine(HttpConnection connection, String name, String requestPath, String query, String version)
Generates HTTP request line according to the specified attributes.- Parameters:
connection- theconnectionused to execute this HTTP methodname- the method name generate a request forrequestPath- the path string for the requestquery- the query string for the requestversion- the protocol version to use (e.g. HTTP/1.0)- Returns:
- HTTP request line
-
processResponseBody
protected void processResponseBody(HttpState state, HttpConnection conn)
This method is invoked immediately afterreadResponseBody(HttpState,HttpConnection)and can be overridden by sub-classes in order to provide custom body processing.This implementation does nothing.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- See Also:
readResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection),readResponseBody(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
-
processResponseHeaders
protected void processResponseHeaders(HttpState state, HttpConnection conn)
This method is invoked immediately afterreadResponseHeaders(HttpState,HttpConnection)and can be overridden by sub-classes in order to provide custom response headers processing.This implementation will handle the Set-Cookie and Set-Cookie2 headers, if any, adding the relevant cookies to the given
HttpState.- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- See Also:
readResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection),readResponseHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
-
processCookieHeaders
protected void processCookieHeaders(CookieSpec parser, Header[] headers, HttpState state, HttpConnection conn)
This method processes the specified cookie headers. It is invoked from withinprocessResponseHeaders(HttpState,HttpConnection)- Parameters:
headers- cookieHeaders to be processedstate- thestateinformation associated with this HTTP methodconn- theconnectionused to execute this HTTP method
-
processStatusLine
protected void processStatusLine(HttpState state, HttpConnection conn)
This method is invoked immediately afterreadStatusLine(HttpState,HttpConnection)and can be overridden by sub-classes in order to provide custom response status line processing.- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- See Also:
readResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection),readStatusLine(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
-
readResponse
protected void readResponse(HttpState state, HttpConnection conn) throws IOException, HttpException
Reads the response from the givenconnection.The response is processed as the following sequence of actions:
-
readStatusLine(HttpState,HttpConnection)is invoked to read the request line. -
processStatusLine(HttpState,HttpConnection)is invoked, allowing the method to process the status line if desired. -
readResponseHeaders(HttpState,HttpConnection)is invoked to read the associated headers. -
processResponseHeaders(HttpState,HttpConnection)is invoked, allowing the method to process the headers if desired. -
readResponseBody(HttpState,HttpConnection)is invoked to read the associated body (if any). -
processResponseBody(HttpState,HttpConnection)is invoked, allowing the method to process the response body if desired.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.
-
-
readResponseBody
protected void readResponseBody(HttpState state, HttpConnection conn) throws IOException, HttpException
Read the response body from the givenHttpConnection.The current implementation wraps the socket level stream with an appropriate stream for the type of response (chunked, content-length, or auto-close). If there is no response body, the connection associated with the request will be returned to the connection manager.
Subclasses may want to override this method to to customize the processing.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.- See Also:
readResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection),processResponseBody(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
-
readResponseHeaders
protected void readResponseHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException
Reads the response headers from the givenconnection.Subclasses may want to override this method to to customize the processing.
"It must be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma." - HTTP/1.0 (4.3)
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.- See Also:
readResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection),processResponseHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
-
readStatusLine
protected void readStatusLine(HttpState state, HttpConnection conn) throws IOException, HttpException
Read the status line from the givenHttpConnection, setting mystatus codeandstatus text.Subclasses may want to override this method to to customize the processing.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.- See Also:
StatusLine
-
writeRequest
protected void writeRequest(HttpState state, HttpConnection conn) throws IOException, HttpException
Sends the request via the given
connection.The request is written as the following sequence of actions:
-
writeRequestLine(HttpState, HttpConnection)is invoked to write the request line. -
writeRequestHeaders(HttpState, HttpConnection)is invoked to write the associated headers. - \r\n is sent to close the head part of the request.
-
writeRequestBody(HttpState, HttpConnection)is invoked to write the body part of the request.
Subclasses may want to override one or more of the above methods to to customize the processing. (Or they may choose to override this method if dramatically different processing is required.)
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.
-
-
writeRequestBody
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException, HttpException
Writes the request body to the givenconnection.This method should return true if the request body was actually sent (or is empty), or false if it could not be sent for some reason.
This implementation writes nothing and returns true.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- Returns:
- true
- 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.
-
writeRequestHeaders
protected void writeRequestHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException
Writes the request headers to the givenconnection.This implementation invokes
addRequestHeaders(HttpState,HttpConnection), and then writes each header to the request stream.Subclasses may want to override this method to to customize the processing.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.- See Also:
addRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection),getRequestHeaders()
-
writeRequestLine
protected void writeRequestLine(HttpState state, HttpConnection conn) throws IOException, HttpException
Writes the request line to the givenconnection.Subclasses may want to override this method to to customize the processing.
- Parameters:
state- thestateinformation associated with this methodconn- theconnectionused to execute this HTTP method- 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.- See Also:
generateRequestLine(org.apache.commons.httpclient.HttpConnection, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
-
getParams
public HttpMethodParams getParams()
ReturnsHTTP protocol parametersassociated with this method.- Specified by:
getParamsin interfaceHttpMethod- Returns:
- HTTP parameters.
- Since:
- 3.0
- See Also:
HttpMethodParams
-
setParams
public void setParams(HttpMethodParams params)
AssignsHTTP protocol parametersfor this method.- Specified by:
setParamsin interfaceHttpMethod- Since:
- 3.0
- See Also:
HttpMethodParams
-
getEffectiveVersion
public HttpVersion getEffectiveVersion()
Returns the HTTP version used with this method (may be null if undefined, that is, the method has not been executed)- Returns:
- HTTP version.
- Since:
- 3.0
-
getProxyAuthenticationRealm
public String getProxyAuthenticationRealm()
Deprecated.use #getProxyAuthState()Returns proxy authentication realm, if it has been used during authentication process. Otherwise returns null.- Returns:
- proxy authentication realm
-
getAuthenticationRealm
public String getAuthenticationRealm()
Deprecated.use #getHostAuthState()Returns authentication realm, if it has been used during authentication process. Otherwise returns null.- Returns:
- authentication realm
-
getContentCharSet
protected String getContentCharSet(Header contentheader)
Returns the character set from the Content-Type header.- Parameters:
contentheader- The content header.- Returns:
- String The character set.
-
getRequestCharSet
public String getRequestCharSet()
Returns the character encoding of the request from the Content-Type header.- Returns:
- String The character set.
-
getResponseCharSet
public String getResponseCharSet()
Returns the character encoding of the response from the Content-Type header.- Returns:
- String The character set.
-
getRecoverableExceptionCount
public int getRecoverableExceptionCount()
Deprecated.no longer used Returns the number of "recoverable" exceptions thrown and handled, to allow for monitoring the quality of the connection.- Returns:
- The number of recoverable exceptions handled by the method.
-
responseBodyConsumed
protected void responseBodyConsumed()
A response has been consumed.The default behavior for this class is to check to see if the connection should be closed, and close if need be, and to ensure that the connection is returned to the connection manager - if and only if we are not still inside the execute call.
-
getHostConfiguration
public HostConfiguration getHostConfiguration()
Deprecated.no longer applicableReturns thehost configuration.- Specified by:
getHostConfigurationin interfaceHttpMethod- Returns:
- the host configuration
-
setHostConfiguration
public void setHostConfiguration(HostConfiguration hostconfig)
Deprecated.no longer applicableSets thehost configuration.- Parameters:
hostconfig- The hostConfiguration to set
-
getMethodRetryHandler
public MethodRetryHandler getMethodRetryHandler()
Deprecated.useHttpMethodParamsReturns theretry handlerfor this HTTP method- Returns:
- the methodRetryHandler
-
setMethodRetryHandler
public void setMethodRetryHandler(MethodRetryHandler handler)
Deprecated.useHttpMethodParamsSets theretry handlerfor this HTTP method- Parameters:
handler- the methodRetryHandler to use when this method executed
-
getHostAuthState
public AuthState getHostAuthState()
Returns the target hostauthentication state- Specified by:
getHostAuthStatein interfaceHttpMethod- Returns:
- host authentication state
- Since:
- 3.0
-
getProxyAuthState
public AuthState getProxyAuthState()
Returns the proxyauthentication state- Specified by:
getProxyAuthStatein interfaceHttpMethod- Returns:
- host authentication state
- Since:
- 3.0
-
isAborted
public boolean isAborted()
Tests whether the execution of this method has been aborted- Returns:
- true if the execution of this method has been aborted, false otherwise
- Since:
- 3.0
-
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.- Specified by:
isRequestSentin interfaceHttpMethod- Returns:
- true if the request has been sent, false otherwise
-
-