Example usage for java.net HttpURLConnection getDate

List of usage examples for java.net HttpURLConnection getDate

Introduction

In this page you can find the example usage for java.net HttpURLConnection getDate.

Prototype

public long getDate() 

Source Link

Document

Returns the value of the date header field.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    long date = httpCon.getDate();
    if (date == 0)
        System.out.println("No date information.");
    else//  ww  w .ja  va 2 s.  com
        System.out.println("Date: " + new Date(date));

}

From source file:com.wanikani.wklib.Connection.java

protected Response call(Meter meter, String resource, boolean isArray, String arg, CacheInfo cinfo)
        throws IOException {
    HttpURLConnection conn;
    JSONTokener tok;/*from   w  ww .j av  a2  s  .com*/
    InputStream is;
    URL url;

    url = new URL(makeURL(resource, arg));
    conn = null;
    tok = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        if (cinfo != null) {
            if (cinfo.etag != null)
                conn.setRequestProperty("If-None-Match", cinfo.etag);
            else if (cinfo.modified != null)
                conn.setIfModifiedSince(cinfo.modified.getTime());
        }
        setTimeouts(conn);
        conn.connect();
        if (cinfo != null && cinfo.hasData() && conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED)
            throw new NotModifiedException();
        measureHeaders(meter, conn, false);
        is = conn.getInputStream();
        tok = new JSONTokener(readStream(meter, is));
    } finally {
        if (conn != null)
            conn.disconnect();
    }

    if (cinfo != null) {
        cinfo.modified = new Date();
        if (conn.getDate() > 0)
            cinfo.modified = new Date(conn.getDate());
        if (conn.getLastModified() > 0)
            cinfo.modified = new Date(conn.getLastModified());

        cinfo.etag = conn.getHeaderField("ETag");
    }

    try {
        return new Response(new JSONObject(tok), isArray);
    } catch (JSONException e) {
        throw new ParseException();
    }
}

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Rcupere la resource via un HttpConnection
 *
 * @param resource/*from www  .j a  v  a2s  . co  m*/
 * @return
 * @throws MalformedURLException
 * @throws IOException
 */
protected HttpURLConnection getConnectionForResource(String resource)
        throws MalformedURLException, IOException {
    StringBuilder sb = new StringBuilder("http://localhost:");
    sb.append(PORT).append(Constants.SLASH).append(CTXPATH).append(Constants.SLASH).append(resource);
    URL url = new URL(sb.toString());
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    //      Authenticator.setDefault(new Authenticator() {
    //         @Override
    //         protected PasswordAuthentication getPasswordAuthentication() {
    //            return new PasswordAuthentication("demo", "demo".toCharArray());
    //         }
    //      });
    //    ou
    //      String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary("demo:demo".getBytes());
    //      System.out.println(basicAuth);
    //      uc.setRequestProperty("Authorization", basicAuth);
    System.out.println("Content-type: " + uc.getContentType());
    System.out.println("Date: " + new Date(uc.getDate()));
    System.out.println("Last modified: " + new Date(uc.getLastModified()));
    System.out.println("Expiration date: " + new Date(uc.getExpiration()));
    System.out.println("Response code: " + uc.getResponseCode());
    assertThat(uc.getResponseCode()).isEqualTo(200).as("'%s' is unreachable", sb);
    return uc;
}

From source file:edu.umn.cs.spatialHadoop.nasa.HTTPFileSystem.java

/**
 * Returns the status of a file. This method is designed specifically to work
 * with LP DAAC archive and will not work correctly with other web sites.
 * Since HTTP does not tell whether a URL points to a file or directory,
 * we assume that URLs ending with HDF, XML and JPG are files while anything
 * else is considered a directory./*  w  w  w  .j  a va 2  s .co  m*/
 */
@Override
public FileStatus getFileStatus(Path f) throws IOException {
    f = f.makeQualified(this);
    URL url = f.toUri().toURL();
    int retryCount = HTTPFileSystem.retries;

    HttpURLConnection connection = null;
    try {
        while (connection == null && retryCount-- > 0) {
            try {
                connection = (HttpURLConnection) url.openConnection();
            } catch (java.net.SocketException e) {
                if (retryCount == 0)
                    throw e;
                LOG.info("Error accessing file '" + url + "'. Trials left: " + retryCount);
                try {
                    ;
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
            } catch (java.net.UnknownHostException e) {
                if (retryCount == 0)
                    throw e;
                LOG.info("Error accessing file '" + url + "'. Trials left: " + retryCount);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
            }
        }

        if (connection == null)
            throw new RuntimeException("Could not connect to " + f);
        String lengthStr = connection.getHeaderField("content-Length");
        long length = lengthStr == null ? -1 : Long.parseLong(lengthStr);
        if (length == -1)
            LOG.info("Unknown HTTP file length " + length);
        long modificationTime = connection.getLastModified();
        if (modificationTime == 0)
            modificationTime = connection.getDate();
        // Hard coded to work with LP DAAC archives
        boolean isdir = !f.getName().matches("(?i:([^*\\?])*\\.(hdf|xml|jpg|gz|bz2|zip|txt|csv|tsv)$)");
        return new FileStatus(length, isdir, 1, BLOCK_SIZE, modificationTime, 0, null, null, null, f);
    } finally {
        if (connection != null)
            connection.disconnect();
    }
}

From source file:easyshop.downloadhelper.HttpPageGetter.java

public HttpPage getDHttpPage(PageRef url, String charSet) {
    count++;/*from  www.  j a  v  a  2  s  .c  o m*/
    log.debug("getURL(" + count + ")");

    if (url.getUrlStr() == null) {
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new OriHttpPage(-1, null, null, null, conRes, null);
    }
    URL requestedURL = null;
    try {
        requestedURL = new URL(url.getUrlStr());
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        log.error("wrong urlstr" + url.getUrlStr());
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new OriHttpPage(-1, null, null, null, conRes, null);
    }
    ;
    //        System.out.println(""+requestedURL.toExternalForm());
    URL referer = null;
    try {
        log.debug("Creating HTTP connection to " + requestedURL);
        HttpURLConnection conn = (HttpURLConnection) requestedURL.openConnection();
        if (referer != null) {
            log.debug("Setting Referer header to " + referer);
            conn.setRequestProperty("Referer", referer.toExternalForm());
        }

        if (userAgent != null) {
            log.debug("Setting User-Agent to " + userAgent);
            conn.setRequestProperty("User-Agent", userAgent);
        }

        //            DateFormat dateFormat=DateFormat.getDateInstance();
        //            conn.setRequestProperty("If-Modlfied-Since",dateFormat.parse("2005-08-15 20:18:30").toGMTString());
        conn.setUseCaches(false);

        //            conn.setRequestProperty("connection","keep-alive");
        for (Iterator it = conn.getRequestProperties().keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            if (key == null) {
                break;
            }
            String value = conn.getHeaderField(key);
            //                System.out.println("Request header " + key + ": " + value);
        }
        log.debug("Opening URL");
        long startTime = System.currentTimeMillis();
        conn.connect();

        String resp = conn.getResponseMessage();
        log.debug("Remote server response: " + resp);

        int code = conn.getResponseCode();

        if (code != 200) {
            log.error("Could not get connection for code=" + code);
            System.err.println("Could not get connection for code=" + code);
            ConnResponse conRes = new ConnResponse(null, null, 0, 0, code);
            return new HttpPage(requestedURL.toExternalForm(), null, conRes, null);
        }

        //            if (conn.getContentLength()<=0||conn.getContentLength()>10000000){
        //               log.error("Content length==0");
        //               System.err.println("Content length==0");
        //               ConnResponse conRes=new ConnResponse(null,null,null,0,0,-100);
        //               return new URLObject(-1,requestedURL, null,null,conRes);
        //            }

        String respStr = conn.getHeaderField(0);
        long serverDate = conn.getDate();
        //            log.info("Server response: " + respStr);

        for (int i = 1; i < conn.getHeaderFields().size(); i++) {
            String key = conn.getHeaderFieldKey(i);
            if (key == null) {
                break;
            }
            String value = conn.getHeaderField(key);
            //                System.out.println("Received header " + key + ": " + value);
            //               log.debug("Received header " + key + ": " + value);
        }

        //            log.debug("Getting buffered input stream from remote connection");
        log.debug("start download(" + count + ")");
        BufferedInputStream remoteBIS = new BufferedInputStream(conn.getInputStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
        byte[] buf = new byte[1024];
        int bytesRead = 0;
        while (bytesRead >= 0) {
            baos.write(buf, 0, bytesRead);
            bytesRead = remoteBIS.read(buf);
        }
        //            baos.write(remoteBIS.read(new byte[conn.getContentLength()]));
        //            remoteBIS.close();
        byte[] content = baos.toByteArray();
        long timeTaken = System.currentTimeMillis() - startTime;
        if (timeTaken < 100)
            timeTaken = 500;

        int bytesPerSec = (int) ((double) content.length / ((double) timeTaken / 1000.0));
        //            log.info("Downloaded " + content.length + " bytes, " + bytesPerSec + " bytes/sec");
        if (content.length < conn.getContentLength()) {
            log.warn("Didn't download full content for URL: " + url);
            //                failureCount++;
            ConnResponse conRes = new ConnResponse(conn.getContentType(), null, content.length, serverDate,
                    code);
            return new HttpPage(requestedURL.toExternalForm(), null, conRes, conn.getContentType());
        }

        log.debug("download(" + count + ")");

        ConnResponse conRes = new ConnResponse(conn.getContentType(), null, conn.getContentLength(), serverDate,
                code);
        String c = charSet;
        if (c == null)
            c = conRes.getCharSet();
        HttpPage obj = new HttpPage(requestedURL.toExternalForm(), content, conRes, c);
        return obj;
    } catch (IOException ioe) {
        log.warn("Caught IO Exception: " + ioe.getMessage(), ioe);
        failureCount++;
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new HttpPage(requestedURL.toExternalForm(), null, conRes, null);
    } catch (Exception e) {
        log.warn("Caught Exception: " + e.getMessage(), e);
        failureCount++;
        ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0);
        return new HttpPage(requestedURL.toExternalForm(), null, conRes, null);
    }
}

From source file:com.cr_wd.android.network.HttpClient.java

/**
 * Performs a HTTP GET/POST Request/*from  w w w. j av  a 2  s  . c o  m*/
 * 
 * @return id of the request
 */
protected int doRequest(final Method method, final String url, HttpHeaders headers, HttpParams params,
        final HttpHandler handler) {
    if (headers == null) {
        headers = new HttpHeaders();
    }
    if (params == null) {
        params = new HttpParams();
    }

    handler.client = this;

    final int requestId = incrementRequestId();

    class HandlerRunnable extends Handler implements Runnable {

        private final Method method;
        private String url;
        private final HttpHeaders headers;
        private final HttpParams params;
        private final HttpHandler handler;
        private HttpResponse response;

        private boolean canceled = false;
        private int retries = 0;

        protected HandlerRunnable(final Method method, final String url, final HttpHeaders headers,
                final HttpParams params, final HttpHandler handler) {
            this.method = method;
            this.url = url;
            this.headers = headers;
            this.params = params;
            this.handler = handler;
        }

        @Override
        public void run() {
            execute();
        }

        private void execute() {
            response = new HttpResponse(requestId, method, url);

            HttpURLConnection conn = null;
            try {
                /* append query string for GET requests */
                if (method == Method.GET) {
                    if (!params.urlParams.isEmpty()) {
                        url += ('?' + params.getParamString());
                    }
                }

                /* setup headers for POST requests */
                if (method == Method.POST) {
                    headers.addHeader("Accept-Charset", requestOptions.encoding);
                    if (params.hasMultipartParams()) {
                        final SimpleMultipart multipart = params.getMultipart();
                        headers.addHeader("Content-Type", multipart.getContentType());
                    } else {
                        headers.addHeader("Content-Type",
                                "application/x-www-form-urlencoded;charset=" + requestOptions.encoding);
                    }
                }

                if (canceled) {
                    postCancel();
                    return;
                }

                /* open and configure the connection */
                conn = (HttpURLConnection) new URL(url).openConnection();

                postStart();

                if (method == Method.GET) {
                    conn = (HttpURLConnection) new URL(url).openConnection();
                    conn.setRequestMethod("GET");
                } else if (method == Method.POST) {
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);
                }
                conn.setAllowUserInteraction(false);
                conn.setReadTimeout(requestOptions.readTimeout);
                conn.setConnectTimeout(requestOptions.connectTimeout);

                /* add headers to the connection */
                for (final Map.Entry<String, List<String>> entry : headers.getHeaders().entrySet()) {
                    for (final String value : entry.getValue()) {
                        conn.addRequestProperty(entry.getKey(), value);
                    }
                }

                if (canceled) {
                    try {
                        conn.disconnect();
                    } catch (final Exception e) {
                    }
                    postCancel();
                    return;
                }

                response.requestProperties = conn.getRequestProperties();

                /* do post */
                if (method == Method.POST) {
                    InputStream is;

                    if (params.hasMultipartParams()) {
                        is = params.getMultipart().getContent();
                    } else {
                        is = new ByteArrayInputStream(params.getParamString().getBytes());
                    }

                    final OutputStream os = conn.getOutputStream();

                    writeStream(os, is);
                } else {
                    conn.connect();
                }

                if (canceled) {
                    try {
                        conn.disconnect();
                    } catch (final Exception e) {
                    }
                    postCancel();
                    return;
                }

                response.contentEncoding = conn.getContentEncoding();
                response.contentLength = conn.getContentLength();
                response.contentType = conn.getContentType();
                response.date = conn.getDate();
                response.expiration = conn.getExpiration();
                response.headerFields = conn.getHeaderFields();
                response.ifModifiedSince = conn.getIfModifiedSince();
                response.lastModified = conn.getLastModified();
                response.responseCode = conn.getResponseCode();
                response.responseMessage = conn.getResponseMessage();

                /* do get */
                if (conn.getResponseCode() < 400) {
                    response.responseBody = readStream(conn.getInputStream());
                    postSuccess();
                } else {
                    response.responseBody = readStream(conn.getErrorStream());
                    response.throwable = new Exception(response.responseMessage);
                    postError();
                }

            } catch (final Exception e) {
                if (retries < requestOptions.maxRetries) {
                    retries++;
                    postRetry();
                    execute();
                } else {
                    response.responseBody = e.getMessage();
                    response.throwable = e;
                    postError();
                }
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }
        }

        private String readStream(final InputStream is) throws IOException {
            final BufferedInputStream bis = new BufferedInputStream(is);
            final ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int read = 0;
            final byte[] buffer = new byte[8192];
            while (true) {
                if (canceled) {
                    break;
                }

                read = bis.read(buffer);
                if (read == -1) {
                    break;
                }
                baf.append(buffer, 0, read);
            }

            try {
                bis.close();
            } catch (final IOException e) {
            }

            try {
                is.close();
            } catch (final IOException e) {
            }

            return new String(baf.toByteArray());
        }

        private void writeStream(final OutputStream os, final InputStream is) throws IOException {
            final BufferedInputStream bis = new BufferedInputStream(is);
            int read = 0;
            final byte[] buffer = new byte[8192];
            while (true) {
                if (canceled) {
                    break;
                }

                read = bis.read(buffer);
                if (read == -1) {
                    break;
                }
                os.write(buffer, 0, read);
            }

            if (!canceled) {
                os.flush();
            }

            try {
                os.close();
            } catch (final IOException e) {
            }

            try {
                bis.close();
            } catch (final IOException e) {
            }

            try {
                is.close();
            } catch (final IOException e) {
            }
        }

        @Override
        public void handleMessage(final Message msg) {
            if (msg.what == HttpHandler.MESSAGE_CANCEL) {
                canceled = true;
            }
        }

        private void postSuccess() {
            postMessage(HttpHandler.MESSAGE_SUCCESS);
        }

        private void postError() {
            postMessage(HttpHandler.MESSAGE_ERROR);
        }

        private void postCancel() {
            postMessage(HttpHandler.MESSAGE_CANCEL);
        }

        private void postStart() {
            postMessage(HttpHandler.MESSAGE_START);
        }

        private void postRetry() {
            postMessage(HttpHandler.MESSAGE_RETRY);
        }

        private void postMessage(final int what) {
            final Message msg = handler.obtainMessage();
            msg.what = what;
            msg.arg1 = requestId;
            msg.obj = response;
            handler.sendMessage(msg);
        }
    }
    ;
    /* Create a new HandlerRunnable and start it */
    final HandlerRunnable hr = new HandlerRunnable(method, url, headers, params, handler);
    requests.put(requestId, new WeakReference<Handler>(hr));
    new Thread(hr).start();

    /* Return with the request id */
    return requestId;
}