List of usage examples for java.net URLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:DocumentIteratorExample.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.java2s.com"); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); HTMLEditorKit htmlKit = new HTMLEditorKit(); HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument(); HTMLEditorKit.Parser parser = new ParserDelegator(); HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0); parser.parse(br, callback, true);//www . j av a 2 s. c o m for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A); iterator.isValid(); iterator .next()) { AttributeSet attributes = iterator.getAttributes(); String srcString = (String) attributes.getAttribute(HTML.Attribute.HREF); System.out.print(srcString); int startOffset = iterator.getStartOffset(); int endOffset = iterator.getEndOffset(); int length = endOffset - startOffset; String text = htmlDoc.getText(startOffset, length); System.out.println(" - " + text); } System.exit(0); }
From source file:Main.java
public static void main(String[] argv) throws Exception { URL url = new URL("http://www.java.com/"); URLConnection urlConnection = url.openConnection(); DataInputStream dis = new DataInputStream(urlConnection.getInputStream()); String html = "", tmp = ""; while ((tmp = dis.readUTF()) != null) { html += " " + tmp; }/*from ww w.j a v a 2 s. c o m*/ dis.close(); html = html.replaceAll("\\s+", " "); Pattern p = Pattern.compile("<title>(.*?)</title>"); Matcher m = p.matcher(html); while (m.find() == true) { System.out.println(m.group(1)); } }
From source file:Main.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.google.com"); URLConnection httpCon = (URLConnection) url.openConnection(); System.out.println(URLConnection.guessContentTypeFromStream(httpCon.getInputStream())); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.java2s.com"); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); HTMLEditorKit htmlKit = new HTMLEditorKit(); HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument(); HTMLEditorKit.Parser parser = new ParserDelegator(); HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0); parser.parse(br, callback, true);/* w w w . j a va2 s . co m*/ ElementIterator iterator = new ElementIterator(htmlDoc); Element element; while ((element = iterator.next()) != null) { AttributeSet attributes = element.getAttributes(); Object name = attributes.getAttribute(StyleConstants.NameAttribute); if ((name instanceof HTML.Tag) && (name == HTML.Tag.H1)) { StringBuffer text = new StringBuffer(); int count = element.getElementCount(); for (int i = 0; i < count; i++) { Element child = element.getElement(i); AttributeSet childAttributes = child.getAttributes(); if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) { int startOffset = child.getStartOffset(); int endOffset = child.getEndOffset(); int length = endOffset - startOffset; text.append(htmlDoc.getText(startOffset, length)); } } System.out.println(name + ": " + text.toString()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { URLConnection urlc = new URL("http://www.google.com").openConnection(); BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream()); int byteRead; while ((byteRead = buffer.read()) != -1) { System.out.println((char) byteRead); }//from ww w . j av a 2s. com buffer.close(); }
From source file:com.vaadin.buildhelpers.FetchReleaseNotesTickets.java
public static void main(String[] args) throws IOException { String version = System.getProperty("vaadin.version"); if (version == null || version.equals("")) { usage();/* w w w. j av a2s . co m*/ } URL url = new URL(queryURL.replace("@version@", version)); URLConnection connection = url.openConnection(); InputStream urlStream = connection.getInputStream(); @SuppressWarnings("unchecked") List<String> tickets = IOUtils.readLines(urlStream); for (String ticket : tickets) { String[] fields = ticket.split("\t"); if ("id".equals(fields[0])) { // This is the header continue; } System.out.println(ticketTemplate.replace("@ticket@", fields[0]).replace("@description@", fields[1])); } urlStream.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.google.com"); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); HTMLEditorKit htmlKit = new HTMLEditorKit(); HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument(); HTMLEditorKit.Parser parser = new ParserDelegator(); HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0); parser.parse(br, callback, true);/*from ww w . j a v a 2s . c om*/ Element element; ElementIterator iterator = new ElementIterator(htmlDoc); while ((element = iterator.next()) != null) { AttributeSet attributes = element.getAttributes(); Object name = attributes.getAttribute(StyleConstants.NameAttribute); if ((name instanceof HTML.Tag) && (name == HTML.Tag.H1 || name == HTML.Tag.H2 || name == HTML.Tag.P)) { // Build up content text as it may be within multiple elements int count = element.getElementCount(); for (int i = 0; i < count; i++) { Element child = element.getElement(i); AttributeSet childAttributes = child.getAttributes(); if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) { int startOffset = child.getStartOffset(); int endOffset = child.getEndOffset(); int length = endOffset - startOffset; System.out.println(htmlDoc.getText(startOffset, length)); } } } } }
From source file:Main.java
public static void main(String[] args) throws Exception { URLConnection connection = new URL("http://api.androidhive.info/pizza/?format=xml").openConnection(); InputStream inputStream = connection.getInputStream(); XMLStreamReader streamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream, "UTF-8"); while (streamReader.hasNext()) if (streamReader.next() == XMLStreamConstants.START_ELEMENT) System.out.println("START_ELEMENT " + streamReader.getName()); }
From source file:Main.java
public static void main(String args[]) throws Exception { byte[] b = new byte[1]; URL url = new URL("http://www.server.com/a.gif"); URLConnection urlConnection = url.openConnection(); urlConnection.connect();/* ww w .j av a2 s . c o m*/ DataInputStream di = new DataInputStream(urlConnection.getInputStream()); FileOutputStream fo = new FileOutputStream("a.gif"); while (-1 != di.read(b, 0, 1)) fo.write(b, 0, 1); di.close(); fo.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { int c;/*from w ww. ja v a 2 s .c o m*/ URL hp = new URL("http", "www.java2s.com", 80, "/"); URLConnection hpCon = hp.openConnection(); if (hpCon.getContentLength() > 0) { InputStream input = hpCon.getInputStream(); int i = hpCon.getContentLength(); while (((c = input.read()) != -1) && (--i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No Content Available"); } }