List of usage examples for java.net HttpURLConnection disconnect
public abstract void disconnect();
From source file:mayo.edu.server.JsonServiceImpl.java
/** * Make a REST call to retrieve XML response * @param endpoint//from ww w . j a v a 2s . co m * @return */ private String makeRestCall(String endpoint) { HttpURLConnection request = null; BufferedReader rd = null; StringBuilder response = null; try { java.net.URL endpointUrl = new java.net.URL(endpoint); request = (java.net.HttpURLConnection) endpointUrl.openConnection(); request.setRequestMethod("GET"); rd = new java.io.BufferedReader(new java.io.InputStreamReader(request.getInputStream())); request.connect(); // read the response response = new StringBuilder(); String line = null; while ((line = rd.readLine()) != null) { response.append(line + '\n'); } // Finally, the connection is released and the response is processed request.disconnect(); rd.close(); System.out.println((response != null) ? response.toString() : "No Response"); } catch (MalformedURLException e) { System.out.println("Exception: " + e.getMessage()); // e.printStackTrace(); } catch (ProtocolException e) { System.out.println("Exception: " + e.getMessage()); // e.printStackTrace(); } catch (IOException e) { System.out.println("Exception: " + e.getMessage()); // e.printStackTrace(); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); // e.printStackTrace(); } finally { try { request.disconnect(); } catch (Exception e) { } if (rd != null) { try { rd.close(); } catch (IOException ex) { } rd = null; } } return response.toString(); }
From source file:export.GarminUploader.java
@Override public Status listWorkouts(List<Pair<String, String>> list) { Status s;//from w w w .j a v a 2s . c o m if ((s = connect()) != Status.OK) { return s; } HttpURLConnection conn = null; Exception ex = null; try { conn = (HttpURLConnection) new URL(LIST_WORKOUTS_URL).openConnection(); conn.setRequestMethod("GET"); addCookies(conn); conn.connect(); getCookies(conn); InputStream in = new BufferedInputStream(conn.getInputStream()); JSONObject obj = parse(in); conn.disconnect(); int responseCode = conn.getResponseCode(); String amsg = conn.getResponseMessage(); if (responseCode == 200) { obj = obj.getJSONObject("com.garmin.connect.workout.dto.BaseUserWorkoutListDto"); JSONArray arr = obj.getJSONArray("baseUserWorkouts"); for (int i = 0;; i++) { obj = arr.optJSONObject(i); if (obj == null) break; list.add(new Pair<String, String>(obj.getString("workoutId"), obj.getString("workoutName") + ".json")); } return Status.OK; } ex = new Exception(amsg); } catch (IOException e) { ex = e; } catch (JSONException e) { ex = e; } s = Uploader.Status.ERROR; s.ex = ex; if (ex != null) { ex.printStackTrace(); } return s; }
From source file:uk.ac.ed.portlets.almaportlet.service.AlmaReaderService.java
/** * Given userid and type of request (fine, loan, request) get xml from web service * @param userid user id/*from ww w .ja va 2s . c o m*/ * @param type type of request (fine, loan, request) * @return xml wraps the data */ private String readRestfulService(String userid, String type, String status) throws Exception { logger.debug("applicationKey --> " + applicationKey); logger.debug("almaBaseURL --> " + almaBaseURL); try { String endpoint = almaBaseURL + userid + "/" + type; logger.debug("call alma --> " + endpoint); StringBuilder urlBuilder = new StringBuilder(endpoint); urlBuilder.append("?"); urlBuilder.append(URLEncoder.encode("user_id_type", "UTF-8") + "=" + URLEncoder.encode("all_unique", "UTF-8") + "&"); urlBuilder .append(URLEncoder.encode("status", "UTF-8") + "=" + URLEncoder.encode(status, "UTF-8") + "&"); if (type.equals(TYPE_LOAN)) { urlBuilder.append( URLEncoder.encode("limit", "UTF-8") + "=" + URLEncoder.encode("100", "UTF-8") + "&"); } urlBuilder.append( URLEncoder.encode("apikey", "UTF-8") + "=" + URLEncoder.encode(applicationKey, "UTF-8")); URL url = new URL(urlBuilder.toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); logger.debug("Response code: " + conn.getResponseCode()); BufferedReader rd; if (conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) { rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); } else { rd = new BufferedReader(new InputStreamReader(conn.getErrorStream())); } StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); String result = sb.toString(); logger.debug("result --> " + result); if (type.equals(TYPE_FINE)) { if (result.contains("<fees")) { return result; } } else if (type.equals(TYPE_LOAN)) { if (result.contains("<item_loans")) { return result; } } else if (type.equals(TYPE_REQUEST)) { if (result.contains("<user_requests")) { return result; } } return ""; } catch (Exception e) { return ""; } }
From source file:com.commonsware.android.EMusicDownloader.SingleBook.java
private Bitmap getImageBitmap(String url) { Bitmap bm = null;//from ww w. j a v a 2 s . c om try { URL aURL = new URL(url); HttpURLConnection conn = (HttpURLConnection) aURL.openConnection(); long ifs = 0; ifs = conn.getContentLength(); if (ifs == -1) { conn.disconnect(); conn = (HttpURLConnection) aURL.openConnection(); ifs = conn.getContentLength(); } vArtExists = false; if (ifs > 0) { conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(bis); bis.close(); is.close(); vArtExists = true; } } catch (IOException e) { vArtExists = false; final IOException ef = e; final String urlf = url; } return bm; }
From source file:com.esri.geoevent.processor.reversegeocoder.ReverseGeocoderProcessor.java
private String getReverseGeocode(URL url) { String output = ""; try {//from w w w . j a v a 2 s. c o m HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { String errorString = "Failed : HTTP error code : " + conn.getResponseCode(); throw new RuntimeException(errorString); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String line; while ((line = br.readLine()) != null) { output += line; } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return output; }
From source file:dk.cafeanalog.AnalogDownloader.java
public AnalogStatus isOpen() { HttpURLConnection connection = null; JsonReader reader = null;/*w w w . ja v a 2 s . c o m*/ try { URL url = new URL("http", "cafeanalog.dk", "api/open"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); reader = new JsonReader(new InputStreamReader(connection.getInputStream())); reader.beginObject(); while (!reader.nextName().equals("open")) { reader.skipValue(); } return reader.nextBoolean() ? AnalogStatus.OPEN : AnalogStatus.CLOSED; } catch (IOException e) { return AnalogStatus.UNKNOWN; } finally { if (connection != null) connection.disconnect(); if (reader != null) { try { reader.close(); } catch (IOException ignored) { } } } }
From source file:lk.appzone.client.MchoiceAventuraSmsSender.java
public String excutePost(String targetURL, String username, String password, String urlParameters) throws MchoiceAventuraMessagingException { URL url;//from w ww .ja va 2 s. c o m HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection) url.openConnection(); setSdpHeaderParams(connection); setBasicAuthentication(username, password, connection); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); sendRequest(urlParameters, connection); return getResponse(connection); } catch (Exception e) { logger.error("Error occcured while sending the request", e); throw new MchoiceAventuraMessagingException("Error occcured while sending the request", e); } finally { if (connection != null) { connection.disconnect(); } } }
From source file:com.finlay.geomonsters.WeatherManager.java
private String getWeatherData(String address) { Log.v(TAG, "getWeatherData: " + address); HttpURLConnection con = null; InputStream is = null;/*ww w. j a va2 s . c o m*/ try { con = (HttpURLConnection) (new java.net.URL(address)).openConnection(); con.setRequestMethod("GET"); con.setDoInput(true); con.setDoOutput(true); con.connect(); Log.v(TAG, "Open Weather connected."); _parent.appendMessage("Open Weather connected."); // Let's read the response StringBuffer buffer = new StringBuffer(); is = con.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) buffer.append(line + "\r\n"); is.close(); con.disconnect(); return buffer.toString(); } catch (Throwable t) { Log.e(TAG, t.getMessage()); } finally { try { is.close(); } catch (Throwable t) { } try { con.disconnect(); } catch (Throwable t) { } } return null; }
From source file:com.PAB.ibeaconreference.AppEngineClient.java
public void delete(URL uri, Map<String, List<String>> headers) { DELETE delete = new DELETE(uri, headers); URL url = null;/*from ww w. ja v a 2 s .co m*/ try { url = new URL("http://localhost:8080/deleteservice"); } catch (MalformedURLException exception) { exception.printStackTrace(); } HttpURLConnection httpURLConnection = null; try { httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestMethod("DELETE"); System.out.println("DELETE response code..! " + httpURLConnection.getResponseCode()); } catch (IOException exception) { exception.printStackTrace(); } finally { if (httpURLConnection != null) { httpURLConnection.disconnect(); } } }
From source file:com.google.appengine.tck.logservice.RequestLogsTest.java
private String performPostRequest(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try {/*from ww w .j ava 2 s . c om*/ connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); PrintWriter out = new PrintWriter(connection.getOutputStream()); try { out.println("foo=bar"); } finally { out.close(); } return readFullyAndClose(connection.getInputStream()).trim(); } finally { connection.disconnect(); } }