List of usage examples for java.net HttpURLConnection disconnect
public abstract void disconnect();
From source file:edu.stanford.muse.launcher.Main.java
private static boolean killRunningServer(String url) throws IOException { try {//from w w w . j a va 2 s .c om // attempt to fetch the page // throws a connect exception if the server is not even running // so catch it and return false String http = url + "/exit.jsp?message=Shutdown%20request%20from%20a%20different%20instance%20of%20Muse"; // version num spaces and brackets screw up the URL connection System.err.println("Sending a kill request to " + http); HttpURLConnection u = (HttpURLConnection) new URL(http).openConnection(); u.connect(); if (u.getResponseCode() == 200) { u.disconnect(); return true; } u.disconnect(); } catch (ConnectException ce) { } return false; }
From source file:com.cyphermessenger.client.SyncRequest.java
public static CypherSession userLogin(String username, byte[] serverPassword, byte[] localPassword) throws IOException, APIErrorException { String passwordHashEncoded = Utils.BASE64_URL.encode(serverPassword); HttpURLConnection conn = doRequest("login", null, new String[] { "username", "password" }, new String[] { username, passwordHashEncoded }); if (conn.getResponseCode() != 200) { throw new IOException("Server error"); }/*from w w w .java 2 s .c o m*/ InputStream in = conn.getInputStream(); JsonNode node = MAPPER.readTree(in); conn.disconnect(); int statusCode = node.get("status").asInt(); if (statusCode == StatusCode.OK) { long userID = node.get("userID").asLong(); ECKey key; try { key = Utils.decodeKey(node.get("publicKey").asText(), node.get("privateKey").asText(), localPassword); } catch (InvalidCipherTextException ex) { throw new RuntimeException(ex); } long keyTimestamp = node.get("keyTimestamp").asLong(); key.setTime(keyTimestamp); CypherUser newUser = new CypherUser(username, localPassword, serverPassword, userID, key, keyTimestamp); String sessionID = node.get("sessionID").asText(); return new CypherSession(newUser, sessionID); } else { throw new APIErrorException(statusCode); } }
From source file:cit360.sandbox.BackEndMenu.java
public static final void urltest() { URL url;/*from ww w .ja v a 2s . c om*/ HttpURLConnection urlConnection = null; try { url = new URL("http://marvelcomicsuniverse.com"); urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = urlConnection.getInputStream(); InputStreamReader isw = new InputStreamReader(in); int data = isw.read(); while (data != -1) { char current = (char) data; data = isw.read(); System.out.print(current); } } catch (Exception e) { e.printStackTrace(); } finally { try { urlConnection.disconnect(); } catch (Exception e) { e.printStackTrace(); //If you want further info on failure... } } }
From source file:edu.stanford.epadd.launcher.Main.java
private static boolean killRunningServer(String url) throws IOException { try {/* w w w . ja v a 2 s . c o m*/ // attempt to fetch the page // throws a connect exception if the server is not even running // so catch it and return false String http = url + "/exit.jsp?message=Shutdown%20request%20from%20a%20different%20instance%20of%20ePADD"; // version num spaces and brackets screw up the URL connection System.err.println("Sending a kill request to " + http); HttpURLConnection u = (HttpURLConnection) new URL(http).openConnection(); u.connect(); if (u.getResponseCode() == 200) { u.disconnect(); return true; } u.disconnect(); } catch (ConnectException ce) { } return false; }
From source file:OCRRestAPI.java
private static void DownloadConvertedFile(String outputFileUrl) throws IOException { URL downloadUrl = new URL(outputFileUrl); HttpURLConnection downloadConnection = (HttpURLConnection) downloadUrl.openConnection(); if (downloadConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inputStream = downloadConnection.getInputStream(); // opens an output stream to save into file FileOutputStream outputStream = new FileOutputStream("C:\\converted_file.doc"); int bytesRead = -1; byte[] buffer = new byte[4096]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); }//w ww . j av a 2 s . c om outputStream.close(); inputStream.close(); } downloadConnection.disconnect(); }
From source file:net.andylizi.colormotd.utils.AttributionUtil.java
private static String sendGet(String url, String param, String charset) { StringBuilder result = new StringBuilder(1024); BufferedReader in = null;//from w w w . j a va 2 s . co m try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); conn.setRequestProperty("User-Agent", "ColorMOTD/" + UUID.randomUUID()); conn.setRequestProperty("Accept-Charset", charset); conn.setUseCaches(true); conn.setConnectTimeout(2000); conn.setReadTimeout(3000); conn.connect(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.forName(charset)), 1024); String line; while ((line = in.readLine()) != null) { result.append(line); } conn.disconnect(); } catch (Exception e) { } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { } } return result.toString(); }
From source file:Main.java
static public JSONArray loadJSON(String url) { HttpURLConnection connection = null; JSONArray json = null;//from w w w .j a v a 2 s . co m InputStream is = null; try { connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(5000); is = new BufferedInputStream(connection.getInputStream()); json = new JSONArray(convertStreamToString(is)); } catch (IOException ioe) { ioe.printStackTrace(); } catch (JSONException je) { je.printStackTrace(); } finally { try { if (is != null) { is.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } if (connection != null) { connection.disconnect(); } } return json; }
From source file:lu.list.itis.dkd.aig.util.FusekiHttpHelper.java
/** * Create a new dataset/*w w w .jav a2s.c om*/ * * @param dataSetName * @throws IOException * @throws HttpException */ public static void createDataSet(@NonNull String dataSetName) throws IOException, HttpException { logger.info("create dataset: " + dataSetName); URL url = new URL(HOST + "/$/datasets"); final HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setUseCaches(false); httpConnection.setRequestMethod("POST"); httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // set content httpConnection.setDoOutput(true); final OutputStreamWriter out = new OutputStreamWriter(httpConnection.getOutputStream()); out.write("dbName=" + URLEncoder.encode(dataSetName, "UTF-8") + "&dbType=mem"); out.close(); // handle HTTP/HTTPS strange behaviour httpConnection.connect(); httpConnection.disconnect(); // handle response switch (httpConnection.getResponseCode()) { case HttpURLConnection.HTTP_OK: break; default: throw new HttpException( httpConnection.getResponseCode() + " message: " + httpConnection.getResponseMessage()); } }
From source file:core.Utility.java
public static ArrayList<String> getWikiText(String _word) throws MalformedURLException, IOException { ArrayList<String> _list = new ArrayList(); try {//w w w. java 2s . c om URL url = new URL("http://en.wiktionary.org/w/api.php" + "?action=parse" + "&prop=wikitext" + "&page=" + _word + "&format=xmlfm"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output, wikiText = ""; while ((output = br.readLine()) != null) { wikiText += output + "\n"; } conn.disconnect(); } catch (MalformedURLException e) { throw e; } catch (IOException e) { throw e; } return _list; }
From source file:net.andylizi.colormotd.anim.utils.AttributionUtil.java
private static String sendGet(String url, String param, String charset) { StringBuilder result = new StringBuilder(1024); BufferedReader in = null;/*from ww w .j a v a2 s. c o m*/ try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); conn.setRequestProperty("User-Agent", Main.getInstance().getDescription().getFullName()); conn.setRequestProperty("Accept-Charset", charset); conn.setUseCaches(true); conn.setConnectTimeout(2000); conn.setReadTimeout(3000); conn.connect(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.forName(charset)), 1024); String line; while ((line = in.readLine()) != null) { result.append(line); } conn.disconnect(); } catch (Exception e) { } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { } } return result.toString(); }