List of usage examples for com.squareup.okhttp.internal.http HttpDate format
public static String format(Date value)
From source file:at.bitfire.dav4android.exception.ServiceUnavailableExceptionTest.java
License:Open Source License
public void testRetryAfter() { Response response = new Response.Builder() .request(new Request.Builder().url("http://www.example.com").get().build()) .protocol(Protocol.HTTP_1_1).code(503).build(); ServiceUnavailableException e = new ServiceUnavailableException(response); assertNull(e.retryAfter);/* www. ja v a 2 s . c o m*/ response = response.newBuilder().header("Retry-After", "120").build(); e = new ServiceUnavailableException(response); assertNotNull(e.retryAfter); assertTrue(withinTimeRange(e.retryAfter, 120)); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 30); response = response.newBuilder().header("Retry-After", HttpDate.format(cal.getTime())).build(); e = new ServiceUnavailableException(response); assertNotNull(e.retryAfter); assertTrue(withinTimeRange(e.retryAfter, 30 * 60)); }
From source file:de.schildbach.wallet.data.DynamicFeeLoader.java
License:Open Source License
private static void fetchDynamicFees(final HttpUrl url, final File tempFile, final File targetFile, final String userAgent) { final Stopwatch watch = Stopwatch.createStarted(); final Request.Builder request = new Request.Builder(); request.url(url);// w ww . j a v a2 s . c o m request.header("User-Agent", userAgent); if (targetFile.exists()) request.header("If-Modified-Since", HttpDate.format(new Date(targetFile.lastModified()))); final OkHttpClient httpClient = Constants.HTTP_CLIENT.clone(); httpClient.setConnectTimeout(5, TimeUnit.SECONDS); httpClient.setWriteTimeout(5, TimeUnit.SECONDS); httpClient.setReadTimeout(5, TimeUnit.SECONDS); final Call call = httpClient.newCall(request.build()); try { final Response response = call.execute(); final int status = response.code(); if (status == HttpURLConnection.HTTP_NOT_MODIFIED) { log.info("Dynamic fees not modified at {}, took {}", url, watch); } else if (status == HttpURLConnection.HTTP_OK) { final ResponseBody body = response.body(); final FileOutputStream os = new FileOutputStream(tempFile); Io.copy(body.byteStream(), os); os.close(); final Date lastModified = response.headers().getDate("Last-Modified"); if (lastModified != null) tempFile.setLastModified(lastModified.getTime()); body.close(); if (!tempFile.renameTo(targetFile)) throw new IllegalStateException("Cannot rename " + tempFile + " to " + targetFile); watch.stop(); log.info("Dynamic fees fetched from {}, took {}", url, watch); } else { log.warn("HTTP status {} when fetching dynamic fees from {}", response.code(), url); } } catch (final Exception x) { log.warn("Problem when fetching dynamic fees rates from " + url, x); } }
From source file:io.apiman.gateway.platforms.servlet.connectors.ok.HttpURLConnectionImpl.java
License:Apache License
@Override public void setIfModifiedSince(long newValue) { super.setIfModifiedSince(newValue); if (ifModifiedSince != 0) { requestHeaders.set("If-Modified-Since", HttpDate.format(new Date(ifModifiedSince))); } else {// ww w .j a v a2s.co m requestHeaders.removeAll("If-Modified-Since"); } }
From source file:net.yatomiya.e4.util.HttpUtils.java
License:Open Source License
public static String formatDate(Date date) { return HttpDate.format(date); }