List of usage examples for java.net URLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:msearch.io.MSFilmlisteLesen.java
private boolean filmlisteDownload(String uurl, File nachDatei) { boolean ret = false; try {//from ww w.ja v a 2 s .co m URLConnection conn = new URL(uurl).openConnection(); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); conn.setRequestProperty("User-Agent", MSConfig.getUserAgent()); BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); FileOutputStream fOut; byte[] buffer = new byte[1024]; int n = 0; int count = 0; int countMax; if (uurl.endsWith(MSConst.FORMAT_XZ) || uurl.endsWith(MSConst.FORMAT_BZ2) || uurl.endsWith(MSConst.FORMAT_ZIP)) { countMax = 44; } else { countMax = 250; } fOut = new FileOutputStream(nachDatei); this.notifyProgress(uurl); while (!MSConfig.getStop() && (n = in.read(buffer)) != -1) { fOut.write(buffer, 0, n); ++count; if (count > countMax) { this.notifyProgress(uurl); count = 0; } } if (MSConfig.getStop()) { ret = false; } else { ret = true; } try { fOut.close(); in.close(); } catch (Exception e) { } } catch (Exception ex) { MSLog.fehlerMeldung(952163678, MSLog.FEHLER_ART_PROG, "MSearchIoXmlFilmlisteLesen.filmlisteDownload", ex); } return ret; }
From source file:de.mango.business.GoogleSearchProvider.java
public Vector<String> search(String query, int count, int page) { Vector<String> results = new Vector<String>(); // Prepare the query try {/*from ww w .jav a2s .c om*/ query = "http://ajax.googleapis.com/ajax/services/search/images?" + GoogleSearchProvider.searchArguments + this.hostLanguage + "&q=" + URLEncoder.encode( GoogleSearchProvider.restrictToOpenClipart ? query + GoogleSearchProvider.openClipart : query, "UTF-8") + "&start="; } catch (UnsupportedEncodingException e) { if (DEBUG) { Log.w(TAG, "Unsupported Encoding Exception:" + e.getMessage()); Log.w(TAG, Log.getStackTraceString(e)); } return results; } // start argument to pass to google int firstindex = count * page; // count of results to skip before adding them to the result array int skip = 0; // start indices > 56 are skipped by Google, so we // ask for results from 56, but skip the unwanted $skip indices if (firstindex > 63) return results; if (firstindex > 56) { skip = firstindex - 56; firstindex = 56; } boolean readMore = true; // do we need more queries and are they // possible? while (readMore) { // add start index to the query String currentQuery = query + firstindex; if (DEBUG) Log.d(TAG, "Searching: " + currentQuery); try { // prepare the connection URL url = new URL(currentQuery); URLConnection connection = url.openConnection(); connection.addRequestProperty("Referer", GoogleSearchProvider.refererUrl); connection.setConnectTimeout(2000); connection.setReadTimeout(2000); connection.connect(); // receive the results StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { builder.append(line); } // parse the results JSONObject json = new JSONObject(builder.toString()); int responseStatus = json.getInt("responseStatus"); if (responseStatus == 200)// successful search { json = json.getJSONObject("responseData"); JSONArray res = json.getJSONArray("results"); if (res.length() == 0) return results; String s; int limit = Math.min(res.length(), count - results.size() + skip); for (int i = skip; i < limit; i++) { s = res.getJSONObject(i).getString("unescapedUrl"); if (s != null) results.addElement(s); } // see if there are "more Results" JSONObject cursor = json.getJSONObject("cursor"); JSONArray pages = cursor.getJSONArray("pages"); int pageCount = pages.length(); int currentPageIndex = cursor.getInt("currentPageIndex"); this.moreResults = readMore = (pageCount - 1) > currentPageIndex; } else { if (DEBUG) Log.w(TAG, "Goole Search Error (Code " + responseStatus + "):" + json.getString("responseDetails")); this.moreResults = readMore = false;// prevent for (;;) loop // on errors } } catch (MalformedURLException e) { if (DEBUG) { Log.w(TAG, "MalformedURLException:" + e.getMessage()); Log.w(TAG, Log.getStackTraceString(e)); } this.moreResults = readMore = false; } catch (IOException e) { if (DEBUG) { Log.w(TAG, "IOException:" + e.getMessage()); Log.w(TAG, Log.getStackTraceString(e)); } this.moreResults = readMore = false; } catch (JSONException e) { if (DEBUG) { Log.w(TAG, "JSONException:" + e.getMessage()); Log.w(TAG, Log.getStackTraceString(e)); } this.moreResults = readMore = false; } // read more only if we can read more AND want to have more readMore = readMore && results.size() < count; if (readMore) { firstindex += 8; if (firstindex > 56)// the last pages always need to start // querying at index 56 (or google returns // errors) { skip = firstindex - 56; firstindex = 56; } } } return results; }
From source file:net.sf.taverna.t2.activities.wsdl.WSDLActivity.java
private void parseWSDL() throws ParserConfigurationException, WSDLException, IOException, SAXException, UnknownOperationException {/*from ww w.j av a 2 s . c om*/ String wsdlLocation = configurationBean.get("operation").get("wsdl").textValue(); URLConnection connection = null; try { URL wsdlURL = new URL(wsdlLocation); connection = wsdlURL.openConnection(); connection.setConnectTimeout(RemoteHealthChecker.getTimeoutInSeconds() * 1000); connection.connect(); } catch (MalformedURLException e) { throw new IOException("Malformed URL", e); } catch (SocketTimeoutException e) { throw new IOException("Timeout", e); } catch (IOException e) { throw e; } finally { if ((connection != null) && (connection.getInputStream() != null)) { connection.getInputStream().close(); } } parser = new WSDLParser(wsdlLocation); isWsrfService = parser.isWsrfService(); }
From source file:org.apache.eagle.security.hive.jobrunning.HiveJobFetchSpout.java
private boolean fetchRunningConfig(AppInfo appInfo, List<MRJob> mrJobs) { InputStream is = null;/*from www . j a v a 2s . c o m*/ for (MRJob mrJob : mrJobs) { String confURL = appInfo.getTrackingUrl() + Constants.MR_JOBS_URL + "/" + mrJob.getId() + "/" + Constants.MR_CONF_URL + "?" + Constants.ANONYMOUS_PARAMETER; try { LOG.info("fetch job conf from {}", confURL); final URLConnection connection = URLConnectionUtils.getConnection(confURL); connection.setRequestProperty(XML_HTTP_HEADER, XML_FORMAT); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); is = connection.getInputStream(); Map<String, String> hiveQueryLog = new HashMap<>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dt = db.parse(is); Element element = dt.getDocumentElement(); NodeList propertyList = element.getElementsByTagName("property"); int length = propertyList.getLength(); for (int i = 0; i < length; i++) { Node property = propertyList.item(i); String key = property.getChildNodes().item(0).getTextContent(); String value = property.getChildNodes().item(1).getTextContent(); hiveQueryLog.put(key, value); } if (hiveQueryLog.containsKey(Constants.HIVE_QUERY_STRING)) { collector.emit(new ValuesArray(appInfo.getUser(), mrJob.getId(), Constants.ResourceType.JOB_CONFIGURATION, hiveQueryLog), mrJob.getId()); } } catch (Exception e) { LOG.warn("fetch job conf from {} failed, {}", confURL, e); e.printStackTrace(); return false; } finally { Utils.closeInputStream(is); } } return true; }
From source file:com.romeikat.datamessie.core.base.service.download.AbstractDownloader.java
protected URLConnection getConnection(final String url) throws IOException { final URLConnection urlConnection = new URL(url).openConnection(); if (urlConnection instanceof HttpURLConnection) { final HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; httpUrlConnection.setInstanceFollowRedirects(true); }/* ww w .j a v a 2 s. c om*/ urlConnection.setConnectTimeout(timeout); urlConnection.setReadTimeout(timeout); urlConnection.setRequestProperty("User-Agent", userAgent); return urlConnection; }
From source file:org.apache.taverna.activities.wsdl.WSDLActivity.java
private void parseWSDL() throws ParserConfigurationException, WSDLException, IOException, SAXException, UnknownOperationException {//from w w w . j a v a2 s. c om URLConnection connection = null; try { URL wsdlURL = new URL(configurationBean.get("operation").get("wsdl").textValue()); connection = wsdlURL.openConnection(); connection.setConnectTimeout(RemoteHealthChecker.getTimeoutInSeconds() * 1000); connection.connect(); } catch (MalformedURLException e) { throw new IOException("Malformed URL", e); } catch (SocketTimeoutException e) { throw new IOException("Timeout", e); } catch (IOException e) { throw e; } finally { if ((connection != null) && (connection.getInputStream() != null)) { connection.getInputStream().close(); } } parser = new WSDLParser(configurationBean.get("operation").get("wsdl").textValue()); isWsrfService = parser.isWsrfService(); }
From source file:com.baidu.jprotobuf.rpc.client.SimpleHttpRequestExecutor.java
/** * Open an HttpURLConnection for the given remote invocation request. * @param config the HTTP invoker configuration that specifies the * target service/*from www .j av a2 s .c om*/ * @return the HttpURLConnection for the given request * @throws IOException if thrown by I/O methods * @see java.net.URL#openConnection() */ protected HttpURLConnection openConnection(IDLHttpClientInvoker invoker) throws IOException { URLConnection con = new URL(invoker.getServiceUrl()).openConnection(); if (!(con instanceof HttpURLConnection)) { throw new IOException("Service URL [" + invoker.getServiceUrl() + "] is not an HTTP URL"); } if (invoker.getConnectTimeout() > 0) { con.setConnectTimeout(invoker.getConnectTimeout()); } if (invoker.getReadTimeout() > 0) { con.setReadTimeout(invoker.getReadTimeout()); } return (HttpURLConnection) con; }
From source file:game.Clue.JerseyClient.java
public void sendPUT(String name) { System.out.println("SendPUT method called"); try {/*from w w w.j ava 2 s. com*/ JSONObject jsonObject = new JSONObject("{name:" + name + "}"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/player1"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //setDoOutput(true); connection.setRequestProperty("PUT", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(jsonObject.toString()); System.out.println("Sent PUT message to server"); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:game.Clue.JerseyClient.java
public void sendPOST() { System.out.println("POST method called"); try {/*from www .j ava 2 s . co m*/ JSONObject jsonObject = new JSONObject("{player:Brian}"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //setDoOutput(true); connection.setRequestProperty("POST", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); System.out.println(jsonObject.toString()); out.write("{" + jsonObject.toString()); System.out.println("Sent PUT message to server"); out.close(); } catch (Exception e) { System.out.println("\nError while calling REST POST Service"); System.out.println(e); } }
From source file:org.y20k.transistor.helpers.MetadataHelper.java
private void shoutcastProxyReaderLoop(Socket proxy, URLConnection connection) throws IOException { connection.setConnectTimeout(5000); connection.setReadTimeout(5000);/* ww w.j ava2 s. c o m*/ connection.setRequestProperty("Icy-MetaData", "1"); connection.connect(); InputStream in = connection.getInputStream(); OutputStream out = proxy.getOutputStream(); out.write(("HTTP/1.0 200 OK\r\n" + "Pragma: no-cache\r\n" + "Content-Type: " + connection.getContentType() + "\r\n\r\n").getBytes(StandardCharsets.UTF_8)); byte buf[] = new byte[16384]; // one second of 128kbit stream int count = 0; int total = 0; int metadataSize = 0; final int metadataOffset = connection.getHeaderFieldInt("icy-metaint", 0); int bitRate = Math.max(connection.getHeaderFieldInt("icy-br", 128), 32); LogHelper.v(LOG_TAG, "createProxyConnection: connected, icy-metaint " + metadataOffset + " icy-br " + bitRate); while (true) { count = Math.min(in.available(), buf.length); if (count <= 0) { count = Math.min(bitRate * 64, buf.length); // buffer half-second of stream data } if (metadataOffset > 0) { count = Math.min(count, metadataOffset - total); } count = in.read(buf, 0, count); if (count == 0) { continue; } if (count < 0) { break; } out.write(buf, 0, count); total += count; if (metadataOffset > 0 && total >= metadataOffset) { // read metadata total = 0; count = in.read(); if (count < 0) { break; } count *= 16; metadataSize = count; if (metadataSize == 0) { continue; } // maximum metadata length is 4080 bytes total = 0; while (total < metadataSize) { count = in.read(buf, total, count); if (count < 0) { break; } if (count == 0) { continue; } total += count; count = metadataSize - total; } total = 0; String[] metadata = new String(buf, 0, metadataSize, StandardCharsets.UTF_8).split(";"); for (String s : metadata) { if (s.indexOf(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER) == 0 && s.length() >= TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length() + 1) { handleMetadataString( s.substring(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length(), s.length() - 1)); // break; } } } } }