public class HTTPClient
extends java.lang.Object
http://hc.apache.org/
Modifier and Type | Class and Description |
---|---|
static class |
HTTPClient.IdleConnectionMonitorThread |
Modifier and Type | Field and Description |
---|---|
private static HttpClientBuilder |
clientBuilder |
private static HTTPClient.IdleConnectionMonitorThread |
connectionMonitor |
private HttpUriRequest |
currentRequest |
private static int |
default_timeout |
private static RequestConfig |
dfltReqConf |
private static java.util.concurrent.ExecutorService |
executor |
private java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> |
headers |
private java.lang.String |
host |
private CloseableHttpResponse |
httpResponse |
private static int |
maxcon |
private RequestConfig.Builder |
reqConfBuilder |
private long |
timeout |
private long |
upbytes |
Constructor and Description |
---|
HTTPClient(ClientIdentification.Agent agent) |
HTTPClient(ClientIdentification.Agent agent,
int timeout) |
Modifier and Type | Method and Description |
---|---|
static void |
closeConnectionManager()
This method should be called just before shutdown
to stop the ConnectionManager and idledConnectionEvictor
|
private static ConnectionKeepAliveStrategy |
customKeepAliveStrategy()
If the Keep-Alive header is not present in the response,
HttpClient assumes the connection can be kept alive indefinitely.
|
private void |
execute(HttpUriRequest httpUriRequest,
boolean concurrent) |
void |
finish()
This method ensures correct finish of client-connections
This method should be used after every use of GET or POST and writeTo or getContentstream!
|
void |
GET(MultiProtocolURL url,
boolean concurrent)
This method GETs a page from the server.
|
void |
GET(java.lang.String uri,
boolean concurrent)
This method GETs a page from the server.
|
static byte[] |
getByteArray(HttpEntity entity,
int maxBytes)
Return entity content loaded as a byte array
|
byte[] |
GETbytes(MultiProtocolURL url,
java.lang.String username,
java.lang.String pass,
boolean concurrent)
This method GETs a page from the server.
|
byte[] |
GETbytes(MultiProtocolURL url,
java.lang.String username,
java.lang.String pass,
int maxBytes,
boolean concurrent)
This method GETs a page from the server.
|
byte[] |
GETbytes(java.lang.String uri,
java.lang.String username,
java.lang.String pass,
boolean concurrent)
This method GETs a page from the server.
|
byte[] |
GETbytes(java.lang.String uri,
java.lang.String username,
java.lang.String pass,
int maxBytes,
boolean concurrent)
This method GETs a page from the server.
|
private byte[] |
getContentBytes(HttpUriRequest httpUriRequest,
int maxBytes,
boolean concurrent) |
java.io.InputStream |
getContentstream()
This method gets direct access to the content-stream
Since this way is uncontrolled by the Client think of using 'writeTo' instead!
Please take care to call finish()!
|
HttpResponse |
getHttpResponse() |
private static SSLConnectionSocketFactory |
getSSLSocketFactory() |
int |
getStatusCode() |
HttpResponse |
HEADResponse(MultiProtocolURL url,
boolean concurrent)
This method gets HEAD response
|
HttpResponse |
HEADResponse(java.lang.String uri,
boolean concurrent)
This method gets HEAD response
|
private static HttpClientBuilder |
initClientBuilder() |
private static PoolingHttpClientConnectionManager |
initPoolingConnectionManager() |
private static RequestConfig |
initRequestConfig() |
static void |
main(java.lang.String[] args)
testing
|
void |
POST(MultiProtocolURL url,
java.io.InputStream instream,
long length,
boolean concurrent)
This method POSTs a page from the server.
|
byte[] |
POSTbytes(MultiProtocolURL url,
java.lang.String vhost,
java.util.Map<java.lang.String,ContentBody> post,
boolean usegzip,
boolean concurrent)
send data to the server named by vhost
|
static void |
setDefaultUserAgent(java.lang.String defaultAgent) |
void |
setHeader(java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> entrys)
This method sets the Header used for the request
|
private void |
setHeaders(HttpUriRequest httpUriRequest) |
void |
setHost(java.lang.String host)
This method sets the host to be called at the request
|
void |
setRedirecting(boolean redirecting)
This method sets if requests should follow redirects
|
void |
setTimout(int timeout)
This method sets the timeout of the Connection and Socket
|
void |
setUserAgent(ClientIdentification.Agent agent)
This method sets the UserAgent to be used for the request
|
private void |
storeConnectionInfo(HttpUriRequest httpUriRequest) |
void |
writeTo(java.io.OutputStream outputStream)
This method streams the content to the outputStream
Please take care to call finish()!
|
private static final int default_timeout
private static final int maxcon
private static HTTPClient.IdleConnectionMonitorThread connectionMonitor
private static final RequestConfig dfltReqConf
private static final HttpClientBuilder clientBuilder
private final RequestConfig.Builder reqConfBuilder
private java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> headers
private CloseableHttpResponse httpResponse
private HttpUriRequest currentRequest
private long upbytes
private java.lang.String host
private final long timeout
private static java.util.concurrent.ExecutorService executor
public HTTPClient(ClientIdentification.Agent agent)
public HTTPClient(ClientIdentification.Agent agent, int timeout)
public static void setDefaultUserAgent(java.lang.String defaultAgent)
private static RequestConfig initRequestConfig()
private static HttpClientBuilder initClientBuilder()
private static PoolingHttpClientConnectionManager initPoolingConnectionManager()
public static void closeConnectionManager() throws java.lang.InterruptedException
java.lang.InterruptedException
public void setHeader(java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> entrys)
entrys
- to be set as request headerpublic void setTimout(int timeout)
timeout
- in millisecondspublic void setUserAgent(ClientIdentification.Agent agent)
userAgent
- public void setHost(java.lang.String host)
host
- public void setRedirecting(boolean redirecting)
redirecting
- public byte[] GETbytes(java.lang.String uri, java.lang.String username, java.lang.String pass, boolean concurrent) throws java.io.IOException
uri
- the url to getjava.io.IOException
public byte[] GETbytes(MultiProtocolURL url, java.lang.String username, java.lang.String pass, boolean concurrent) throws java.io.IOException
uri
- the url to getjava.io.IOException
public byte[] GETbytes(java.lang.String uri, java.lang.String username, java.lang.String pass, int maxBytes, boolean concurrent) throws java.io.IOException
uri
- the url to getmaxBytes
- to getjava.io.IOException
public byte[] GETbytes(MultiProtocolURL url, java.lang.String username, java.lang.String pass, int maxBytes, boolean concurrent) throws java.io.IOException
uri
- the url to getmaxBytes
- to getjava.io.IOException
public void GET(java.lang.String uri, boolean concurrent) throws java.io.IOException
uri
- the url to getjava.io.IOException
public void GET(MultiProtocolURL url, boolean concurrent) throws java.io.IOException
url
- the url to getjava.io.IOException
public HttpResponse HEADResponse(java.lang.String uri, boolean concurrent) throws java.io.IOException
uri
- the url to Response fromjava.io.IOException
public HttpResponse HEADResponse(MultiProtocolURL url, boolean concurrent) throws java.io.IOException
url
- the url to Response fromjava.io.IOException
public void POST(MultiProtocolURL url, java.io.InputStream instream, long length, boolean concurrent) throws java.io.IOException
url
- the url to postinstream
- the input to postlength
- the contentlengthjava.io.IOException
public byte[] POSTbytes(MultiProtocolURL url, java.lang.String vhost, java.util.Map<java.lang.String,ContentBody> post, boolean usegzip, boolean concurrent) throws java.io.IOException
url
- address of the servervhost
- name of the server at address which should respondpost
- data to send (name-value-pairs)usegzip
- if the body should be gzippedjava.io.IOException
public HttpResponse getHttpResponse()
public int getStatusCode()
public java.io.InputStream getContentstream() throws java.io.IOException
java.io.IOException
public void writeTo(java.io.OutputStream outputStream) throws java.io.IOException
outputStream
- java.io.IOException
public void finish() throws java.io.IOException
java.io.IOException
private byte[] getContentBytes(HttpUriRequest httpUriRequest, int maxBytes, boolean concurrent) throws java.io.IOException
java.io.IOException
private void execute(HttpUriRequest httpUriRequest, boolean concurrent) throws java.io.IOException
java.io.IOException
public static byte[] getByteArray(HttpEntity entity, int maxBytes) throws java.io.IOException
entity
- HTTP entitymaxBytes
- maximum bytes to read. -1 means no maximum limit.java.io.IOException
- when a read error occured or content length is over maxBytesprivate void setHeaders(HttpUriRequest httpUriRequest)
private void storeConnectionInfo(HttpUriRequest httpUriRequest)
private static SSLConnectionSocketFactory getSSLSocketFactory()
private static ConnectionKeepAliveStrategy customKeepAliveStrategy()
defaultHttpClient
- public static void main(java.lang.String[] args)
args
- urls to test