I am trying to implement request retries after response isn't received.
I was reading about it in the Httpclient tutorial. the HttpRequestRetryHandler gets invoked only once and then it throw exception . what do I do wrong here?
updated
I added one if condition in the exception handling for SocketTimeoutException.
but what to do from there? how can I retry?
private void setRetry(int executionCount)
{
myRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception,int executionCount,HttpContext context) {
if (executionCount >= 4) {
// Do not retry if over max retry count
System.out.println("retry count");
return false;
}
if (exception instanceof NoHttpResponseException) {
System.out.println("NoHttpResponseException exception");// Retry if the server dropped connection on us
return true;
}
if (exception instanceof SSLHandshakeException) {
// Do not retry on SSL handshake exception
System.out.println("SSLHandshakeException exception");
return false;
}
if (exception instanceof java.net.SocketTimeoutException) {
// Do not retry on SSL handshake exception
System.out.println("java.net.SocketTimeoutException exception");
return false;
}
HttpRequest request = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST);
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
System.out.println("idempotent exception");
// Retry if the request is considered idempotent
return true;
}
return false;
}
};
}
public String postHttpReqest(int retries,
int socketTimeoutMillis,
int isSSL,
String target,
String url,
String base_url,
int port,
LinkedHashMap<String, String> lHashMapParams) throws Exception
{
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 50000);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
defaulthttpclient = new DefaultHttpClient(httpParams);
setRetry(retries); // here i set the handler
defaulthttpclient.setHttpRequestRetryHandler(myRetryHandler);
String line = "";
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
for (String key : lHashMapParams.keySet()) {
String val = lHashMapParams.get(key);
params.add(new BasicNameValuePair(key,val));
}
UrlEncodedFormEntity query = new UrlEncodedFormEntity(params);
HttpPost httppost = new HttpPost(url+":"+Integer.toString(port)+"//"+base_url);
httppost.setEntity(query);
HttpResponse response_ = defaulthttpclient.execute(httppost);
HttpEntity entity = response_.getEntity();
if (entity != null) {
line = EntityUtils.toString(entity);
System.out.println(line);
}
return line;
}
In the server, I set break point so it will hold the connection and will not return response.
The error I am getting in the Httpclient :
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:110)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:260)
at org.apache.http.impl.conn.LoggingSessionInputBuffer.readLine(LoggingSessionInputBuffer.java:115)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:633)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:454)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at com.fts.lb.connector.ut.HttpClientImpl.postHttpReqest(HttpClientImpl.java:183)
Um .. your RetryHandler is specifically returning false for a SocketTimeoutException.
So I would expect it to throw an Exception 100% of the time, which it is.
If you want the Request to be retried when the client times out before receiving a response, then you need to have your RetyrHandler return true for SocketTimeOutExceptions. You might also want to increase your SocketTimeOut before retrying.
Googlein a little bit I found this bug in the HttpClient 4.1 client (only on Windows).
For my problem I already use the latest 4.1.1, but maybe I have to implement the retryHandler.
I hope it will be helpful.
Related
RestTemplate example is below.
public class SimpleClient {
private final String URL;
private AsyncRestTemplate rest = new AsyncRestTemplate(new Netty4ClientHttpRequestFactory());
private RestTemplate restTemplate = new RestTemplate(new Netty4ClientHttpRequestFactory());
public SimpleClient(String url) {
this.URL = url;
Netty4ClientHttpRequestFactory nettyFactory = new Netty4ClientHttpRequestFactory();
try {
nettyFactory.setSslContext(SslContextBuilder.forClient().build());
} catch (SSLException e) {
e.printStackTrace();
}
rest = new AsyncRestTemplate(nettyFactory);
}
#Override
public ResponseEntity<ResponseData> doSendByPOST(RequestData data,Class<ResponseData> clazz) {
List<HttpMessageConverter<?>> messageConvertors = new ArrayList<>();
messageConvertors.add(new MappingJackson2HttpMessageConverter());
rest.setMessageConverters(messageConvertors);
restTemplate.setMessageConverters(messageConvertors);
HttpHeaders headers = new HttpHeaders();
ObjectMapper objectMapper = new ObjectMapper();
StringWriter writer = new StringWriter();
try {
objectMapper.writeValue(writer, data);
} catch (IOException e) {
e.printStackTrace();
}
headers.set(HttpHeaders.CONTENT_LENGTH,String.valueOf(writer.toString().getBytes(Charset.forName("UTF-8")).length));
headers.set(HttpHeaders.CONTENT_TYPE,"application/json");
HttpEntity<ResponseData> request = new HttpEntity<ResponseData>(headers);
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
try {
parts.add("requestData", objectMapper.writeValueAsString(data));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
// return restTemplate.exchange(this.URL,HttpMethod.POST ,request, clazz, parts);
ListenableFuture<ResponseEntity<ResponseData>> entity = rest.exchange(this.URL,HttpMethod.POST ,request, clazz, parts);
return extractResponseEntity(entity);
}
// ...
}
Netty read data from request channelRead method
#Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
DefaultHttpRequest defaultHttpRequest = (DefaultHttpRequest) msg;
if (EmptyHttpHeaders.is100ContinueExpected(defaultHttpRequest)) {
ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.CONTINUE));
}
boolean keepAlive = EmptyHttpHeaders.isKeepAlive(defaultHttpRequest);
handle = frontController.dispatchRequest(defaultHttpRequest);
}
if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
ByteArrayOutputStream body = new ByteArrayOutputStream(64);
ByteBuf content = httpContent.content();
if (content.isReadable()) {
//body.write(content.array());
content.readBytes(body,content.readableBytes());
//body.append(content.toString(CharsetUtil.UTF_8));
FullHttpResponse response = handle.handle(body);
if(response == null){
response = prepareDefaultResponse();
}
response.headers().set("content-type", "application/json");
response.headers().set("content-length", response.content().readableBytes());
response.headers().set("connection", HttpHeaderValues.KEEP_ALIVE);
}
if (msg instanceof LastHttpContent) {
//process request
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
The code below is working fine but I guess there is a problem with blocking io and nonblocking io. When the request is dispatched, I can not reach the HttpContent I only get HttpRequest as a msg parameter. Spring resttemplate waits for a response but Netty does not care :)
if (msg instanceof HttpRequest) {
DefaultHttpRequest defaultHttpRequest = (DefaultHttpRequest) msg;
if (EmptyHttpHeaders.is100ContinueExpected(defaultHttpRequest)) {
ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.CONTINUE));
}
boolean keepAlive = EmptyHttpHeaders.isKeepAlive(defaultHttpRequest);
handle = frontController.dispatchRequest(defaultHttpRequest);
}
My problem is how to get response from netty server by rest template.
I have tried many ways to accomplish full req/resp.
When restTemplate request to Netty server it hangs the thread so I can not move on the distributed in memory cache implementation.
Hanging in RestTemplate.java Line : 681
Method waits forever when using Netty4ClientHttpRequestFactory.
response = request.execute();
From my understanding, you read HTTP post request that from Rest Client as HttpRequest Object lets call it first case so that means you don't even branch on the if (msg instanceof HttpContent) {} case (second one) your HTTP server just writes the default response without any content or header that you're setting in the second case. If this is the cause for the blocking on the client side you have to fill that default response just like on the second case an see what client do.
I think netty API provides this
https://netty.io/4.1/api/io/netty/handler/codec/http/DefaultFullHttpResponse.html
Also this example could give you an idea of what could be wrong server side.
http://www.seepingmatter.com/2016/03/30/a-simple-standalone-http-server-with-netty.html
I'm using Apache HttpClient 4.3.6 and I'm having a hard time with it in one particular instance.
Normally, all of my HttpPost calls are in a single main thread and happen serially. However, there's one instance where once a particular type of HttpPost happens, I need to start a side thread to monitor it. This side thread periodically tries to connect to the server to see if it's still up. If the connection to the server drops, I need to abort() the connection back in main so that it doesn't just get stuck until the timeout happens (I can legitimately have an operation where it's possible to take 45 minutes to complete).
I set up a PoolingHttpClientConnectionManager and a CloseableHttpClient at the beginning of main, and then I have a send method that main and the side thread both use to set up HttpPost objects that are local to the method. However, the client.execute(httpPost) local variable result from the side thread instance of the send method is somehow getting assigned to the local variable result in the main thread instance of the send method. They're getting crossed. I've searched around for the last few hours and I haven't found any solutions to this.
EDIT: Added example code. Had to obfuscated company stuff by changing various things to basic variables where classes would be used, so forgive some typos/whatever during that.
Class variables:
CloseableHttpClient client;
HttpPost longHttpPost;
boolean diePlz = false;
In main:
clientPool = new PoolingHttpClientConnectionManager();
clientPool.setMaxTotal(20);
clientPool.setDefaultMaxPerRoute(20);
client = HttpClients.custom().setConnectionManager(clientPool).build();
//Lots of logic here to set up things to be 'send()'ed
Send method:
private String send(String message, boolean longCall){
RequestConfig.Builder requestConfig = RequestConfig.custom();
requestConfig.setConnectTimeout(10000);
String url = "http://" + ip + ":" + port + "/connector";
HttpPost httpPost;
if(longCall){
httpPost = longHttpPost = new HttpPost(url);
} else {
httpPost = new HttpPost(url);
}
httpPost.setConfig(requestConfig.build());
httpPost.setHeader("Content-Type", "application/xml");
httpPost.setHeader("charset", "UTF-8");
httpPost.setEntity(getEntity(message)); //getEntity() is a local method that overrides AbstractHttpEntity
try {
if(longCall && !threadRunning){
diePlz = false;
ConnectionCheck watcher = new ConnectionCheck();
watcher.start();
}
//httpReponse is what seems to get crossed between invocations of send()
CloseableHttpResponse httpResponse = client.execute(longCall ? longHttpPost : httpPost);
//more stuff here to work with response
} catch (Exception e) {
LOG.error(e, e);
return null;
} finally {
if(longCall){
longHttpPost.releaseConnection();
} else {
httpPost.releaseConnection();
}
}
}
Nested thread class inside main class:
private class ConnectionCheck extends Thread implements Runnable, Serializable {
#Override
public void run(){
threadRunning = true;
try{Thread.sleep(5000);}catch(Exception e){LOG.error(e, e);}
boolean registerReturn = register(false); //this calls send()
if(registerReturn){
String response = checkStatus(); //this calls send()
if(response == null){ //if the connection we're watching drops...
if(longHttpPost != null){
longHttpPost.abort(); //...then kill it
}
} else {
while(response != null && !diePlz){
try {
Thread.sleep(5000);
} catch (Exception e){
LOG.error(e, e);
register(true);
threadRunning = false;
return;
}
response = checkStatus();
if(response == null){
if(longHttpPost != null){
longHttpPost.abort();
}
}
}
register(true); //this calls send()
threadRunning = false;
}
} else {
register(true); //this calls send()
threadRunning = false;
}
}
}
I have a processor that reads messages from Apache Kafka and sends the data to a REST Endpoint.
The server only has 4 cores and 4 GB ram, out of which a max of 2GB is allocated to the java process
Messages are produced and consumed at the rate of 4k/second.
After running couple of minutes, the program goes out of memory.
What is the best way to call http rest end-points asynchronously and not wait for response
How to manage the httpClient connection? I was under the impression that I need to start the client an never close it so I can reuse the connection
Do you see any issues with the below code
public class SomeProcesor implements BProcessor {
private ThreadPoolExecutor exec = (ThreadPoolExecutor) Executors.newFixedThreadPool(4);
private CompletionService<Boolean> pool = new ExecutorCompletionService<Boolean>(exec);
CloseableHttpAsyncClient httpclient = null ;
#Override
public void begin() {
httpclient = HttpAsyncClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(5000).build();
HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build();
// Start the client
httpclient.start();
}
#Override
public void process(MessageAndMetadata<?, ?> mMData, List<Map> events) {
List<Map<String, Object>> listMap = new ArrayList<>();
// loop and extract the data from events into the above List
//..
//..
// submit to seperate thread to post to HTTP
pool.submit(new HttpThread(listMap);
}
private class HttpThread implements Callable<Boolean> {
List<Map<String, Object>> listMap = null;
public HttpThread(List<Map<String, Object>> listMap) {
this.listMap = listMap;
}
#Override
public Boolean call() throws Exception {
return postToHttp(listMap);
}
}
private Boolean postToHttp(List<Map<String, Object>> listMap) throws UnsupportedEncodingException {
for (Map<String, Object> map : listMap) {
try {
HttpPost postRequest = new HttpPost("https://myserver:8080/services/collector");
postRequest.addHeader(HttpHeaders.ACCEPT, "application/json");
postRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
postRequest.addHeader(HttpHeaders.CONNECTION, "keep-alive");
StringEntity input = new StringEntity(methodToConvertMapToJSON(map));
input.setContentType("application/json");
postRequest.setEntity(input);
httpclient.execute(postRequest, null);
} catch (Exception e) {
return false;
} catch (Throwable th) {
return false;
}
}
return true;
}
}
need consume the http response or release connection, otherwise the connection will consume resources.
change
httpclient.execute(postRequest, null);
to
HttpResponse response = httpclient.execute(postRequest, null).get();
if(response.getStatusLine().getStatusCode() != 200) {
// do something
}
// release the connection, better add to a finally clause
postRequest.releaseConnection();
When I execute an API through following method, I always get 404 as response code.
private void execute() throws IllegalStateException, IOException, NoSuchAlgorithmException {
Map<String, String> comment = new HashMap<String, String>();
comment.put("accounts-groups", "customers/enterprise");
comment.put("companyType", "customer");
comment.put("companyName", "Test");
String json = new GsonBuilder().create().toJson(comment, Map.class);
Log.i(TAG, "json : "+json);
HttpResponse response = makeRequest(URL, json);
/*Checking response */
if(response != null) {
InputStream inputStream = response.getEntity().getContent(); //Get the data in the entity
int statusCode = response.getStatusLine().getStatusCode();
Log.i(TAG, "statusCode : "+statusCode);
String result;
// convert inputstream to string
if(inputStream != null)
result = convertStreamToString(inputStream);
else
result = "Did not work!";
Log.i(TAG, "result : "+result);
}
}
private HttpResponse makeRequest(String uri, String json) throws NoSuchAlgorithmException {
Log.i(TAG, "uri : "+uri);
try {
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json, HTTP.UTF_8));
long timestamp = System.currentTimeMillis();
String signatureKey = PRIVATE_KEY + timestamp;
byte[] bytesOfMessage = signatureKey.getBytes(HTTP.UTF_8);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
char[] signature = Hex.encodeHex(thedigest);
String finalSignature = String.valueOf(signature);
Log.i(TAG, "finalSignature : "+finalSignature);
httpPost.setHeader("Timestamp", ""+timestamp);
httpPost.setHeader("Api_token", API_TOKEN);
httpPost.setHeader("Signature" , finalSignature);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I am not getting where am I going wrong. Can anybody please help me out?
from wiki:
The 404 or Not Found error message is a HTTP standard response code
indicating that the client was able to communicate with the server,
but the server could not find what was requested.
so, your code is OK, but server cannot find resource you are looking for. Double check if your url is correct.
how to pass request through fiddler proxy for debugging purposes:
HttpParams params = new BasicHttpParams();
// ....
HttpHost proxy = new HttpHost("192.168.1.12", 8888); // IP to your PC with fiddler proxy
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// use params as a second parameter to: following constructor:
// public DefaultHttpClient (ClientConnectionManager conman, HttpParams params)
I was getting 404 for POST requests because mod_headers module of Apache 2 server was not enabled. If that happens you can enable it with:
sudo a2enmod headers
and then restart apache:
sudo service apache2 restart
I have created the following function for checking the connection status:
private void checkConnectionStatus() {
HttpClient httpClient = new DefaultHttpClient();
try {
String url = "http://xxx.xxx.xxx.xxx:8000/GaitLink/"
+ strSessionString + "/ConnectionStatus";
Log.d("phobos", "performing get " + url);
HttpGet method = new HttpGet(new URI(url));
HttpResponse response = httpClient.execute(method);
if (response != null) {
String result = getResponse(response.getEntity());
...
When I shut down the server for testing the execution waits a long time at line
HttpResponse response = httpClient.execute(method);
Does anyone know how to set the timeout in order to avoid waiting too long?
Thanks!
In my example, two timeouts are set. The connection timeout throws java.net.SocketTimeoutException: Socket is not connected and the socket timeout java.net.SocketTimeoutException: The operation timed out.
HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
If you want to set the Parameters of any existing HTTPClient (e.g. DefaultHttpClient or AndroidHttpClient) you can use the function setParams().
httpClient.setParams(httpParameters);
To set settings on the client:
AndroidHttpClient client = AndroidHttpClient.newInstance("Awesome User Agent V/1.0");
HttpConnectionParams.setConnectionTimeout(client.getParams(), 3000);
HttpConnectionParams.setSoTimeout(client.getParams(), 5000);
I've used this successfully on JellyBean, but should also work for older platforms ....
HTH
If your are using Jakarta's http client library then you can do something like:
HttpClient client = new HttpClient();
client.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(5000));
client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, new Integer(5000));
GetMethod method = new GetMethod("http://www.yoururl.com");
method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(5000));
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
int statuscode = client.executeMethod(method);
If you're using the default http client, here's how to do it using the default http params:
HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);
Original credit goes to http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/
For those saying that the answer of #kuester2000 does not work, please be aware that HTTP requests, first try to find the host IP with a DNS request and then makes the actual HTTP request to the server, so you may also need to set a timeout for the DNS request.
If your code worked without the timeout for the DNS request it's because you are able to reach a DNS server or you are hitting the Android DNS cache. By the way you can clear this cache by restarting the device.
This code extends the original answer to include a manual DNS lookup with a custom timeout:
//Our objective
String sURL = "http://www.google.com/";
int DNSTimeout = 1000;
int HTTPTimeout = 2000;
//Get the IP of the Host
URL url= null;
try {
url = ResolveHostIP(sURL,DNSTimeout);
} catch (MalformedURLException e) {
Log.d("INFO",e.getMessage());
}
if(url==null){
//the DNS lookup timed out or failed.
}
//Build the request parameters
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, HTTPTimeout);
HttpConnectionParams.setSoTimeout(params, HTTPTimeout);
DefaultHttpClient client = new DefaultHttpClient(params);
HttpResponse httpResponse;
String text;
try {
//Execute the request (here it blocks the execution until finished or a timeout)
httpResponse = client.execute(new HttpGet(url.toString()));
} catch (IOException e) {
//If you hit this probably the connection timed out
Log.d("INFO",e.getMessage());
}
//If you get here everything went OK so check response code, body or whatever
Used method:
//Run the DNS lookup manually to be able to time it out.
public static URL ResolveHostIP (String sURL, int timeout) throws MalformedURLException {
URL url= new URL(sURL);
//Resolve the host IP on a new thread
DNSResolver dnsRes = new DNSResolver(url.getHost());
Thread t = new Thread(dnsRes);
t.start();
//Join the thread for some time
try {
t.join(timeout);
} catch (InterruptedException e) {
Log.d("DEBUG", "DNS lookup interrupted");
return null;
}
//get the IP of the host
InetAddress inetAddr = dnsRes.get();
if(inetAddr==null) {
Log.d("DEBUG", "DNS timed out.");
return null;
}
//rebuild the URL with the IP and return it
Log.d("DEBUG", "DNS solved.");
return new URL(url.getProtocol(),inetAddr.getHostAddress(),url.getPort(),url.getFile());
}
This class is from this blog post. Go and check the remarks if you will use it.
public static class DNSResolver implements Runnable {
private String domain;
private InetAddress inetAddr;
public DNSResolver(String domain) {
this.domain = domain;
}
public void run() {
try {
InetAddress addr = InetAddress.getByName(domain);
set(addr);
} catch (UnknownHostException e) {
}
}
public synchronized void set(InetAddress inetAddr) {
this.inetAddr = inetAddr;
}
public synchronized InetAddress get() {
return inetAddr;
}
}
An option is to use the OkHttp client, from Square.
Add the library dependency
In the build.gradle, include this line:
compile 'com.squareup.okhttp:okhttp:x.x.x'
Where x.x.x is the desired library version.
Set the client
For example, if you want to set a timeout of 60 seconds, do this way:
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
ps: If your minSdkVersion is greater than 8, you can use TimeUnit.MINUTES. So, you can simply use:
okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);
For more details about the units, see TimeUnit.
HttpParams httpParameters = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParameters,
HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(httpParameters, true);
// Set the timeout in milliseconds until a connection is
// established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 35 * 1000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 30 * 1000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
you can creat HttpClient instance by the way with Httpclient-android-4.3.5,it can work well.
SSLContext sslContext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCircularRedirectsAllowed(false).setConnectionRequestTimeout(30*1000).setConnectTimeout(30 * 1000).setMaxRedirects(10).setSocketTimeout(60 * 1000);
CloseableHttpClient hc = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(requestConfigBuilder.build()).build();
If you are using the HttpURLConnection, call setConnectTimeout() as described here:
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
public boolean isInternetWorking(){
try {
int timeOut = 5000;
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress("8.8.8.8",53);
socket.connect(socketAddress,timeOut);
socket.close();
return true;
} catch (IOException e) {
//silent
}
return false;
}