List of usage examples for java.net URLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:com.wareninja.opensource.common.wsrequest.HttpUtils.java
/** * Send an HTTP(s) request with POST parameters. * /* www. j a v a 2s. co m*/ * @param parameters * @param url * @throws UnsupportedEncodingException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ public static void doPost(Map<?, ?> parameters, URL url) throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException { URLConnection cnx = getConnection(url); // Construct data StringBuilder dataBfr = new StringBuilder(); Iterator<?> iKeys = parameters.keySet().iterator(); while (iKeys.hasNext()) { if (dataBfr.length() != 0) { dataBfr.append('&'); } String key = (String) iKeys.next(); dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=') .append(URLEncoder.encode((String) parameters.get(key), "UTF-8")); } // POST data cnx.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream()); if (LOGGING.DEBUG) Log.d(LOG_TAG, "Posting crash report data"); wr.write(dataBfr.toString()); wr.flush(); wr.close(); if (LOGGING.DEBUG) Log.d(LOG_TAG, "Reading response"); BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream())); String line; int linecount = 0; while ((line = rd.readLine()) != null) { linecount++; if (linecount <= 2) { if (LOGGING.DEBUG) Log.d(LOG_TAG, line); } } rd.close(); }
From source file:com.betfair.tornjak.monitor.active.url.CheckResponseNotContains.java
public void check(URLConnection urlConnection) throws Exception { InputStream inputStream = urlConnection.getInputStream(); String response = IOUtils.toString(inputStream); if (response.contains(errorString)) { throw new RuntimeException( String.format("Service reports [%s], which contains [%s]", response, errorString)); }//from w w w .j a v a 2 s .com }
From source file:org.fao.geonet.util.XslUtil.java
/** * To get the xml content of an url/*from w ww.j av a 2 s .c o m*/ * It supports the usage of a proxy * @param surl * @return */ public static Node getUrlContent(String surl) { Node res = null; InputStream is = null; ServiceContext context = ServiceContext.get(); try { URL url = new URL(surl); URLConnection conn = Lib.net.setupProxy(context, url); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); is = conn.getInputStream(); res = db.parse(is); } catch (Throwable e) { Log.error(Geonet.GEONETWORK, "Failed fetching url: " + surl, e); } finally { IOUtils.closeQuietly(is); } return res; }
From source file:com.betfair.tornjak.monitor.active.url.CheckResponseContains.java
public void check(URLConnection urlConnection) throws Exception { InputStream inputStream = urlConnection.getInputStream(); String response = IOUtils.toString(inputStream); if (!response.contains(expectedString)) { throw new RuntimeException( String.format("Service reports [%s], which NOT contains [%s]", response, expectedString)); }//from w w w . j a va2s. c o m }
From source file:com.betfair.tornjak.monitor.active.url.CheckResponseMatches.java
public void check(URLConnection urlConnection) throws Exception { InputStream inputStream = urlConnection.getInputStream(); String response = IOUtils.toString(inputStream); if (!pattern.matcher(response).matches()) { throw new RuntimeException( String.format("Service reports [%s], doesn't match [%s]", response, pattern)); }/*from w w w . ja v a2s . c o m*/ }
From source file:edu.ucsd.ccdb.cil.xml2json.XML2JsonUtil.java
public String getTextFromURL(String url) throws Exception { URL website = new URL(url); URLConnection connection = website.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String inputLine;//from w ww . j a va2s . com while ((inputLine = in.readLine()) != null) response.append(inputLine + "\n"); in.close(); return response.toString(); }
From source file:league.stat.checker.nameToID.java
public String nameToID(String name) throws IOException { //HTTP response input code taken WhiteFang34 on Stack overflow ; http://stackoverflow.com/questions/5769717/how-can-i-get-an-http-response-body-as-a-string-in-java URL url = new URL("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + name + "?api_key=RGAPI-f2ba678c-7c42-4265-8c36-ae1364c3e9b9"); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); //Make this code more efficient by using String slicing int index = body.indexOf("id") + 4; String sID = ""; while (body.charAt(index) != ',') { sID += body.charAt(index);/*from w w w . ja v a 2s.c om*/ index++; } return sID; }
From source file:com.elega9t.commons.geo.impl.HostIpDotInfoGeoLocationProvider.java
@Override public GeoLocationInfo lookup(String ip) throws IOException { URL url = new URL("http://api.hostip.info/get_json.php?ip=" + URLEncoder.encode(ip, "UTF-8")); URLConnection connection = url.openConnection(); connection.connect();// ww w. ja va 2 s . com InputStream inputStream = connection.getInputStream(); String json = IOUtilities.toString(inputStream); inputStream.close(); try { JSONObject object = new JSONObject(json); return new GeoLocationInfo(object.getString("ip"), object.getString("country_name"), object.getString("country_code"), object.getString("city")); } catch (JSONException e) { throw new IOException(e); } }
From source file:league.stat.checker.nameToID.java
public String[] findInfo(String input, String name) throws IOException { String[] output = new String[5]; output[0] = name;//from w w w . j av a 2 s.c o m output[1] = input; URL url = new URL("https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/" + input + "/summary?season=SEASON2017&api_key=RGAPI-f2ba678c-7c42-4265-8c36-ae1364c3e9b9"); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); String rankedInfo = body.substring(body.indexOf("RankedSolo5x5"), body.lastIndexOf("}")); //Wins Number output[2] = rankedInfo.substring(rankedInfo.indexOf("wins") + 6, rankedInfo.length()).substring(0, rankedInfo.substring(rankedInfo.indexOf("wins") + 6, rankedInfo.length()).indexOf(",")); //Loss Number output[3] = rankedInfo.substring(rankedInfo.indexOf("losses") + 8, rankedInfo.length()).substring(0, rankedInfo.substring(rankedInfo.indexOf("losses") + 8, rankedInfo.length()).indexOf(",")); //Win percent output[4] = (String.format("%.2f", (Double.parseDouble(output[2]) / (Double.parseDouble(output[2]) + Double.parseDouble(output[3]))) * 100)); return output; }
From source file:gemlite.core.internal.support.hotdeploy.scanner.ScannerIteratorItem.java
private byte[] readContent(ClassLoader loader, JarEntry entry) throws IOException { URL url = loader.getResource(entry.getName()); URLConnection ulc = url.openConnection(); InputStream in3 = ulc.getInputStream(); InputStream in2 = url.openStream(); InputStream in = loader.getResourceAsStream(entry.getName()); if (in == null) { LogUtil.getCoreLog().trace("ReadContent inputStream is null entry.name={} , loader={}", entry.getName(), loader);/*from ww w. ja v a 2s.c om*/ } BufferedInputStream bi = new BufferedInputStream(in); byte[] bt = new byte[in.available()]; bi.read(bt); bi.close(); in.close(); return bt; }