Results 1 to 1 of 1
-
Reading Web Pages with Nonblocking Channels
This Java tip shows how to read a web server response using nonblocking
channels in Java.
Java Code:import java.io.IOException; import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.Set; public class GetWebPageApp { static Selector selector; public static void main(String args[]) throws Exception { String resource, host, file; int slashPos; resource = "www.java-forums.org"; // skip HTTP:// slashPos = resource.indexOf('/'); // find host/file separator if (slashPos < 0) { resource = resource + "/"; slashPos = resource.indexOf('/'); } file = resource.substring(slashPos); // isolate host and file parts host = resource.substring(0, slashPos); System.out.println("Host to contact: '" + host + "'"); System.out.println("File to fetch : '" + file + "'"); SocketChannel channel = null; try { Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); CharBuffer charBuffer = CharBuffer.allocate(1024); InetSocketAddress socketAddress = new InetSocketAddress(host, 80); channel = SocketChannel.open(); channel.configureBlocking(false); channel.connect(socketAddress); selector = Selector.open(); channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ); while (selector.select(500) > 0) { Set readyKeys = selector.selectedKeys(); try { Iterator readyItor = readyKeys.iterator(); while (readyItor.hasNext()) { SelectionKey key = (SelectionKey) readyItor.next(); readyItor.remove(); SocketChannel keyChannel = (SocketChannel) key .channel(); if (key.isConnectable()) { if (keyChannel.isConnectionPending()) { keyChannel.finishConnect(); } String request = "GET " + file + " \r\n\r\n"; keyChannel.write(encoder.encode(CharBuffer .wrap(request))); } else if (key.isReadable()) { keyChannel.read(buffer); buffer.flip(); decoder.decode(buffer, charBuffer, false); charBuffer.flip(); System.out.print(charBuffer); buffer.clear(); charBuffer.clear(); } else { System.err.println("Unknown key"); } } } catch (ConcurrentModificationException e) { } } } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } finally { if (channel != null) { try { channel.close(); } catch (IOException ignored) { } } } System.out.println("\nDone."); } }
Similar Threads
-
How To Edit/Add JSP Pages in NetBeans IDE
By JavaForums in forum NetBeansReplies: 2Last Post: 02-17-2009, 11:14 AM -
Reading Web Pages with Socket Channels
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:00 PM -
Reading Web Pages with Streams
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 07:59 PM -
JSP pages with no caching
By Java Tip in forum Java TipReplies: 0Last Post: 01-31-2008, 12:53 PM -
Getting all the pages from a domain
By eva in forum New To JavaReplies: 0Last Post: 12-25-2007, 11:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks