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:jmc.util.UtlFbComents.java

public static String getJSONComentarios(String url, Long numComents) throws JMCException {
    String linea = "";
    String buf = "";

    try {/*from w w w.jav  a2s  .c  o m*/
        Properties props = ConfigPropiedades.getProperties("props_config.properties");
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL ur = new URL(
                "http://graph.facebook.com/comments?id=" + url + "&limit=" + numComents + "&filter=stream");
        HttpURLConnection cn = (HttpURLConnection) ur.openConnection();

        cn.setRequestProperty("user-agent", props.getProperty("navegador"));
        cn.setInstanceFollowRedirects(false);
        cn.setUseCaches(false);
        cn.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream()));

        while ((linea = br.readLine()) != null) {
            buf.concat(linea);
        }
        cn.disconnect();

    } catch (IOException e) {
        throw new JMCException(e);
    }
    return buf;
}

From source file:de.cubeisland.engine.core.util.McUUID.java

private static HttpURLConnection postQuery(ArrayNode node, int page) throws IOException {
    HttpURLConnection con = (HttpURLConnection) new URL(MOJANG_API_URL_NAME_UUID + page).openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");
    con.setUseCaches(false);
    con.setDoInput(true);//from  ww w .j a v a2s .  com
    con.setDoOutput(true);
    DataOutputStream writer = new DataOutputStream(con.getOutputStream());
    writer.write(node.toString().getBytes());
    writer.close();
    return con;
}

From source file:Main.java

/**
 * Given a string url, connects and returns response code
 *
 * @param urlString       string to fetch
 * @param network       network/*from w  w w  .  j  a  va2s  . c o m*/
 * @param readTimeOutMs       read time out
 * @param connectionTimeOutMs       connection time out
 * @param urlRedirect       should use urlRedirect
 * @param useCaches       should use cache
 * @return httpResponseCode http response code
 * @throws IOException
 */

@TargetApi(LOLLIPOP)
public static int checkUrlWithOptionsOverNetwork(String urlString, Network network, int readTimeOutMs,
        int connectionTimeOutMs, boolean urlRedirect, boolean useCaches) throws IOException {
    if (network == null) {
        return -1;
    }
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) network.openConnection(url);
    connection.setReadTimeout(readTimeOutMs /* milliseconds */);
    connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */);
    connection.setRequestMethod("GET");
    connection.setInstanceFollowRedirects(urlRedirect);
    connection.setUseCaches(useCaches);
    // Starts the query
    connection.connect();
    int responseCode = connection.getResponseCode();
    connection.disconnect();
    return responseCode;
}

From source file:jmc.util.UtlFbComents.java

public static void getURLComentarios(String url, Long numComents) throws JMCException {

    File f = null;/*from  w w w.j  a  v  a  2  s. c  o  m*/

    try {
        Properties props = ConfigPropiedades.getProperties("props_config.properties");
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL ur = new URL(
                "http://graph.facebook.com/comments?id=" + url + "&limit=" + numComents + "&filter=stream");
        HttpURLConnection cn = (HttpURLConnection) ur.openConnection();

        cn.setRequestProperty("user-agent", props.getProperty("navegador"));
        cn.setInstanceFollowRedirects(false);
        cn.setUseCaches(false);
        cn.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream()));
        String dirTempHtml = props.getProperty("archive_json") + "/";
        File fld = new File(dirTempHtml);

        boolean creado = fld.exists() ? true : fld.mkdir();

        if (creado) {
            String archFile = "archivo.json";
            String archivo = dirTempHtml + archFile;
            String linea = null;

            f = new File(archivo);
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            while ((linea = br.readLine()) != null) {
                bw.append(linea);
            }
            bw.close();

            cn.disconnect();

            List<Fbcoment> lfb = Convertidor.getFbcomentObjects(1l, archivo);
            // Fbcoment.actComents(idContenido, lfb);

        } else {
            cn.disconnect();
        }

    } catch (IOException e) {
        throw new JMCException(e);
    }
}

From source file:com.surfs.storage.common.util.HttpUtils.java

public static String invokeHttpForGet(String path, String... agrs) throws IOException {
    URL url = new URL(path);
    LogFactory.info("rest url:" + url.toString());
    HttpURLConnection con = null;
    try {//from  w  w  w  . j a  va 2  s .c om
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setConnectTimeout(10000);
        con.setReadTimeout(1000 * 60 * 30);
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(false);

        for (String string : agrs) {
            con.setRequestProperty("Content-type", "application/json");
            con.setRequestMethod("POST");
            OutputStream out = con.getOutputStream();
            out.write(string.getBytes("UTF-8"));
        }

        if (con.getResponseCode() != 200)
            throw new ConnectException(con.getResponseMessage());

        InputStream is = con.getInputStream();

        return readResponse(is);

    } catch (IOException e) {
        throw e;
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }
}

From source file:jmc.util.UtlFbComents.java

public static void getComentarios(Long idContenido, Long numComents) throws JMCException {

    File f = null;/*from w  ww  . jav a 2s.c  o  m*/

    try {
        Properties props = ConfigPropiedades.getProperties("props_config.properties");
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL ur = new URL("http://graph.facebook.com/comments?id="
                + Contenido.getContenido("Where cont_id = '" + idContenido + "'").get(0).getEnlaceURL()
                + "&limit=" + numComents + "&filter=stream");
        HttpURLConnection cn = (HttpURLConnection) ur.openConnection();

        cn.setRequestProperty("user-agent", props.getProperty("navegador"));
        cn.setInstanceFollowRedirects(false);
        cn.setUseCaches(false);
        cn.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream()));
        String dirTempHtml = props.getProperty("archive_json") + "/";
        File fld = new File(dirTempHtml);

        boolean creado = fld.exists() ? true : fld.mkdir();

        if (creado) {
            String archFile = idContenido + ".json";
            String archivo = dirTempHtml + archFile;
            String linea = null;

            f = new File(archivo);
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            while ((linea = br.readLine()) != null) {
                bw.append(linea);
            }
            bw.close();

            cn.disconnect();

            List<Fbcoment> lfb = Convertidor.getFbcomentObjects(idContenido, archivo);
            Fbcoment.actComents(idContenido, lfb);

        } else {
            cn.disconnect();
        }

    } catch (IOException e) {
        throw new JMCException(e);
    }
}

From source file:com.gmt2001.HttpRequest.java

public static HttpResponse getData(RequestType type, String url, String post, HashMap<String, String> headers) {
    Thread.setDefaultUncaughtExceptionHandler(com.gmt2001.UncaughtExceptionHandler.instance());

    HttpResponse r = new HttpResponse();

    r.type = type;//  www .  java 2  s  .  c om
    r.url = url;
    r.post = post;
    r.headers = headers;

    try {
        URL u = new URL(url);

        HttpURLConnection h = (HttpURLConnection) u.openConnection();

        for (Entry<String, String> e : headers.entrySet()) {
            h.addRequestProperty(e.getKey(), e.getValue());
        }

        h.setRequestMethod(type.name());
        h.setUseCaches(false);
        h.setDefaultUseCaches(false);
        h.setConnectTimeout(timeout);
        h.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015");
        if (!post.isEmpty()) {
            h.setDoOutput(true);
        }

        h.connect();

        if (!post.isEmpty()) {
            BufferedOutputStream stream = new BufferedOutputStream(h.getOutputStream());
            stream.write(post.getBytes());
            stream.flush();
            stream.close();
        }

        if (h.getResponseCode() < 400) {
            r.content = IOUtils.toString(new BufferedInputStream(h.getInputStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = true;
        } else {
            r.content = IOUtils.toString(new BufferedInputStream(h.getErrorStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = false;
        }
    } catch (IOException ex) {
        r.success = false;
        r.httpCode = 0;
        r.exception = ex.getMessage();

        com.gmt2001.Console.err.printStackTrace(ex);
    }

    return r;
}

From source file:och.util.NetUtil.java

public static HttpURLConnection openGetConn(String url, int connectTimeout, int readTimeout)
        throws IOException {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setConnectTimeout(connectTimeout);
    conn.setReadTimeout(readTimeout);/* w  w w  .  ja va2  s.  c  o m*/
    conn.setUseCaches(false);
    conn.setDoOutput(false);
    conn.setDoInput(true);
    conn.setRequestMethod("GET");
    return conn;
}

From source file:och.util.NetUtil.java

public static HttpURLConnection openPostConn(String url, int connTimeout, int readTimeout) throws IOException {

    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setConnectTimeout(connTimeout);
    conn.setReadTimeout(readTimeout);//from   w w  w .j a v  a 2s .co m
    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");

    String agent = System.getProperty("http.agent");
    if (hasText(agent)) {
        conn.setRequestProperty("User-Agent", agent);
    }

    return conn;
}

From source file:core.RESTCalls.RESTPost.java

public static String httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception {

    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");

    conn.setDoOutput(true);/*w w w  . ja va 2 s  . c  o m*/
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    OutputStream out = conn.getOutputStream();

    Writer writer = new OutputStreamWriter(out, "UTF-8");

    for (int i = 0; i < paramName.length; i++) {

        writer.write(paramName[i]);
        writer.write("=");
        writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
        writer.write("&");
    }

    writer.close();
    out.close();

    if (conn.getResponseCode() != 200)
        throw new IOException(conn.getResponseMessage());

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    StringBuilder sb = new StringBuilder();

    String line;

    while ((line = rd.readLine()) != null)
        sb.append(line + "\n");

    rd.close();

    conn.disconnect();

    return sb.toString();
}