Example usage for java.net HttpURLConnection setUseCaches

List of usage examples for java.net HttpURLConnection setUseCaches

Introduction

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

Prototype

public void setUseCaches(boolean usecaches) 

Source Link

Document

Sets the value of the useCaches field of this URLConnection to the specified value.

Usage

From source file:com.jwrapper.maven.java.JavaDownloadMojo.java

protected HttpURLConnection connection(final String locationURL) throws Exception {

    final URL url = new URL(locationURL);

    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setUseCaches(false);
    connection.setConnectTimeout(10 * 1000);
    connection.setInstanceFollowRedirects(true);

    for (final Map.Entry<String, String> entry : headerMap.entrySet()) {
        connection.setRequestProperty(entry.getKey(), entry.getValue());
    }/*from  w  w w . ja v a2  s.  c  om*/

    connection.connect();

    return connection;

}

From source file:net.gcolin.simplerepo.test.AbstractRepoTest.java

protected int getStatus(String url, long lastUpdate) throws IOException {
    HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
    try {//from  www . j a v  a2s. c  o  m
        if (lastUpdate > 0) {
            c.setIfModifiedSince(lastUpdate);
        }
        c.setUseCaches(false);
        c.connect();
        return c.getResponseCode();
    } finally {
        c.disconnect();
    }
}

From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java

private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setConnectTimeout(request.getTimeOut());
    connection.setReadTimeout(request.getTimeOut());
    connection.setUseCaches(false);
    connection.setDoInput(true);//w w  w.  j a  v  a2  s .co  m

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    //        if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
    //            ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
    //        }

    return connection;
}

From source file:com.yahoo.druid.hadoop.DruidHelper.java

protected List<DataSegment> getSegmentsToLoad(String dataSource, Interval interval, String overlordUrl) {
    String urlStr = "http://" + overlordUrl + "/druid/indexer/v1/action";
    logger.info("Sending request to overlord at " + urlStr);

    String requestJson = getSegmentListUsedActionJson(dataSource, interval.toString());
    logger.info("request json is " + requestJson);

    int numTries = 3; //TODO: should be configurable?
    for (int trial = 0; trial < numTries; trial++) {
        try {/*w  ww .  jav a 2  s. co m*/
            logger.info("attempt number {} to get list of segments from overlord", trial);
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("content-type", "application/json");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setConnectTimeout(60000); //TODO: 60 secs, shud be configurable?
            OutputStream out = conn.getOutputStream();
            out.write(requestJson.getBytes());
            out.close();
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                ObjectMapper mapper = DruidInitialization.getInstance().getObjectMapper();
                Map<String, Object> obj = mapper.readValue(conn.getInputStream(),
                        new TypeReference<Map<String, Object>>() {
                        });
                return mapper.convertValue(obj.get("result"), new TypeReference<List<DataSegment>>() {
                });
            } else {
                logger.warn(
                        "Attempt Failed to get list of segments from overlord. response code {} , response {}",
                        responseCode, IOUtils.toString(conn.getInputStream()));
            }
        } catch (Exception ex) {
            logger.warn("Exception in getting list of segments from overlord", ex);
        }

        try {
            Thread.sleep(5000); //wait before next trial
        } catch (InterruptedException ex) {
            Throwables.propagate(ex);
        }
    }

    throw new RuntimeException(
            String.format("failed to find list of segments, dataSource[%s], interval[%s], overlord[%s]",
                    dataSource, interval, overlordUrl));
}

From source file:net.gcolin.simplerepo.test.AbstractRepoTest.java

protected String getContent(String url, long lastUpdate) throws IOException {
    HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
    try {/*  ww w.j a  va  2s  .  c  om*/
        if (lastUpdate > 0) {
            c.setIfModifiedSince(lastUpdate);
        }
        c.setUseCaches(false);
        c.connect();
        return c.getResponseCode() == HttpURLConnection.HTTP_OK
                ? new String(IOUtils.toByteArray(c.getInputStream()), "utf-8")
                : null;
    } finally {
        c.disconnect();
    }
}

From source file:com.snda.mymarket.providers.downloads.HurlStack.java

/**
 * Opens an {@link HttpURLConnection} with parameters.
 * @param url//from   w w  w. j  a  va 2  s. co  m
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(URL url, HttpUriRequest request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    int timeoutMs = TIMEOUT_MSECONDES;
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    return connection;
}

From source file:github.srlee309.lessWrongBookCreator.scraper.PostSectionExtractor.java

public boolean downloadFileFromURL(String fetchUrl, File outputFile)
        throws IOException, FileNotFoundException, IOException {

    HttpURLConnection c;

    //save file       
    URL url = new URL(fetchUrl);
    c = (HttpURLConnection) url.openConnection();

    //set cache and request method settings  
    c.setUseCaches(false);
    c.setDoOutput(false);/*from ww w  .j  av a  2  s  . co m*/

    //set other headers  
    c.setRequestProperty("Content-Type", "image/jpeg");
    System.out.println(c.getContentType());
    //connect  
    c.connect();

    BufferedInputStream in = new BufferedInputStream(c.getInputStream());

    OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
    byte[] buf = new byte[256];
    int n = 0;
    while ((n = in.read(buf)) >= 0) {
        out.write(buf, 0, n);
    }
    out.flush();
    out.close();

    return true;
}

From source file:net.terryyiu.emailservice.providers.AbstractEmailServiceProvider.java

private HttpURLConnection createConnection() throws IOException {
    URL url = new URL(getServiceUrl());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);/* www .j a  va2  s.  c  o m*/
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);

    modifyConnection(connection);

    return connection;
}

From source file:com.shopgun.android.sdk.network.impl.HttpURLNetwork.java

private HttpURLConnection openConnection(Request<?> request, URL url) throws IOException {

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setConnectTimeout(request.getTimeOut());
    connection.setReadTimeout(request.getTimeOut());
    connection.setUseCaches(false);
    connection.setDoInput(true);//from w w  w  . j  a va2s  .co m
    connection.setInstanceFollowRedirects(false);

    setHeaders(request, connection);
    setRequestMethod(connection, request);
    return connection;
}

From source file:com.jyzn.wifi.validate.platforminterface.SmsInterfaceImpl.java

@Override
public Map HttpSendSms(String postUrl, String postData) {
    String result = "";
    Map resultMap = Maps.newHashMap();
    try {//from  w  w w. j a va2 s .com
        //??POST
        URL url = new URL(postUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setUseCaches(false);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Length", "" + postData.length());

        try {
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();//?
        } catch (IOException e) {
        }

        //???
        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.out.println("connect failed!");
            resultMap.put("status", "fail");
            //return "fail";
        }

        //??
        String line;
        try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
        }
        resultMap.put("status", "sucess");
        resultMap.put("result", result);
    } catch (IOException e) {
        e.printStackTrace(System.out);
    }
    return resultMap;
}