List of usage examples for java.net HttpURLConnection getContent
public Object getContent() throws IOException
From source file:com.playhaven.android.req.UrlRequest.java
@Override public String call() throws MalformedURLException, IOException, PlayHavenException { HttpURLConnection connection = (HttpURLConnection) new URL(mInitialUrl).openConnection(); connection.setInstanceFollowRedirects(true); connection.setRequestProperty(CoreProtocolPNames.USER_AGENT, UserAgent.USER_AGENT); connection.getContent(); // .getHeaderFields() will return null if headers not accessed once before if (connection.getHeaderFields().containsKey(LOCATION_HEADER)) { return connection.getHeaderField(LOCATION_HEADER); } else {//w w w . j av a2s . c o m throw new PlayHavenException(); } }
From source file:org.apache.olingo.odata2.fit.client.util.Client.java
private Edm getEdmInternal() throws IOException, ODataException, HttpException { HttpURLConnection connection = connect(METADATA, null, APPLICATION_XML, "GET"); edm = EntityProvider.readMetadata((InputStream) connection.getContent(), false); return edm;//from www. j av a 2s . c om }
From source file:edu.wfu.inotado.helper.OAuthHelper.java
@Deprecated public void sendRequest(AuthStore authStore) throws Exception { OAuthConsumer consumer = cacheHelper.getFromCache(authStore.getSystemName(), consumerCache); URL url;/*from w w w. j av a2s. c om*/ url = new URL("https://vigrior.schoolchapters.com/api/v1/courses.json"); HttpURLConnection request = (HttpURLConnection) url.openConnection(); consumer.sign(request); System.out.println("Sending request..."); request.connect(); System.out.println(request.getContent()); InputStream in = (InputStream) request.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String result, line = reader.readLine(); result = line; while ((line = reader.readLine()) != null) { result += line; } System.out.println(result); System.out.println("Response: " + request.getResponseCode() + " " + request.getResponseMessage()); }
From source file:org.sonar.api.utils.ServerHttpClient.java
protected String getRemoteContent(String url) { HttpURLConnection conn = null; Reader reader = null;/* w ww. j av a 2 s .c o m*/ try { conn = getConnection(url, "GET"); reader = new InputStreamReader((InputStream) conn.getContent()); int statusCode = conn.getResponseCode(); if (statusCode != HttpURLConnection.HTTP_OK) { throw new ServerConnectionException( "Status returned by url : '" + url + "' is invalid : " + statusCode); } return IOUtils.toString(reader); } catch (IOException e) { throw new ServerConnectionException("url=" + url, e); } finally { IOUtils.closeQuietly(reader); if (conn != null) { conn.disconnect(); } } }
From source file:edu.wfu.inotado.helper.RestClientHelper.java
public String getFromService(String serviceName, String urlStr) throws Exception { OAuthConsumer consumer = oauthHelper.getConsumer(serviceName); URL url;/* w w w . jav a 2 s .c o m*/ url = new URL(urlStr); HttpURLConnection request = (HttpURLConnection) url.openConnection(); consumer.sign(request); log.info("Sending request..."); request.connect(); InputStream in = (InputStream) request.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String result, line = reader.readLine(); result = line; while ((line = reader.readLine()) != null) { result += line; } log.debug("Respone: " + result); log.info("Response: " + request.getResponseCode() + " " + request.getResponseMessage()); return result; }
From source file:edu.rit.chrisbitler.ritcraft.slackintegration.SlackIntegration.java
private void startRTM() throws IOException, URISyntaxException, DeploymentException { URL url = new URL("https://www.slack.com/api/rtm.start?token=" + BOT_TOKEN); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(true); conn.connect();//www . ja v a 2 s. co m JSONObject returnVal = (JSONObject) JSONValue.parse(new InputStreamReader((InputStream) conn.getContent())); String wsUrl = (String) returnVal.get("url"); //unescape the wsUrl string's slashes wsUrl = wsUrl.replace("\\", ""); //Query the users so we can link user id -> username System.out.println("Querying slack users.."); UserList.queryUsers(); System.out.println("Recieved WebSocket URI from slack, connecting... " + wsUrl); //Connect via the real-time messaging client and the websocket. ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build(); client = ClientManager.createClient(); rtm = new RTMClient(); client.connectToServer(rtm, cec, new URI(wsUrl)); }
From source file:com.adamrosenfield.wordswithcrosses.net.DerStandardDownloader.java
protected Reader getURLReader(HttpURLConnection conn) throws IOException, MalformedURLException, UnsupportedEncodingException { InputStream in = (InputStream) conn.getContent(); String encoding = conn.getContentEncoding(); String type = conn.getContentType(); if (encoding == null) { Matcher m = P_CHARSET_IN_TYPE.matcher(type); if (m.matches()) { encoding = m.group(1);/* w ww . ja va2 s . c o m*/ } else { encoding = "ISO-8859-1"; } } Reader r = new InputStreamReader(in, encoding); return r; }
From source file:weatherRoute.MapHandler.java
public void testing() { //replace these with input values String origin = "Boise"; String destination = "Rexburg"; String apiQuery = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&key=AIzaSyDbWBUUbN6UHlG9auqFVGmN6WJY9HSe8YI"; try {/* w ww. j av a2 s . co m*/ URL url = new URL(apiQuery); HttpURLConnection request = (HttpURLConnection) url.openConnection(); request.connect(); JsonParser jp = new JsonParser(); JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //You have to step through the ugly, JSON within array within JSON stuff. JsonArray stepsArray = root.getAsJsonObject().getAsJsonArray("routes").get(0).getAsJsonObject() .getAsJsonArray("legs").get(0).getAsJsonObject().getAsJsonArray("steps"); //loop through steps to get city locations for (JsonElement element : stepsArray) { //this gets a string value of the distance in miles String distance = element.getAsJsonObject().get("distance").getAsJsonObject().get("text") .toString(); //parse return value and cast to a Double to be able to compare distance = distance.replace(" mi", ""); distance = distance.replace("\"", ""); Double test = Double.parseDouble(distance); //this limits out all the short .5 mile steps //should mostly get different cities now if (Double.compare(test, 10.0) > 0) { System.out.println("TEST DISTANCE: " + distance + ", " + test); JsonObject location = element.getAsJsonObject().get("end_location").getAsJsonObject(); String latitude = location.get("lat").toString(); String longitude = location.get("lng").toString(); //now I need to hit a different API and get CITY names from lat long //http://stackoverflow.com/questions/6548504/how-can-i-get-city-name-from-a-latitude-and-longitude-point //fortunately it shows me how here ^ System.out.println("TEST LOCATION LAT: " + latitude + " LONG: " + longitude); } } } catch (Exception e) { System.out.println("Error: " + e); } }
From source file:weatherRoute.MapHandler.java
public ArrayList<Location> getCities(String origin, String destination) { ArrayList<Location> cities = new ArrayList<Location>(); origin = origin.replace(" ", ""); destination = destination.replace(" ", ""); String apiQuery = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&key=AIzaSyDbWBUUbN6UHlG9auqFVGmN6WJY9HSe8YI"; try {//w w w.j ava 2 s . com URL url = new URL(apiQuery); HttpURLConnection request = (HttpURLConnection) url.openConnection(); request.connect(); JsonParser jp = new JsonParser(); JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //You have to step through the ugly, JSON within array within JSON stuff. JsonArray stepsArray = root.getAsJsonObject().getAsJsonArray("routes").get(0).getAsJsonObject() .getAsJsonArray("legs").get(0).getAsJsonObject().getAsJsonArray("steps"); //loop through steps to get city locations for (JsonElement element : stepsArray) { //this gets a string value of the distance in miles String distance = element.getAsJsonObject().get("distance").getAsJsonObject().get("text") .toString(); if (distance.contains("mi")) { //parse return value and cast to a Double to be able to compare distance = distance.replace(" mi", ""); distance = distance.replace("\"", ""); Double test = Double.parseDouble(distance); //this limits out all the short .5 mile steps //should mostly get different cities now if (Double.compare(test, 10.0) > 0) { System.out.println("TEST DISTANCE: " + distance + ", " + test); JsonObject location = element.getAsJsonObject().get("end_location").getAsJsonObject(); String latitude = location.get("lat").toString(); String longitude = location.get("lng").toString(); Location tempLocation = new Location(Double.parseDouble(latitude), Double.parseDouble(longitude)); cities.add(tempLocation); //now I need to hit a different API and get CITY names from lat long //http://stackoverflow.com/questions/6548504/how-can-i-get-city-name-from-a-latitude-and-longitude-point //fortunately it shows me how here ^ //System.out.println("TEST LOCATION LAT: " + latitude + " LONG: " + longitude); } } } } catch (Exception e) { System.out.println("Error: " + e); } //testing(); return cities; }
From source file:org.neo4j.mytests.SSShortestPathTest.java
@Test public void shouldReturnMaxFlow() { Transaction tx = db.beginTx();// w ww. jav a 2 s .c o m try { Node a = db.createNode(); Node b = db.createNode(); Node c = db.createNode(); Node d = db.createNode(); Node e = db.createNode(); a.createRelationshipTo(e, MyRelationshipTypes.KNOWS); a.createRelationshipTo(b, MyRelationshipTypes.KNOWS); e.createRelationshipTo(b, MyRelationshipTypes.KNOWS); e.createRelationshipTo(d, MyRelationshipTypes.KNOWS); b.createRelationshipTo(c, MyRelationshipTypes.KNOWS); c.createRelationshipTo(d, MyRelationshipTypes.KNOWS); } catch (Exception e) { System.err.println("Exception Error: MaxflowTest.shouldReturnMaxFlow: " + e); tx.failure(); } finally { tx.success(); tx.close(); } String serverBaseUri = server.baseUri().toString(); URL uriArray[] = new URL[6]; String q1 = serverBaseUri + "hintplugin/utils/ssshortestpath/0/2"; // a-c 6 String q2 = serverBaseUri + "hintplugin/utils/ssshortestpath/0/3"; // a-d 4 String q3 = serverBaseUri + "hintplugin/utils/ssshortestpath/0/4"; // a-e 2 String q4 = serverBaseUri + "hintplugin/utils/ssshortestpath/2/3"; // c-d 4 String q5 = serverBaseUri + "hintplugin/utils/ssshortestpath/2/4"; // c-e 2 String q6 = serverBaseUri + "hintplugin/utils/ssshortestpath/3/4"; // d-e 2 try { uriArray[0] = new URL(q1); uriArray[1] = new URL(q2); uriArray[2] = new URL(q3); uriArray[3] = new URL(q4); uriArray[4] = new URL(q5); uriArray[5] = new URL(q6); } catch (Exception ex) { System.out.println("***** ERROR: " + ex); } //Establish a connection to the server and get Content. for (int i = 0; i < uriArray.length; i++) { try { HttpURLConnection http = (HttpURLConnection) uriArray[i].openConnection(); http.setRequestMethod("GET"); http.connect(); StringBuffer text = new StringBuffer(); InputStreamReader in = new InputStreamReader((InputStream) http.getContent()); BufferedReader buff = new BufferedReader(in); String line = ""; while (line != null) { line = buff.readLine(); text.append(line + " "); } JSONObject obj = new JSONObject(text.toString()); System.out.println("***SSSPTH-JSON: " + obj.optDouble("shortestPaths")); } catch (Exception ex) { System.out.println("MaxflowTest Exception: " + ex); } } assertEquals("200", "200"); }