List of usage examples for java.net URL openConnection
public URLConnection openConnection() throws java.io.IOException
From source file:Main.java
public static void main(String[] argv) throws Exception { URL url = new URI("http://www.google.com").toURL(); URLConnection conn = url.openConnection(); Reader rd = new InputStreamReader(conn.getInputStream()); EditorKit kit = new HTMLEditorKit(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); kit.read(rd, doc, 0);/* ww w .java2 s. co m*/ HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A); while (it.isValid()) { SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes(); String link = (String) s.getAttribute(HTML.Attribute.HREF); if (link != null) { System.out.println(link); } it.next(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] b = new byte[1]; Properties systemSettings = System.getProperties(); systemSettings.put("http.proxyHost", "proxy.mydomain.local"); systemSettings.put("http.proxyPort", "80"); URL u = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); BASE64Encoder encoder = new BASE64Encoder(); String encodedUserPwd = encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes()); con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd); DataInputStream di = new DataInputStream(con.getInputStream()); while (-1 != di.read(b, 0, 1)) { System.out.print(new String(b)); }// w ww .j a v a 2s . co m }
From source file:MainClass.java
public static void main(String args[]) throws Exception { int c;/*from w w w . ja v a 2 s.com*/ 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"); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] b = new byte[1]; Properties systemSettings = System.getProperties(); systemSettings.put("http.proxyHost", "proxy.mydomain.local"); systemSettings.put("http.proxyPort", "80"); Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("mydomain\\username", "password".toCharArray()); }//from ww w .j av a2 s .c o m }); URL u = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); DataInputStream di = new DataInputStream(con.getInputStream()); while (-1 != di.read(b, 0, 1)) { System.out.print(new String(b)); } }
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/postedresults.jsp"); URLConnection conn = url.openConnection(); conn.setDoInput(true);// ww w .j a v a 2 s .c o m conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); String content = "CONTENT=HELLO JSP !&ONEMORECONTENT =HELLO POST !"; out.writeBytes(content); out.flush(); out.close(); DataInputStream in = new DataInputStream(conn.getInputStream()); String str; while (null != ((str = in.readUTF()))) { System.out.println(str); } in.close(); }
From source file:Main.java
public static void main(String s[]) throws Exception { try {//from w ww . j a va2 s .c o m Properties systemSettings = System.getProperties(); systemSettings.put("proxySet", "true"); systemSettings.put("http.proxyHost", "proxy.mycompany.local"); systemSettings.put("http.proxyPort", "80"); URL u = new URL("http://www.java.com"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); String encodedUserPwd = encoder.encode("domain\\username:password".getBytes()); con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd); con.setRequestMethod("HEAD"); System.out.println(con.getResponseCode() + " : " + con.getResponseMessage()); System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { e.printStackTrace(); System.out.println(false); } }
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);//from w w w . jav 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); } }
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);/*w w w .j a v 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); } }
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);/* ww w . ja va2 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); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { URL u = new URL("http://www.java2s.com"); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); int code = uc.getResponseCode(); String response = uc.getResponseMessage(); System.out.println("HTTP/1.x " + code + " " + response); for (int j = 1;; j++) { String header = uc.getHeaderField(j); String key = uc.getHeaderFieldKey(j); if (header == null || key == null) break; System.out.println(uc.getHeaderFieldKey(j) + ": " + header); }// www . j a v a 2s . c o m InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } }