List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:com.cloudera.earthquake.GetData.java
public static void main(String[] args) throws IOException { URL url = new URL("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); InputStream connStream = conn.getInputStream(); FileSystem hdfs = FileSystem.get(new Configuration()); FSDataOutputStream outStream = hdfs.create(new Path(args[0], "month.txt")); IOUtils.copy(connStream, outStream); outStream.close();//from w w w . j av a 2s . c o m connStream.close(); conn.disconnect(); }
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); }// w ww. j a va 2s .c o 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:GoogleImages.java
public static void main(String[] args) throws InterruptedException { String searchTerm = "s woman"; // term to search for (use spaces to separate terms) int offset = 40; // we can only 20 results at a time - use this to offset and get more! String fileSize = "50mp"; // specify file size in mexapixels (S/M/L not figured out yet) String source = null; // string to save raw HTML source code // format spaces in URL to avoid problems searchTerm = searchTerm.replaceAll(" ", "%20"); // get Google image search HTML source code; mostly built from PhyloWidget example: // http://code.google.com/p/phylowidget/source/browse/trunk/PhyloWidget/src/org/phylowidget/render/images/ImageSearcher.java int offset2 = 0; Set urlsss = new HashSet<String>(); while (offset2 < 600) { try {// w w w. j a va 2 s . c o m URL query = new URL("https://www.google.ru/search?start=" + offset2 + "&q=angry+woman&newwindow=1&client=opera&hs=bPE&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiAgcKozIfNAhWoHJoKHSb_AUoQ_AUIBygB&biw=1517&bih=731&dpr=0.9#imgrc=G_1tH3YOPcc8KM%3A"); HttpURLConnection urlc = (HttpURLConnection) query.openConnection(); // start connection... urlc.setInstanceFollowRedirects(true); urlc.setRequestProperty("User-Agent", ""); urlc.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream())); // stream in HTTP source to file StringBuffer response = new StringBuffer(); char[] buffer = new char[1024]; while (true) { int charsRead = in.read(buffer); if (charsRead == -1) { break; } response.append(buffer, 0, charsRead); } in.close(); // close input stream (also closes network connection) source = response.toString(); } // any problems connecting? let us know catch (Exception e) { e.printStackTrace(); } // print full source code (for debugging) // println(source); // extract image URLs only, starting with 'imgurl' if (source != null) { // System.out.println(source); int c = StringUtils.countMatches(source, "http://www.vmir.su"); System.out.println(c); int index = source.indexOf("src="); System.out.println(source.subSequence(index, index + 200)); while (index >= 0) { System.out.println(index); index = source.indexOf("src=", index + 1); if (index == -1) { break; } String rr = source.substring(index, index + 200 > source.length() ? source.length() : index + 200); if (rr.contains("\"")) { rr = rr.substring(5, rr.indexOf("\"", 5)); } System.out.println(rr); urlsss.add(rr); } } offset2 += 20; Thread.sleep(1000); System.out.println("off set = " + offset2); } System.out.println(urlsss); urlsss.forEach(new Consumer<String>() { public void accept(String s) { try { saveImage(s, "C:\\Users\\Java\\Desktop\\ang\\" + UUID.randomUUID().toString() + ".jpg"); } catch (IOException ex) { Logger.getLogger(GoogleImages.class.getName()).log(Level.SEVERE, null, ex); } } }); // String[][] m = matchAll(source, "img height=\"\\d+\" src=\"([^\"]+)\""); // older regex, no longer working but left for posterity // built partially from: http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression // String[][] m = matchAll(source, "imgurl=(.*?\\.(?i)(jpg|jpeg|png|gif|bmp|tif|tiff))"); // (?i) means case-insensitive // for (int i = 0; i < m.length; i++) { // iterate all results of the match // println(i + ":\t" + m[i][1]); // print (or store them)** // } }
From source file:ru.apertum.qsystem.client.forms.FAdmin.java
/** * @param args the command line arguments * @throws Exception// w ww .j av a 2s.c o m */ public static void main(String args[]) throws Exception { QLog.initial(args, 3); Locale.setDefault(Locales.getInstance().getLangCurrent()); //? ? , ? final Thread tPager = new Thread(() -> { FAbout.loadVersionSt(); String result = ""; try { final URL url = new URL(PAGER_URL + "/qskyapi/getpagerdata?qsysver=" + FAbout.VERSION_); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("User-Agent", "Java bot"); conn.connect(); final int code = conn.getResponseCode(); if (code == 200) { try (BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "utf8"))) { String inputLine; while ((inputLine = in.readLine()) != null) { result += inputLine; } } } conn.disconnect(); } catch (Exception e) { System.err.println("Pager not enabled. " + e); return; } final Gson gson = GsonPool.getInstance().borrowGson(); try { final Answer answer = gson.fromJson(result, Answer.class); forPager = answer; if (answer.getData().size() > 0) { forPager.start(); } } catch (Exception e) { System.err.println("Pager not enabled but working. " + e); } finally { GsonPool.getInstance().returnGson(gson); } }); tPager.setDaemon(true); tPager.start(); Uses.startSplash(); // plugins Uses.loadPlugins("./plugins/"); // ?. FLogin.logining(QUserList.getInstance(), null, true, 3, FLogin.LEVEL_ADMIN); Uses.showSplash(); java.awt.EventQueue.invokeLater(() -> { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager .getInstalledLookAndFeels()) { System.out.println(info.getName()); /*Metal Nimbus CDE/Motif Windows Windows Classic //GTK+*/ if ("Windows".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } if ("/".equals(File.separator)) { final FontUIResource f = new FontUIResource(new Font("Serif", Font.PLAIN, 10)); final Enumeration<Object> keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { final Object key = keys.nextElement(); final Object value = UIManager.get(key); if (value instanceof FontUIResource) { final FontUIResource orig = (FontUIResource) value; final Font font1 = new Font(f.getFontName(), orig.getStyle(), f.getSize()); UIManager.put(key, new FontUIResource(font1)); } } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } try { form = new FAdmin(); if (forPager != null) { forPager.showData(false); } else { form.panelPager.setVisible(false); } form.setVisible(true); } catch (Exception ex) { QLog.l().logger().error(" ? ?? . ", ex); } finally { Uses.closeSplash(); } }); }
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(); 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();// ww w . j a v a 2 s . co m } System.out.println(text.toString()); }
From source file:Main.java
public static InputStream getIS(String address) { InputStream is = null;//from w w w .j av a 2s .c o m try { URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); is = conn.getInputStream(); } catch (Exception e) { e.printStackTrace(); } return is; }
From source file:Main.java
/** * Send the Http's request with the address of url * @param String url/* w ww .j a va2 s . c om*/ * @return String */ public static String getHttpRequest(String url) { //should use the StringBuilder or StringBuffer, String is not used. StringBuffer jsonContent = new StringBuffer(); try { URL getUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.connect(); //Getting the inputting stream, and then read the stream. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines = ""; while ((lines = reader.readLine()) != null) { jsonContent.append(lines); } reader.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } //ScanningActivity.log("getHttpRequest(): " + content); //BooksPutIn.log("getHttpRequest(): " + content); return jsonContent.toString(); }
From source file:Main.java
public static Bitmap getBitmapFromRemote(String address) { Bitmap bitmap = null;//from www.j ava2 s . co m try { URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
From source file:Main.java
private static int safelyConnect(HttpURLConnection connection) throws IOException { try {//from w w w . j a v a 2s .c o m connection.connect(); } catch (NullPointerException e) { // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895 throw new IOException(e); } catch (IllegalArgumentException e) { // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895 throw new IOException(e); } catch (IndexOutOfBoundsException e) { // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895 throw new IOException(e); } catch (SecurityException e) { // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895 throw new IOException(e); } try { return connection.getResponseCode(); } catch (NullPointerException e) { // this is maybe this Android bug: http://code.google.com/p/android/issues/detail?id=15554 throw new IOException(e); } catch (StringIndexOutOfBoundsException e) { // this is maybe this Android bug: http://code.google.com/p/android/issues/detail?id=15554 throw new IOException(e); } catch (IllegalArgumentException e) { // this is maybe this Android bug: http://code.google.com/p/android/issues/detail?id=15554 throw new IOException(e); } }
From source file:Main.java
private static InputStream OpenHttpConnection(String strURL, Boolean cache) throws IOException { InputStream inputStream = null; try {//from w w w.j a va 2s . c o m URL url = new URL(strURL); URLConnection conn = url.openConnection(); conn.setUseCaches(cache); HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setRequestMethod("GET"); httpConn.connect(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) inputStream = httpConn.getInputStream(); } catch (Exception ex) { } return inputStream; }