List of usage examples for java.net URLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:org.voyanttools.trombone.input.source.UriInputSource.java
private URLConnection getURLConnection(URI uri, int readTimeoutMilliseconds, int connectTimeoutMilliseconds) throws IOException { URLConnection c; try {/* w w w. j a va2 s .c o m*/ c = uri.toURL().openConnection(); } catch (MalformedURLException e) { throw new IllegalArgumentException("Attempt to use a malformed URL: " + uri, e); } c.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; Trombone)"); c.setReadTimeout(readTimeoutMilliseconds); c.setConnectTimeout(connectTimeoutMilliseconds); return c; }
From source file:com.quartercode.quarterbukkit.api.query.ServerModsAPIQuery.java
/** * Executes the stored query and returns the result as a {@link JSONArray}. * The query string ({@link #getQuery()}) is attached to {@code https://api.curseforge.com/servermods/}. * The GET response of that {@link URL} is then parsed to a {@link JSONArray}. * //from ww w . jav a 2 s . c o m * @return The response of the server mods api. * @throws QueryException Something goes wrong while querying the server mods api. */ public JSONArray execute() throws QueryException { // Build request url using the query URL requestUrl = null; try { requestUrl = new URL(HOST + query); } catch (MalformedURLException e) { throw new QueryException(QueryExceptionType.MALFORMED_URL, this, HOST + query, e); } // Open connection to request url URLConnection request = null; try { request = requestUrl.openConnection(); } catch (IOException e) { throw new QueryException(QueryExceptionType.CANNOT_OPEN_CONNECTION, this, requestUrl.toExternalForm(), e); } // Set connection timeout request.setConnectTimeout(CONNECTION_TIMEOUT); // Set user agent request.addRequestProperty("User-Agent", USER_AGENT); // Set api key (if provided) String apiKey = QuarterBukkit.getPlugin().getConfig().getString("server-mods-api-key"); if (apiKey != null && !apiKey.isEmpty()) { request.addRequestProperty("X-API-Key", apiKey); } // We want to read the results request.setDoOutput(true); // Read first line from the response BufferedReader reader = null; String response = null; try { reader = new BufferedReader(new InputStreamReader(request.getInputStream())); response = reader.readLine(); } catch (IOException e) { if (e.getMessage().contains("HTTP response code: 403")) { throw new QueryException(QueryExceptionType.INVALID_API_KEY, this, requestUrl.toExternalForm(), e); } else { throw new QueryException(QueryExceptionType.CANNOT_READ_RESPONSE, this, requestUrl.toExternalForm(), e); } } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new QueryException(QueryExceptionType.CANNOT_CLOSE_RESPONSE_STREAM, this, requestUrl.toExternalForm(), e); } } } // Parse the response Object jsonResponse = JSONValue.parse(response); if (jsonResponse instanceof JSONArray) { return (JSONArray) jsonResponse; } else { throw new QueryException(QueryExceptionType.INVALID_RESPONSE, this, requestUrl.toExternalForm()); } }
From source file:game.Clue.JerseyClient.java
public void isgameReady() { //Check to see if all players have joined game try {//from www.ja v a 2 s.co m // URL url = new URL("http://192.168.1.7:8080/CluelessServer/webresources/service/game/Status"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/"); URLConnection connection = url.openConnection(); connection.setDoInput(true); //setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // while (in.readLine() != null) { //} System.out.println("\nGET Request for :" + "Game Ready Status? " + "Sent"); System.out.print(in.readLine()); //close connection in.close(); } catch (Exception e) { System.out.println("\nError while calling REST Get Service"); System.out.println(e); } }
From source file:org.mycore.datamodel.ifs.MCRAudioVideoExtender.java
/** * Helper method that connects to the given URL and returns the response as * a String//from ww w. j a v a 2s . c o m * * @param url * the URL to connect to * @return the response content as a String */ protected String getMetadata(String url) throws MCRPersistenceException { try { URLConnection connection = getConnection(url); connection.setConnectTimeout(getConnectTimeout()); String contentType = connection.getContentType(); Charset charset = StandardCharsets.ISO_8859_1; //defined by RFC 2616 (sec 3.7.1) if (contentType != null) { MediaType mediaType = MediaType.parse(contentType); mediaType.charset().or(StandardCharsets.ISO_8859_1); } ByteArrayOutputStream out = new ByteArrayOutputStream(1024); forwardData(connection, out); return new String(out.toByteArray(), charset); } catch (IOException exc) { String msg = "Could not get metadata from Audio/Video Store URL: " + url; throw new MCRPersistenceException(msg, exc); } }
From source file:game.Clue.JerseyClient.java
public void requestLogintoServer(String name) { try {/*from ww w. j a v a2 s .c o m*/ for (int i = 0; i < 6; i++) { JSONObject jsonObject = new JSONObject(name); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/player" + i); 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 for logging into server"); out.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:game.Clue.JerseyClient.java
public JSONObject getGameState() { JSONObject Object = null;//w w w . ja v a 2 s. co m try { URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/player1"); URLConnection connection = url.openConnection(); connection.setDoInput(true); //setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); Object = new JSONObject(in.readLine()); //; // JsonParser parser=new JsonParser(in.readLine()); // while (in.readLine() != null) { //} // System.out.print(in.readLine()); System.out.println(Object.getJSONArray("players").getJSONObject(0).getString("character")); System.out.println("\nREST Service Invoked Successfully..GET Request Sent"); //sendPUT(); //send JSON to Parser //Parser=new JsonParser(in.readLine()); //System.out.println("Parser called"); // sendPUT(); //close connection // in.close(); } catch (Exception e) { System.out.println("\nError while calling REST Service"); System.out.println(e); } return Object; }
From source file:com.twitter.hraven.rest.client.HRavenRestClient.java
public String getCluster(String hostname) throws IOException { String urlString = String.format("http://%s/api/v1/getCluster?hostname=%s", apiHostname, StringUtil.cleanseToken(hostname)); if (LOG.isInfoEnabled()) { LOG.info("Requesting cluster for " + hostname); }// w w w . ja v a2 s. c om URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.setConnectTimeout(this.connectTimeout); connection.setReadTimeout(this.readTimeout); InputStream input = connection.getInputStream(); java.util.Scanner s = new java.util.Scanner(input).useDelimiter("\\A"); String cluster = s.hasNext() ? s.next() : ""; try { input.close(); } catch (IOException ioe) { LOG.error("IOException in closing input stream, returning no error " + ioe.getMessage()); } return cluster; }
From source file:de.ifgi.lodum.sparqlfly.SparqlFly.java
/** * Returns jena model from URL// w ww. j a v a 2 s.com * @throws ParseException */ private Model getModelFromURL(String urlString, String format) throws Exception { Model m = ModelFactory.createDefaultModel(ReificationStyle.Standard); if (format.equals("HTML")) { StatementSink sink = new JenaStatementSink(m); XMLReader parser = ParserFactory.createReaderForFormat(sink, Format.HTML); parser.parse(urlString); } else { URL url = null; try { url = new URL(urlString); } catch (MalformedURLException e1) { e1.printStackTrace(); } URLConnection c = null; try { c = url.openConnection(); c.setConnectTimeout(10000); } catch (IOException e) { e.printStackTrace(); } BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(c.getInputStream(), Charset.forName("UTF-8"))); } catch (IOException e) { e.printStackTrace(); } m.read(in, "", format); try { in.close(); } catch (IOException e) { e.printStackTrace(); } RSIterator it = m.listReifiedStatements(); while (it.hasNext()) { m.add(it.nextRS().getStatement()); } } return m; }
From source file:com.moviejukebox.tools.WebBrowser.java
public URLConnection openProxiedConnection(URL url) throws IOException { URLConnection cnx = url.openConnection(YamjHttpClientBuilder.getProxy()); if (PROXY_USERNAME != null) { cnx.setRequestProperty("Proxy-Authorization", ENCODED_PASSWORD); }/* ww w .j a v a 2 s . c o m*/ cnx.setConnectTimeout(TIMEOUT_CONNECT); cnx.setReadTimeout(TIMEOUT_READ); return cnx; }
From source file:game.Clue.JerseyClient.java
public void getAvailableSlot() { try {/*from w w w. ja v a 2 s . c om*/ for (int i = 1; i < 7; i++) { // URL url = new URL("http://192.168.1.7:8080/CluelessServer/webresources/service/game/Status"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/player" + i + "/"); URLConnection connection = url.openConnection(); connection.setDoInput(true); //setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // while (in.readLine() != null) { //} System.out.println("\nGET Request for :" + "Get Available slot " + "Sent"); if ((in.readLine()).contains("no name")) { game_slot = i; i = 7; } else { } System.out.print("You have slot # " + game_slot); //close connection in.close(); } } catch (Exception e) { System.out.println("\nError slot taken..Checking for another open slot..."); System.out.println(e); } }