List of usage examples for java.net HttpURLConnection getContent
public Object getContent() throws IOException
From source file:trafficdata.GetGeoInfo.java
public static void main(String[] args) throws URISyntaxException { String address = "Sadashiva,bangalore"; String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false&key=AIzaSyC-SQ14QC6ZcilMloviU-KPU-EfmA8qnlY"; URI uri = new URI(url); try {//ww w .ja v a 2s.c om URL page = new URL(uri.toString()); StringBuffer text = new StringBuffer(); HttpURLConnection conn = (HttpURLConnection) page.openConnection(); conn.connect(); InputStreamReader in = new InputStreamReader((InputStream) conn.getContent()); BufferedReader buff = new BufferedReader(in); String st = buff.readLine(); while (st != null) { text.append(st); st = buff.readLine(); } String data = text.toString(); Object obj = JSONValue.parse(data); JSONObject jso = (JSONObject) obj; JSONArray arr1 = (JSONArray) jso.get("results"); String data1 = arr1.get(0).toString(); Object obj1 = JSONValue.parse(data1); JSONObject jso1 = (JSONObject) obj1; String address_updated = jso1.get("formatted_address").toString(); System.out.println(address_updated); String data2 = jso1.get("geometry").toString(); Object obj2 = JSONValue.parse(data2); JSONObject jso2 = (JSONObject) obj2; String data3 = jso2.get("location").toString(); Object obj3 = JSONValue.parse(data3); JSONObject jso3 = (JSONObject) obj3; String lat = jso3.get("lat").toString(); String lng = jso3.get("lng").toString(); // int size = array.size(); //System.out.println(data); } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:org.craftercms.cstudio.publishing.StopServiceMain.java
public static void main(String[] args) throws Exception { FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( "classpath:spring/shutdown-context.xml"); ReadablePropertyPlaceholderConfigurer properties = (ReadablePropertyPlaceholderConfigurer) context .getBean("cstudioShutdownProperties"); if (properties != null) { String url = getProperty(properties, PROP_URL); String path = getProperty(properties, PROP_SERVICE_PATH); String port = getProperty(properties, PROP_PORT); String password = URLEncoder.encode(getProperty(properties, PROP_PASSWORD), "UTF-8"); String target = url + ":" + port + path; if (LOGGER.isDebugEnabled()) { LOGGER.debug("Sending a stop request to " + target); }//from w w w. ja v a 2 s .co m target = target + "?" + StopServiceServlet.PARAM_PASSWORD + "=" + password; URL serviceUrl = new URL(target); HttpURLConnection connection = (HttpURLConnection) serviceUrl.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(10000); connection.connect(); try { connection.getContent(); } catch (ConnectException e) { // ignore this error (server will terminate as soon as the request is sent out) } } else { if (LOGGER.isErrorEnabled()) { LOGGER.error(PROPERTIES_NAME + " is not present in shutdown-context.xml"); } } context.close(); }
From source file:WebReader.java
static void getData(String address) throws Exception { URL page = new URL(address); StringBuffer text = new StringBuffer(); HttpURLConnection conn = (HttpURLConnection) page.openConnection(); conn.connect();/* www . ja v a2 s. c o m*/ InputStreamReader in = new InputStreamReader((InputStream) conn.getContent()); BufferedReader buff = new BufferedReader(in); String line = buff.readLine(); while (line != null) { text.append(line + "\n"); line = buff.readLine(); } System.out.println(text.toString()); }
From source file:org.ff4j.web.console.handler.HttpClient.java
static HttpResponse http(String method, String uri) { try {//from w w w . jav a2 s. com URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(method); Object content = conn.getContent(); if (content instanceof InputStream) return new HttpResponse(conn.getResponseCode(), IOUtils.toString((InputStream) content, "UTF-8")); else if (content instanceof String) return new HttpResponse(conn.getResponseCode(), (String) content); else return new HttpResponse(conn.getResponseCode(), "unknown"); } catch (SocketException e) { return new HttpResponse(SC_NOT_FOUND); } catch (IOException e) { return new HttpResponse(SC_INTERNAL_SERVER_ERROR); } }
From source file:org.esigate.server.Http.java
static Response http(String method, String uri) { try {//ww w. j a v a 2 s .c o m URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(method); Object content = conn.getContent(); if (content instanceof InputStream) { return new Response(conn.getResponseCode(), IOUtils.toString((InputStream) content, "UTF-8")); } else if (content instanceof String) { return new Response(conn.getResponseCode(), (String) content); } else { return new Response(conn.getResponseCode(), "unknown"); } } catch (SocketException e) { return new Response(SC_NOT_FOUND); } catch (IOException e) { return new Response(SC_INTERNAL_SERVER_ERROR); } }
From source file:ee.ria.xroad.signer.certmanager.OcspClient.java
private static byte[] getResponseData(HttpURLConnection connection) throws IOException { byte[] responseData = IOUtils.toByteArray((InputStream) connection.getContent()); if (responseData == null || responseData.length == 0) { throw new IOException("No response from responder"); }//from www . ja v a2 s . c o m return responseData; }
From source file:org.jkcsoft.java.xml.XMLUtil.java
/** * Gets an xml Document that is retrieved via an HTTP GET request. *///w w w. j a v a 2 s . c om public static Document getDocument(String strUrl, DocumentBuilder builder) throws Exception { // use XML parser using a java.net.url to act as http transmitter. URL url = new URL(strUrl); InputStream in = null; String strResponse = null; Document doc = null; HttpURLConnection conn = (HttpURLConnection) url.openConnection(); try { // TODO: Check for type of content to ensure InputStream and text/xml, // as expected. in = (InputStream) conn.getContent(); try { if (builder == null) throw new Exception("null parser from pool!"); try { doc = builder.parse(new InputSource(in)); } catch (Exception ex) { ex.printStackTrace(); } } finally { in.close(); } } finally { conn.disconnect(); } return doc; }
From source file:edu.rit.chrisbitler.ritcraft.slackintegration.rtm.UserList.java
/** * Query the slack api to get the list of users and their real names. This is done whenever we can't find an ID mapping, * and when the plugin loads/*from ww w . j a va2 s . co m*/ */ public static void queryUsers() { try { users.clear(); //Contact the slack api to get the user list HttpURLConnection conn = (HttpURLConnection) new URL( "https://www.slack.com/api/users.list?token=" + SlackIntegration.BOT_TOKEN).openConnection(); conn.connect(); JSONObject retVal = (JSONObject) JSONValue .parse(new InputStreamReader((InputStream) conn.getContent())); JSONArray members = (JSONArray) retVal.get("members"); //Loop through the members and add them to the map Iterator<JSONObject> iter = members.iterator(); while (iter.hasNext()) { JSONObject obj = iter.next(); JSONObject profile = (JSONObject) obj.get("profile"); String id = (String) obj.get("id"); String realName = (String) profile.get("real_name"); users.put(id, realName); } } catch (IOException e) { e.printStackTrace(); } }
From source file:org.giswater.util.UtilsFTP.java
public static boolean isInternetReachable() { try {/*from w ww . ja va2s . c om*/ // Trying to retrieve data from the source. If there is no connection, this line will fail URL url = new URL("http://www.google.com"); HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection(); urlConnect.getContent(); } catch (UnknownHostException e) { return false; } catch (IOException e) { return false; } return true; }
From source file:traffickarmasent.spellcheck.java
public static String spell_check(String message) { try {/*from ww w . j ava 2s .com*/ String url = "https://languagetool.org:8081/?language=en-US&text=" + message; URI uri = new URI(url); URL page = new URL(uri.toString()); StringBuffer text = new StringBuffer(); HttpURLConnection conn = (HttpURLConnection) page.openConnection(); conn.connect(); InputStreamReader in = new InputStreamReader((InputStream) conn.getContent()); BufferedReader buff = new BufferedReader(in); String st1 = buff.readLine(); while (st1 != null) { text.append(st1); st1 = buff.readLine(); } String data = text.toString(); //System.out.println(data); if (data.contains("MORFOLOGIK_RULE_EN_US")) { Pattern p = Pattern.compile("(replacements=\"([^\"]*)\")"); Matcher m = p.matcher(data); if (!Character.isUpperCase(message.charAt(0))) { m.find(); m.find(); } else { m.find(); } //System.out.println(m.group()); if (m.group().contains("#")) { Pattern p1 = Pattern.compile("\"(.*?)#"); Matcher m1 = p1.matcher(m.group()); m1.find(); String ans = m1.group().substring(1, m1.group().length() - 1); return ans; } else { Pattern p1 = Pattern.compile("\"(.*?)\""); Matcher m1 = p1.matcher(m.group()); m1.find(); String ans = m1.group().substring(1, m1.group().length() - 1); //System.out.println(ans); //System.out.println("bababa"); return ans; } } else { return message; } } catch (URISyntaxException | IOException e) { System.out.println(e); } return message; }