List of usage examples for java.net URLConnection getReadTimeout
public int getReadTimeout()
From source file:Main.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); System.out.println(uc.getReadTimeout()); }
From source file:Main.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); uc.setReadTimeout(1000);/* w w w.ja va2 s. c om*/ System.out.println(uc.getReadTimeout()); }
From source file:mc.lib.network.NetworkHelper.java
public static void saveToFile(String url, File file) { InputStream is = null;/*from w w w. ja va 2 s . co m*/ OutputStream os = null; try { URLConnection c = new URL(url).openConnection(); c.connect(); is = c.getInputStream(); os = new FileOutputStream(file); StreamHelper.copyNetworkStream(is, os, c.getReadTimeout()); } catch (MalformedURLException e) { Log.e(LOGTAG, "Wrong url format", e); } catch (Exception e) { Log.e(LOGTAG, "Can not open stream", e); } finally { StreamHelper.close(os); StreamHelper.close(is); } }
From source file:mc.lib.network.NetworkHelper.java
public static String getAsText(String url, String encoding) { InputStream is = null;//from w w w .j a va 2 s . co m try { URLConnection c = new URL(url).openConnection(); c.connect(); is = c.getInputStream(); if (c.getContentEncoding() != null) encoding = c.getContentEncoding(); return StreamHelper.readNetworkStream(is, encoding, c.getReadTimeout()); } catch (MalformedURLException e) { Log.e(LOGTAG, "Wrong url format", e); } catch (Exception e) { Log.e(LOGTAG, "Cannot get text", e); } finally { StreamHelper.close(is); } return null; }
From source file:com.seleritycorp.common.base.coreservices.RawCoreServiceClient.java
private String getRawResponse(String rawRequest, int timeoutMillis) throws IOException { URLConnection connection = apiUrl.openConnection(); connection.setRequestProperty("Accept", "text/plain"); connection.setRequestProperty("Content-type", "application/json"); connection.setRequestProperty("User-Agent", client); connection.setDoOutput(true);//from w w w.ja va 2 s. co m connection.setReadTimeout(timeoutMillis); final OutputStream out = connection.getOutputStream(); out.write(rawRequest.getBytes(StandardCharsets.UTF_8)); out.flush(); out.close(); log.debug("wrote request " + rawRequest + " (timeout wanted: " + timeoutMillis + ", actual: " + connection.getReadTimeout() + ")"); String response = null; try (final InputStream in = connection.getInputStream()) { response = IOUtils.toString(in, StandardCharsets.UTF_8); } return response; }