List of usage examples for java.net URL openConnection
public URLConnection openConnection() throws java.io.IOException
From source file:Main.java
@SuppressWarnings("SameParameterValue") public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height) throws IOException { URL url = new URL(uri); BufferedInputStream is = null; try {// ww w. ja v a 2s .com HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(urlConnection.getInputStream()); is.mark(MAX_READ_LIMIT_PER_IMG); int scaleFactor = findScaleFactor(width, height, is); Log.d(TAG, "Scaling bitmap " + uri + " by factor " + scaleFactor + " to support " + width + "x" + height + "requested dimension"); is.reset(); return scaleBitmap(scaleFactor, is); } finally { if (is != null) { is.close(); } } }
From source file:com.taobao.diamond.server.controller.CheckController.java
public static String url2str(String sURL, String encoding) { try {// ww w.jav a 2s . c om URL url = new URL(sURL); URLConnection conn = url.openConnection(); if (conn instanceof HttpURLConnection) { HttpURLConnection httpUrl = (HttpURLConnection) conn; httpUrl.getResponseCode(); } InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding)); String r = ""; char[] cbuf = new char[1024]; while (br.read(cbuf) > 0) { r += new String(cbuf); } return r; } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:com.appdynamics.common.RESTClient.java
public static void sendPost(String urlString, String input, String apiKey) { try {/*from ww w. j a va 2s . co m*/ URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64((apiKey).getBytes()))); OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush(); if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; logger.info("Output from Server .... \n"); while ((output = br.readLine()) != null) { logger.info(output); } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.compomics.colims.core.util.AccessionConverter.java
/** * convenience method for opening a buffered reader to an url, standard * timeout is set to 500 milliseconds//from www . ja v a 2 s . c o m * * @param aUrl the url to open a reader to * @return the buffered reader for reading the url * @throws IOException if the url could not be read */ public static BufferedReader openReader(String aUrl) throws IOException { URL myURL = new URL(aUrl); HttpURLConnection c = (HttpURLConnection) myURL.openConnection(); c.setConnectTimeout(500); return new BufferedReader(new InputStreamReader(new BufferedInputStream(c.getInputStream()), "UTF-8")); }
From source file:com.compomics.colims.core.util.AccessionConverter.java
public static BufferedInputStream openStream(String aURL) throws IOException { URL myURL = new URL(aURL); HttpURLConnection c = (HttpURLConnection) myURL.openConnection(); c.setConnectTimeout(500);//from w w w .j a v a2 s .c o m return new BufferedInputStream(c.getInputStream()); }
From source file:com.fluidops.iwb.provider.GoogleRefineProvider.java
/** * utility to simplify doing POST requests * //from w w w . j a v a2s. c om * @param url URL to post to * @param parameter map of parameters to post * @return URLConnection is a state where post parameters have been sent * @throws IOException * * TODO: move to util class */ public static URLConnection doPost(URL url, Map<String, String> parameter) throws IOException { URLConnection con = url.openConnection(); con.setDoOutput(true); StringBuilder params = new StringBuilder(); for (Entry<String, String> entry : parameter.entrySet()) params.append(StringUtil.urlEncode(entry.getKey())).append("=") .append(StringUtil.urlEncode(entry.getValue())).append("&"); // Send data OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); wr.write(params.toString()); wr.flush(); wr.close(); return con; }
From source file:com.thomaskuenneth.openweathermapweather.WeatherUtils.java
public static String getFromServer(String url) throws MalformedURLException, IOException { StringBuilder sb = new StringBuilder(); URL _url = new URL(url); HttpURLConnection httpURLConnection = (HttpURLConnection) _url.openConnection(); String contentType = httpURLConnection.getContentType(); String charSet = "ISO-8859-1"; if (contentType != null) { Matcher m = PATTERN_CHARSET.matcher(contentType); if (m.matches()) { charSet = m.group(1);/* w w w .ja v a 2 s.c o m*/ } } final int responseCode = httpURLConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream(), charSet); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line; while ((line = bufferedReader.readLine()) != null) { sb.append(line); } try { bufferedReader.close(); } catch (IOException ex) { LOGGER.log(Level.SEVERE, "getFromServer()", ex); } } httpURLConnection.disconnect(); return sb.toString(); }
From source file:emily.util.YTUtil.java
/** * @param videocode youtubecode// w ww.j a v a 2 s . co m * @return whats in the <title> tag on a youtube page */ public static String getTitleFromPage(String videocode) { String ret = ""; try { URL loginurl = new URL("https://www.youtube.com/watch?v=" + videocode); URLConnection yc = loginurl.openConnection(); yc.setConnectTimeout(10 * 1000); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); StringBuilder input = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) input.append(inputLine); in.close(); int start = input.indexOf("<title>"); int end = input.indexOf("</title>"); ret = input.substring(start + 7, end - 10); } catch (Exception e) { System.out.println(e); e.printStackTrace(); } return StringEscapeUtils.unescapeHtml4(ret); }
From source file:org.kegbot.app.util.Downloader.java
/** * Downloads an HTTP resource to an output file. * * @param url the resource to download * @param output the output file// w ww.j ava 2 s. c om * @throws IOException upon any error */ public static void downloadRaw(final String url, final File output) throws IOException { final URL destUrl = new URL(url); final HttpURLConnection connection = (HttpURLConnection) destUrl.openConnection(); final FileOutputStream out = new FileOutputStream(output); final InputStream input = connection.getInputStream(); try { byte buffer[] = new byte[4096]; int len; while ((len = input.read(buffer)) >= 0) { out.write(buffer, 0, len); } } finally { out.close(); input.close(); } }
From source file:lapispaste.Main.java
private static void paster(Map<String, String> pastemap, StringBuffer pdata) { try {//from w w w . j a v a 2s. c o m URL url = new URL("http://paste.linux-sevenler.org/index.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // set connection 'writeable' conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); // construct the data... String data; String pdataString = pdata.toString(); data = URLEncoder.encode("language", "ISO-8859-9") + "=" + URLEncoder.encode(pastemap.get("format"), "ISO-8859-9"); data += "&" + URLEncoder.encode("source", "ISO-8859-9") + "=" + URLEncoder.encode(pdataString, "ISO-8859-9"); data += "&" + URLEncoder.encode("submit", "ISO-8859-9") + "=" + URLEncoder.encode(" Kaydet ", "ISO-8859-9"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // get new url where the paste is conn.getInputStream(); System.out.println(conn.getURL()); conn.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } }