List of usage examples for java.net HttpURLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:integration.util.graylog.ServerHelper.java
public String getNodeId() throws MalformedURLException, URISyntaxException { final URI uri = IntegrationTestsConfig.getGlServerURL(); ObjectMapper mapper = new ObjectMapper(); try {/* w w w .java 2 s . c o m*/ HttpURLConnection connection = (HttpURLConnection) new URL(uri.toURL(), "/system").openConnection(); connection.setConnectTimeout(HTTP_TIMEOUT); connection.setReadTimeout(HTTP_TIMEOUT); connection.setRequestMethod("GET"); if (uri.getUserInfo() != null) { String encodedUserInfo = Base64 .encodeBase64String(uri.getUserInfo().getBytes(StandardCharsets.UTF_8)); connection.setRequestProperty("Authorization", "Basic " + encodedUserInfo); } InputStream inputStream = connection.getInputStream(); JsonNode json = mapper.readTree(inputStream); return json.get("node_id").toString(); } catch (IOException e) { e.printStackTrace(); } return "00000000-0000-0000-0000-000000000000"; }
From source file:com.ris.mobile.ecloud.util.AbFileUtil.java
/** * ????./*from w w w.j av a 2 s. c o m*/ * @param url ? * @return ?? */ public static String getRealFileNameFromUrl(String url) { String name = null; try { if (AbStrUtil.isEmpty(url)) { return name; } URL mUrl = new URL(url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", ""); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { for (int i = 0;; i++) { String mine = mHttpURLConnection.getHeaderField(i); if (mine == null) { break; } if ("content-disposition".equals(mHttpURLConnection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase()); if (m.find()) return m.group(1).replace("\"", ""); } } } } catch (Exception e) { e.printStackTrace(); // AbLogUtil.e(AbFileUtil.class, "???"); } return name; }
From source file:org.csware.ee.utils.Tools.java
public static String getSessionID(String path) { String[] sessionId = null;// ww w . j a v a 2 s . com try { // URL URL url = new URL(path); // HttpURLConnection HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); String session_value = urlConn.getHeaderField("Set-Cookie"); sessionId = session_value.split(";"); // urlConn.setConnectTimeout(5 * 1000); // urlConn.connect(); } catch (Exception ex) { ex.printStackTrace(); } return sessionId[0]; }
From source file:com.bnrc.util.AbFileUtil.java
/** * ????.//from w w w . j av a2s . c o m * @param url ??? * @return ?? */ public static String getRealFileNameFromUrl(String url) { String name = null; try { if (AbStrUtil.isEmpty(url)) { return name; } URL mUrl = new URL(url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", ""); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { for (int i = 0;; i++) { String mine = mHttpURLConnection.getHeaderField(i); if (mine == null) { break; } if ("content-disposition".equals(mHttpURLConnection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase()); if (m.find()) return m.group(1).replace("\"", ""); } } } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "??"); } return name; }
From source file:com.dv.Utils.DvFileUtil.java
/** * ????./*from w w w. j av a2 s . c o m*/ * * @param Url * @return int ? */ public static int getContentLengthFormUrl(String Url) { int mContentLength = 0; try { URL url = new URL(Url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) url.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", Url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { // ???? mContentLength = mHttpURLConnection.getContentLength(); } } catch (Exception e) { e.printStackTrace(); DvLogUtil.d(DvFileUtil.class, "?" + e.getMessage()); } return mContentLength; }
From source file:de.onelogic.android.weatherdemo.WeatherFetchTask.java
private String downloadUrl(String urlString) throws IOException { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true);// w w w.j a v a 2 s . c om // Starts the query conn.connect(); InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = null; StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); }
From source file:com.threewks.analytics.client.AnalyticsClient.java
/** * Post JSON a URL. Ignore (but log) any errors. * //from w w w. jav a 2s.c o m * @param url the URL to post the data to. * @param json the JSON data to post. */ void postJson(String url, String json) { PrintWriter writer = null; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setDoOutput(true); connection.setRequestProperty(API_KEY_HEADER, apiKey); connection.setRequestProperty(CONTENT_TYPE_HEADER, JSON_CONTENT_TYPE); writer = new PrintWriter(connection.getOutputStream(), true); writer.append(json); writer.close(); int responseCode = connection.getResponseCode(); if (responseCode != 200) { logger.warning(String.format( "Analytics server returned HTTP response code: %s when posting data to url: %s", responseCode, url)); } } catch (Exception e) { logger.warning(String.format("Error sending data to url: %s, error: %s", url, e.getMessage())); } finally { IOUtils.closeQuietly(writer); } }
From source file:com.dv.Utils.DvFileUtil.java
/** * ????./* w ww . jav a 2s . c om*/ * @param url ? * @return ?? */ public static String getRealFileNameFromUrl(String url) { String name = null; try { if (DvStrUtil.isEmpty(url)) { return name; } URL mUrl = new URL(url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", ""); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { for (int i = 0;; i++) { String mine = mHttpURLConnection.getHeaderField(i); if (mine == null) { break; } if ("content-disposition".equals(mHttpURLConnection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase()); if (m.find()) return m.group(1).replace("\"", ""); } } } } catch (Exception e) { e.printStackTrace(); DvLogUtil.e(DvFileUtil.class, "???"); } return name; }
From source file:com.orchestra.portale.external.services.manager.CiRoService.java
@Override public String getResponse(Map<String, String[]> mapParams) { try {/*from w w w . ja v a2s . co m*/ HttpURLConnection urlConnection = (HttpURLConnection) new URL(getUrl).openConnection(); urlConnection.setConnectTimeout(15000); urlConnection.setReadTimeout(30000); urlConnection.addRequestProperty("Accept-Language", Locale.getDefault().toString().replace('_', '-')); String result = IOUtils.toString(urlConnection.getInputStream()); urlConnection.disconnect(); return result; } catch (IOException e) { return "response{code:1,error:" + e.getMessage() + "}"; } }
From source file:eu.openanalytics.shinyproxy.ShinyProxyTestStrategy.java
@Override public boolean testProxy(Proxy proxy) { int totalWaitMs = Integer.parseInt(environment.getProperty("proxy.container-wait-time", "20000")); int waitMs = Math.min(2000, totalWaitMs); int maxTries = totalWaitMs / waitMs; int timeoutMs = Integer.parseInt(environment.getProperty("proxy.container-wait-timeout", "5000")); if (proxy.getTargets().isEmpty()) return false; URI targetURI = proxy.getTargets().values().iterator().next(); return retry(i -> { try {// w ww .j a v a 2 s. c om URL testURL = new URL(targetURI.toString()); HttpURLConnection connection = ((HttpURLConnection) testURL.openConnection()); connection.setConnectTimeout(timeoutMs); int responseCode = connection.getResponseCode(); if (responseCode == 200) return true; } catch (Exception e) { if (i > 1 && log != null) log.warn(String.format("Container unresponsive, trying again (%d/%d): %s", i, maxTries, targetURI)); } return false; }, maxTries, waitMs, false); }