List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:edu.stanford.muse.index.NER.java
public static List<Pair<String, Float>> namesFromArchive(String url, boolean removeCommonNames) throws ClassCastException, IOException, ClassNotFoundException { // only http conns allowed currently HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setInstanceFollowRedirects(true); conn.setRequestProperty("User-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"); conn.connect(); byte[] b = Util.getBytesFromStream(conn.getInputStream()); String text = new String(b, "UTF-8"); text = Util.unescapeHTML(text); org.jsoup.nodes.Document doc = Jsoup.parse(text); text = doc.body().text();/* w w w .j a v a 2 s .co m*/ return namesFromText(text, removeCommonNames); }
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> getBitcoinCharts() { try {// ww w. java 2 s . co m // double btcRate = getGoldCoinValueBTC(); //NullPointerException??? Double btcRate = 0.0; Object result = getGoldCoinValueBTC_ccex(); if (result == null) { result = getCoinValueBTC_cryptopia(); if (result == null) return null; else btcRate = (Double) result; } else btcRate = (Double) result; final URL URL = new URL("http://api.bitcoincharts.com/v1/weighted_prices.json"); final HttpURLConnection connection = (HttpURLConnection) URL.openConnection(); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) return null; Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); final StringBuilder content = new StringBuilder(); IOUtils.copy(reader, content); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); //Add Bitcoin information rates.put("BTC", new ExchangeRate("BTC", Utils.toNanoCoins(String.format("%.8f", btcRate).replace(",", ".")), "pubapi.cryptsy.com")); final JSONObject head = new JSONObject(content.toString()); for (final Iterator<String> i = head.keys(); i.hasNext();) { final String currencyCode = i.next(); if (!"timestamp".equals(currencyCode)) { final JSONObject o = head.getJSONObject(currencyCode); String rate = o.optString("24h", null); if (rate == null) rate = o.optString("7d", null); if (rate == null) rate = o.optString("30d", null); double rateForBTC = Double.parseDouble(rate); rate = String.format("%.8f", rateForBTC * btcRate); if (rate != null) rates.put(currencyCode, new ExchangeRate(currencyCode, Utils.toNanoCoins(rate.replace(",", ".")), URL.getHost())); } } return rates; } finally { if (reader != null) reader.close(); } } catch (final IOException x) { // log.debug("problem reading exchange rates", x); x.printStackTrace(); } catch (final JSONException x) { //log.debug("problem reading exchange rates", x); x.printStackTrace(); } return null; }
From source file:com.cannabiscoin.wallet.ExchangeRatesProvider.java
private static Object getCoinValueBTC() { Date date = new Date(); long now = date.getTime(); //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the LTC rate around for a bit Double btcRate = 0.0;/*from ww w. j ava 2 s . c o m*/ String currencyCryptsy = CoinDefinition.cryptsyMarketCurrency; String urlCryptsy = "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=" + CoinDefinition.cryptsyMarketId; HttpURLConnection connectionCryptsy = null; try { // final String currencyCode = currencies[i]; final URL URLCryptsy = new URL(urlCryptsy); connectionCryptsy = (HttpURLConnection) URLCryptsy.openConnection(); connectionCryptsy.setConnectTimeout(Constants.HTTP_TIMEOUT_MS * 2); connectionCryptsy.setReadTimeout(Constants.HTTP_TIMEOUT_MS * 2); connectionCryptsy.connect(); final StringBuilder contentCryptsy = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connectionCryptsy.getInputStream(), 1024)); Io.copy(reader, contentCryptsy); final JSONObject head = new JSONObject(contentCryptsy.toString()); JSONObject returnObject = head.getJSONObject("return"); JSONObject markets = returnObject.getJSONObject("markets"); JSONObject coinInfo = markets.getJSONObject(CoinDefinition.coinTicker); JSONArray recenttrades = coinInfo.getJSONArray("recenttrades"); double btcTraded = 0.0; double coinTraded = 0.0; for (int i = 0; i < recenttrades.length(); ++i) { JSONObject trade = (JSONObject) recenttrades.get(i); btcTraded += trade.getDouble("total"); coinTraded += trade.getDouble("quantity"); } Double averageTrade = btcTraded / coinTraded; //Double lastTrade = GLD.getDouble("lasttradeprice"); //String euros = String.format("%.7f", averageTrade); // Fix things like 3,1250 //euros = euros.replace(",", "."); //rates.put(currencyCryptsy, new ExchangeRate(currencyCryptsy, Utils.toNanoCoins(euros), URLCryptsy.getHost())); if (currencyCryptsy.equalsIgnoreCase("BTC")) btcRate = averageTrade; } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } } return btcRate; } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } finally { if (connectionCryptsy != null) connectionCryptsy.disconnect(); } return null; }
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Object getCoinValueBTC_cryptopia() { //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the LTC rate around for a bit Double btcRate = 0.0;//from w ww.ja v a 2s . com String currency = "BTC"; String url = "https://www.cryptopia.co.nz/api/GetMarket/2623"; try { // final String currencyCode = currencies[i]; final URL URL_bter = new URL(url); final HttpURLConnection connection = (HttpURLConnection) URL_bter.openConnection(); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS * 2); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS * 2); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); /*{ "Success":true, "Message":null, "Data":{ "TradePairId":100, "Label":"LTC/BTC", "AskPrice":0.00006000, "BidPrice":0.02000000, "Low":0.00006000, "High":0.00006000, "Volume":1000.05639978, "LastPrice":0.00006000, "LastVolume":499.99640000, "BuyVolume":67003436.37658233, "SellVolume":67003436.37658233, "Change":-400.00000000 } }*/ String result = head.getString("Success"); if (result.equals("true")) { JSONObject dataObject = head.getJSONObject("Data"); Double averageTrade = Double.valueOf(0.0); if (dataObject.get("Label").equals("GLD/BTC")) averageTrade = dataObject.getDouble("LastPrice"); if (currency.equalsIgnoreCase("BTC")) btcRate = averageTrade; } return btcRate; } finally { if (reader != null) reader.close(); } } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:info.icefilms.icestream.browse.Location.java
private static Bitmap DownloadImage(URL url, Callback callback) { try {// w w w.j a v a 2s. com // Open the connection HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0"); urlConnection.setConnectTimeout(callback.GetConnectionTimeout()); urlConnection.setReadTimeout(callback.GetConnectionTimeout()); urlConnection.connect(); // Get the input stream InputStream inputStream = urlConnection.getInputStream(); // For some reason we can actually get a null InputStream instead of an exception if (inputStream == null) { Log.e("Ice Stream", "Image download failed. Unable to create Input Stream."); if (callback.GetErrorBoolean() == false) { callback.SetErrorBoolean(true); callback.SetErrorStringID(R.string.browse_image_download_error); } urlConnection.disconnect(); return null; } // Download the image and create the bitmap Bitmap bitmap = BitmapFactory.decodeStream(inputStream); // Close the connection inputStream.close(); urlConnection.disconnect(); // Check for errors if (bitmap == null) { Log.e("Ice Stream", "Image data decoding failed."); if (callback.GetErrorBoolean() == false) { callback.SetErrorBoolean(true); callback.SetErrorStringID(R.string.browse_image_decode_error); } return null; } return bitmap; } catch (SocketTimeoutException exception) { Log.e("Ice Stream", "Image download failed.", exception); if (callback.GetErrorBoolean() == false) { callback.SetErrorBoolean(true); callback.SetErrorStringID(R.string.browse_image_timeout_error); } return null; } catch (IOException exception) { Log.e("Ice Stream", "Image download failed.", exception); if (callback.GetErrorBoolean() == false) { callback.SetErrorBoolean(true); callback.SetErrorStringID(R.string.browse_image_download_error); } return null; } }
From source file:de.langerhans.wallet.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> requestExchangeRates(final URL url, double dogeBtcConversion, final String userAgent, final String source, final String... fields) { final long start = System.currentTimeMillis(); HttpURLConnection connection = null; Reader reader = null;// w ww . j a v a2s . c o m try { connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS); connection.addRequestProperty("User-Agent", userAgent); connection.addRequestProperty("Accept-Encoding", "gzip"); connection.connect(); final int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { final String contentEncoding = connection.getContentEncoding(); InputStream is = new BufferedInputStream(connection.getInputStream(), 1024); if ("gzip".equalsIgnoreCase(contentEncoding)) is = new GZIPInputStream(is); reader = new InputStreamReader(is, Charsets.UTF_8); final StringBuilder content = new StringBuilder(); final long length = Io.copy(reader, content); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); final JSONObject head = new JSONObject(content.toString()); for (final Iterator<String> i = head.keys(); i.hasNext();) { final String currencyCode = i.next(); if (!"timestamp".equals(currencyCode)) { final JSONObject o = head.getJSONObject(currencyCode); for (final String field : fields) { final String rate = o.optString(field, null); if (rate != null) { try { final double btcRate = Double .parseDouble(Fiat.parseFiat(currencyCode, rate).toPlainString()); DecimalFormat df = new DecimalFormat("#.########"); df.setRoundingMode(RoundingMode.HALF_UP); DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setDecimalSeparator('.'); dfs.setGroupingSeparator(','); df.setDecimalFormatSymbols(dfs); final Fiat dogeRate = Fiat.parseFiat(currencyCode, df.format(btcRate * dogeBtcConversion)); if (dogeRate.signum() > 0) { rates.put(currencyCode, new ExchangeRate( new com.dogecoin.dogecoinj.utils.ExchangeRate(dogeRate), source)); break; } } catch (final NumberFormatException x) { log.warn("problem fetching {} exchange rate from {} ({}): {}", currencyCode, url, contentEncoding, x.getMessage()); } } } } } log.info("fetched exchange rates from {} ({}), {} chars, took {} ms", url, contentEncoding, length, System.currentTimeMillis() - start); return rates; } else { log.warn("http status {} when fetching exchange rates from {}", responseCode, url); } } catch (final Exception x) { log.warn("problem fetching exchange rates from " + url, x); } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } if (connection != null) connection.disconnect(); } return null; }
From source file:com.orange.oidc.secproxy_service.HttpOpenidConnect.java
static String getHttpString(String url) { String result = null;/*w ww .j a v a 2 s .c o m*/ // build connection HttpURLConnection huc = getHUC(url); huc.setInstanceFollowRedirects(false); try { // try to establish connection huc.connect(); // get result int responseCode = huc.getResponseCode(); Logd(TAG, "getHttpString response: " + responseCode); // if 200, read http body if (responseCode == 200) { InputStream is = huc.getInputStream(); result = convertStreamToString(is); is.close(); } else { // result = "response code: "+responseCode; } // close connection huc.disconnect(); } catch (Exception e) { Log.e(TAG, "revokeSite FAILED"); e.printStackTrace(); } return result; }
From source file:com.fota.Link.sdpApi.java
public static String getNcnInfo(String CTN) throws JDOMException { String resultStr = ""; String logData = ""; try {/* w w w . j a v a 2s.c o m*/ String endPointUrl = PropUtil.getPropValue("sdp.oif516.url"); String strRequest = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " + "xmlns:oas='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'" + " xmlns:sdp='http://kt.com/sdp'>" + " <soapenv:Header>" + " <oas:Security>" + " <oas:UsernameToken>" + " <oas:Username>" + PropUtil.getPropValue("sdp.id") + "</oas:Username>" + " <oas:Password>" + PropUtil.getPropValue("sdp.pw") + "</oas:Password>" + " </oas:UsernameToken>" + " </oas:Security>" + " </soapenv:Header>" + " <soapenv:Body>" + " <sdp:getBasicUserInfoAndMarketInfoRequest>" + " <!--You may enterthe following 6 items in any order-->" + " <sdp:CALL_CTN>" + CTN + "</sdp:CALL_CTN>" + " </sdp:getBasicUserInfoAndMarketInfoRequest>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; logData = "\r\n---------- Get Ncn Req Info start ----------\r\n"; logData += " get Ncn Req Info - endPointUrl : " + endPointUrl; logData += "\r\n get Ncn Req Info - Content-type : text/xml;charset=utf-8"; logData += "\r\n get Ncn Req Info - RequestMethod : POST"; logData += "\r\n get Ncn Req Info - xml : " + strRequest; logData += "\r\n---------- Get Ncn Req Info end ----------"; logger.info(logData); // connection URL url = new URL(endPointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-type", "text/xml;charset=utf-8"); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); // output OutputStream os = connection.getOutputStream(); // os.write(strRequest.getBytes(), 0, strRequest.length()); os.write(strRequest.getBytes("utf-8")); os.flush(); os.close(); // input InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String line = ""; String resValue = ""; String parseStr = ""; while ((line = br.readLine()) != null) { System.out.println(line); parseStr = line; } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource temp = new InputSource(); temp.setCharacterStream(new StringReader(parseStr)); Document doc = builder.parse(temp); //xml? NodeList list = doc.getElementsByTagName("*"); int i = 0; Element element; String contents; String contractNum = ""; String customerId = ""; while (list.item(i) != null) { element = (Element) list.item(i); if (element.hasChildNodes()) { contents = element.getFirstChild().getNodeValue(); System.out.println(element.getNodeName() + " / " + element.getFirstChild().getNodeName()); if (element.getNodeName().equals("sdp:NS_CONTRACT_NUM")) { // resultStr = element.getFirstChild().getNodeValue(); contractNum = element.getFirstChild().getNodeValue(); } if (element.getNodeName().equals("sdp:NS_CUSTOMER_ID")) { customerId = element.getFirstChild().getNodeValue(); } // System.out.println(" >>>>> " + contents); } i++; } // System.out.println("contractNum : " + contractNum + " / cusomerId : " + customerId); resultStr = getNcnFromContractnumAndCustomerId(contractNum, customerId); // System.out.println("ncn : " + resultStr); //resultStr = resValue; // resultStr = java.net.URLDecoder.decode(resultStr, "euc-kr"); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return resultStr; }
From source file:WebReader.java
void getData(String address) throws Exception { setTitle(address);//from ww w. ja v a 2 s . c o m URL page = new URL(address); StringBuffer text = new StringBuffer(); HttpURLConnection conn = (HttpURLConnection) page.openConnection(); conn.connect(); InputStreamReader in = new InputStreamReader((InputStream) conn.getContent()); BufferedReader buff = new BufferedReader(in); box.setText("Getting data ..."); String line; do { line = buff.readLine(); text.append(line + "\n"); } while (line != null); box.setText(text.toString()); }
From source file:com.dopecoin.wallet.ExchangeRatesProvider.java
private static float requestDogeBtcConversion(int provider) { HttpURLConnection connection = null; Reader reader = null;//from w ww . j a v a 2 s . c o m URL providerUrl; switch (provider) { case 0: providerUrl = DOPEPOOL_URL; break; case 1: providerUrl = VIRCUREX_URL; break; default: providerUrl = DOPEPOOL_URL; break; } try { connection = (HttpURLConnection) providerUrl.openConnection(); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS); connection.connect(); final int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024), Constants.UTF_8); final StringBuilder content = new StringBuilder(); Io.copy(reader, content); try { float rate; switch (provider) { case 0: /*rate = Float.parseFloat( json.getJSONObject("return") .getJSONObject("markets") .getJSONObject("DOPE") .getString("lasttradeprice"));*/ //For later use. rate = Float.parseFloat(content.toString()); break; case 1: final JSONObject json = new JSONObject(content.toString()); rate = Float.parseFloat(json.getString("value")); break; default: return -1; } return rate; } catch (NumberFormatException e) { log.debug("Couldn't get the current exchnage rate from provider " + String.valueOf(provider)); return -1; } } else { log.debug("http status " + responseCode + " when fetching " + providerUrl); } } catch (final Exception x) { log.debug("problem reading exchange rates", x); } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } if (connection != null) connection.disconnect(); } return -1; }