<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FNetwork_Protocol%2FWeb_Server_Client</id>
		<title>Java/Network Protocol/Web Server Client - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FNetwork_Protocol%2FWeb_Server_Client"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Network_Protocol/Web_Server_Client&amp;action=history"/>
		<updated>2026-04-07T07:46:25Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/Network_Protocol/Web_Server_Client&amp;diff=8913&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Network_Protocol/Web_Server_Client&amp;diff=8913&amp;oldid=prev"/>
				<updated>2010-06-01T07:21:25Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 07:21, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/Network_Protocol/Web_Server_Client&amp;diff=8912&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Network_Protocol/Web_Server_Client&amp;diff=8912&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:47Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== A Simple Web Server ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Common Port Assignments and Corresponding RFC Numbers              &lt;br /&gt;
Port Common Name RFC#  Purpose&lt;br /&gt;
7     Echo        862   Echoes data back. Used mostly for testing.&lt;br /&gt;
9     Discard     863   Discards all data sent to it. Used mostly for testing.&lt;br /&gt;
13    Daytime     867   Gets the date and time.&lt;br /&gt;
17    Quotd       865   Gets the quote of the day.&lt;br /&gt;
19    Chargen     864   Generates characters. Used mostly for testing.&lt;br /&gt;
20    ftp-data    959   Transfers files. FTP stands for File Transfer Protocol.&lt;br /&gt;
21    ftp         959   Transfers files as well as commands.&lt;br /&gt;
23    telnet      854   Logs on to remote systems.&lt;br /&gt;
25    SMTP        821   Transfers Internet mail. Stands for Simple Mail Transfer Protocol.&lt;br /&gt;
37    Time        868   Determines the system time on computers.&lt;br /&gt;
43    whois       954   Determines a user&amp;quot;s name on a remote system.&lt;br /&gt;
70    gopher     1436   Looks up documents, but has been mostly replaced by HTTP.&lt;br /&gt;
79    finger     1288   Determines information about users on other systems.&lt;br /&gt;
80    http       1945   Transfer documents. Forms the foundation of the Web.&lt;br /&gt;
110   pop3       1939   Accesses message stored on servers. Stands for Post Office Protocol, version 3.&lt;br /&gt;
443   https      n/a    Allows HTTP communications to be secure. Stands for Hypertext Transfer Protocol over Secure Sockets Layer (SSL).&lt;br /&gt;
*/&lt;br /&gt;
///A Simple Web Server (WebServer.java)&lt;br /&gt;
import java.io.BufferedReader;&lt;br /&gt;
import java.io.InputStreamReader;&lt;br /&gt;
import java.io.PrintWriter;&lt;br /&gt;
import java.net.ServerSocket;&lt;br /&gt;
import java.net.Socket;&lt;br /&gt;
/**&lt;br /&gt;
 * Example program from Chapter 1 Programming Spiders, Bots and Aggregators in&lt;br /&gt;
 * Java Copyright 2001 by Jeff Heaton&lt;br /&gt;
 * &lt;br /&gt;
 * WebServer is a very simple web-server. Any request is responded with a very&lt;br /&gt;
 * simple web-page.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Jeff Heaton&lt;br /&gt;
 * @version 1.0&lt;br /&gt;
 */&lt;br /&gt;
/*&lt;br /&gt;
 * Examining the Mini Web Server&lt;br /&gt;
 * &lt;br /&gt;
 * Server sockets use the ServerSocket object rather than the Socket object that&lt;br /&gt;
 * client sockets use. There are several constructors available with the&lt;br /&gt;
 * ServerSocket object. The simplest constructor accepts only the port number on&lt;br /&gt;
 * which the program should be listening. Listening refers to the mode that a&lt;br /&gt;
 * server is in while it waits for clients to connect. The following lines of&lt;br /&gt;
 * code are used in Listing 1.3 to create a new ServerSocket object and reserve&lt;br /&gt;
 * port 80 as the port number on which the web server should listen for&lt;br /&gt;
 * connections:&lt;br /&gt;
 * &lt;br /&gt;
 * try { &lt;br /&gt;
 * // create the main server &lt;br /&gt;
 * socket s = new ServerSocket(80); &lt;br /&gt;
 * }&lt;br /&gt;
 * catch(Exception e) { &lt;br /&gt;
 * System.out.println(&amp;quot;Error: &amp;quot; + e ); &lt;br /&gt;
 * return; &lt;br /&gt;
 * }&lt;br /&gt;
 * &lt;br /&gt;
 * The try block is necessary because any number of errors could occur when the&lt;br /&gt;
 * program attempts to register port 80. The most common error that would result&lt;br /&gt;
 * is that there is already a server listening to port 80 on this machine.&lt;br /&gt;
 * Warning&lt;br /&gt;
 * &lt;br /&gt;
 * This program will not work on a machine that already has a web server, or&lt;br /&gt;
 * some other program, listening on port 80.&lt;br /&gt;
 * &lt;br /&gt;
 * Once the program has port 80 registered, it can begin listening for&lt;br /&gt;
 * connections. The following line of code is used to wait for a connection:&lt;br /&gt;
 * &lt;br /&gt;
 * Socket remote = s.accept();&lt;br /&gt;
 * &lt;br /&gt;
 * The Socket object that is returned by accept is exactly the same class that&lt;br /&gt;
 * is used for client sockets. Once the connection is established, the&lt;br /&gt;
 * difference between client and server sockets fade. The primary difference&lt;br /&gt;
 * between client and server sockets is the way in which they connect. A client&lt;br /&gt;
 * sever connects to something. A server socket waits for something to connect&lt;br /&gt;
 * to it.&lt;br /&gt;
 * &lt;br /&gt;
 * The accept method is a blocking call, which means the current thread will&lt;br /&gt;
 * wait for a connection. This can present problems for your program if there&lt;br /&gt;
 * are other tasks it would like to accomplish while it is waiting for&lt;br /&gt;
 * connections. Because of this, it is very common to see the accept method call&lt;br /&gt;
 * placed in a worker thread. This allows the main thread to carry on other&lt;br /&gt;
 * tasks, while the worker thread waits for connections to arrive.&lt;br /&gt;
 * &lt;br /&gt;
 * Once a connection is made, the accept method will return a socket object for&lt;br /&gt;
 * the new socket. After this point, reading and writing is the same between&lt;br /&gt;
 * client and server sockets. Many client server programs would create a new&lt;br /&gt;
 * thread to handle this new connection.&lt;br /&gt;
 * &lt;br /&gt;
 * Now that a connection has been made, a new thread could be created to handle&lt;br /&gt;
 * it. This new worker thread would process all the requests from this client in&lt;br /&gt;
 * the background, which allows the ServerSocket object to wait for and service&lt;br /&gt;
 * more connections. However, the example program in Listing 1.3 does not&lt;br /&gt;
 * require such programming. As soon as the socket is accepted, input and output&lt;br /&gt;
 * objects are created; this same process was used with the SMTP client. The&lt;br /&gt;
 * following lines from Listing 1.3 show the process of preparing the newly&lt;br /&gt;
 * accepted socket for input and output:&lt;br /&gt;
 * &lt;br /&gt;
 * //remote is now the connected socket &lt;br /&gt;
 * System.out.println(&amp;quot;Connection, sending data.&amp;quot;); &lt;br /&gt;
 * BufferedReader in &lt;br /&gt;
 * = new BufferedReader( new InputStreamReader(remote.getInputStream()) ); &lt;br /&gt;
 * PrintWriter out = new PrintWriter(remote.getOutputStream());&lt;br /&gt;
 * &lt;br /&gt;
 * Now that the program has input and output objects, it can process the HTTP&lt;br /&gt;
 * request. It first reads the HTTP request lines. A full-featured server would&lt;br /&gt;
 * parse each line and determine the exact nature of this request, however, our&lt;br /&gt;
 * ultra-simple web server just reads in the request lines and ignores them, as&lt;br /&gt;
 * shown here:&lt;br /&gt;
 * &lt;br /&gt;
 * //read the data sent. We basically ignore it, &lt;br /&gt;
 * //stop reading once a blank line is hit. This &lt;br /&gt;
 * //blank line signals the end of the &lt;br /&gt;
 * //client HTTP headers.&lt;br /&gt;
 *  String str=&amp;quot;.&amp;quot;; &lt;br /&gt;
 * while(!str.equals(&amp;quot;&amp;quot;)) &lt;br /&gt;
 * str = in.readLine();&lt;br /&gt;
 * &lt;br /&gt;
 * These lines cause the server to read in lines of text from the newly&lt;br /&gt;
 * connected socket. Once a blank line (which indicates the end of the HTTP&lt;br /&gt;
 * header) is reached, the loop stops, and the server stops reading. Now that&lt;br /&gt;
 * the HTTP header has been retrieved, the server sends an HTTP response. The&lt;br /&gt;
 * following lines of code accomplish this:&lt;br /&gt;
 * &lt;br /&gt;
 * //Send the response &lt;br /&gt;
 * //Send the headers out.println(&amp;quot;HTTP/1.0 200 OK&amp;quot;);&lt;br /&gt;
 * out.println(&amp;quot;Content-Type: text/html&amp;quot;); &lt;br /&gt;
 * out.println(&amp;quot;Server: Bot&amp;quot;); &lt;br /&gt;
 * //this blank line signals the end of the headers out.println(&amp;quot;&amp;quot;);&lt;br /&gt;
 * // Send the HTML page out.println( &amp;quot; &amp;lt;H1&amp;gt; Welcome to the Ultra Mini-WebServer &amp;lt;/H2&amp;gt; &amp;quot;);&lt;br /&gt;
 * &lt;br /&gt;
 * Status code 200, as shown on line 3 of the preceding code, is used to show&lt;br /&gt;
 * that the page was properly transferred, and that the required HTTP headers&lt;br /&gt;
 * were sent. (Refer to Chapter 2 for more information about HTTP headers.)&lt;br /&gt;
 * Following the HTTP headers, the actual HTML page is transferred. Once the&lt;br /&gt;
 * page is transferred, the following lines of code from Listing 1.3 are&lt;br /&gt;
 * executed to clean up:&lt;br /&gt;
 * &lt;br /&gt;
 * out.flush(); remote.close();&lt;br /&gt;
 * &lt;br /&gt;
 * The flush method is necessary to ensure that all data is transferred, and the&lt;br /&gt;
 * close method is necessary to close the socket. Although Java will discard the&lt;br /&gt;
 * Socket object, it will not generally close the socket on most platforms.&lt;br /&gt;
 * Because of this, you must close the socket or else you might eventually get&lt;br /&gt;
 * an error indicating that there are no more file handles. This becomes very&lt;br /&gt;
 * important for a program that opens up many connections, including one to a&lt;br /&gt;
 * spider.&lt;br /&gt;
 */&lt;br /&gt;
public class WebServer {&lt;br /&gt;
  /**&lt;br /&gt;
   * WebServer constructor.&lt;br /&gt;
   */&lt;br /&gt;
  protected void start() {&lt;br /&gt;
    ServerSocket s;&lt;br /&gt;
    System.out.println(&amp;quot;Webserver starting up on port 80&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;(press ctrl-c to exit)&amp;quot;);&lt;br /&gt;
    try {&lt;br /&gt;
      // create the main server socket&lt;br /&gt;
      s = new ServerSocket(80);&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      System.out.println(&amp;quot;Error: &amp;quot; + e);&lt;br /&gt;
      return;&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Waiting for connection&amp;quot;);&lt;br /&gt;
    for (;;) {&lt;br /&gt;
      try {&lt;br /&gt;
        // wait for a connection&lt;br /&gt;
        Socket remote = s.accept();&lt;br /&gt;
        // remote is now the connected socket&lt;br /&gt;
        System.out.println(&amp;quot;Connection, sending data.&amp;quot;);&lt;br /&gt;
        BufferedReader in = new BufferedReader(new InputStreamReader(&lt;br /&gt;
            remote.getInputStream()));&lt;br /&gt;
        PrintWriter out = new PrintWriter(remote.getOutputStream());&lt;br /&gt;
        // read the data sent. We basically ignore it,&lt;br /&gt;
        // stop reading once a blank line is hit. This&lt;br /&gt;
        // blank line signals the end of the client HTTP&lt;br /&gt;
        // headers.&lt;br /&gt;
        String str = &amp;quot;.&amp;quot;;&lt;br /&gt;
        while (!str.equals(&amp;quot;&amp;quot;))&lt;br /&gt;
          str = in.readLine();&lt;br /&gt;
        // Send the response&lt;br /&gt;
        // Send the headers&lt;br /&gt;
        out.println(&amp;quot;HTTP/1.0 200 OK&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;Content-Type: text/html&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;Server: Bot&amp;quot;);&lt;br /&gt;
        // this blank line signals the end of the headers&lt;br /&gt;
        out.println(&amp;quot;&amp;quot;);&lt;br /&gt;
        // Send the HTML page&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;H1&amp;gt;Welcome to the Ultra Mini-WebServer&amp;lt;/H2&amp;gt;&amp;quot;);&lt;br /&gt;
        out.flush();&lt;br /&gt;
        remote.close();&lt;br /&gt;
      } catch (Exception e) {&lt;br /&gt;
        System.out.println(&amp;quot;Error: &amp;quot; + e);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Start the application.&lt;br /&gt;
   * &lt;br /&gt;
   * @param args&lt;br /&gt;
   *            Command line parameters are not used.&lt;br /&gt;
   */&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    WebServer ws = new WebServer();&lt;br /&gt;
    ws.start();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Connect with a Web server ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.net.InetAddress;&lt;br /&gt;
import java.net.Socket;&lt;br /&gt;
public class WebPing {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    try {&lt;br /&gt;
      InetAddress addr;&lt;br /&gt;
      Socket sock = new Socket(&amp;quot;www.jexp.ru&amp;quot;, 80);&lt;br /&gt;
      addr = sock.getInetAddress();&lt;br /&gt;
      System.out.println(&amp;quot;Connected to &amp;quot; + addr);&lt;br /&gt;
      sock.close();&lt;br /&gt;
    } catch (java.io.IOException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Can&amp;quot;t connect to &amp;quot; + args[0]);&lt;br /&gt;
      System.out.println(e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java HTTP/HTTPS Server Based on new io ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reading URLs Protected with HTTP Authentication ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import java.io.BufferedReader;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.InputStreamReader;&lt;br /&gt;
import java.net.Authenticator;&lt;br /&gt;
import java.net.MalformedURLException;&lt;br /&gt;
import java.net.PasswordAuthentication;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
public class AuthDemo {&lt;br /&gt;
  public static void main(String args[]) throws MalformedURLException,&lt;br /&gt;
      IOException {&lt;br /&gt;
    String urlString = &amp;quot;&amp;quot;;&lt;br /&gt;
    String username = &amp;quot;&amp;quot;;&lt;br /&gt;
    String password = &amp;quot;&amp;quot;;&lt;br /&gt;
    Authenticator.setDefault(new MyAuthenticator(username, password));&lt;br /&gt;
    URL url = new URL(urlString);&lt;br /&gt;
    InputStream content = (InputStream) url.getContent();&lt;br /&gt;
    BufferedReader in = new BufferedReader(new InputStreamReader(content));&lt;br /&gt;
    String line;&lt;br /&gt;
    while ((line = in.readLine()) != null) {&lt;br /&gt;
      System.out.println(line);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Done.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  static class MyAuthenticator extends Authenticator {&lt;br /&gt;
    private String username, password;&lt;br /&gt;
    public MyAuthenticator(String user, String pass) {&lt;br /&gt;
      username = user;&lt;br /&gt;
      password = pass;&lt;br /&gt;
    }&lt;br /&gt;
    protected PasswordAuthentication getPasswordAuthentication() {&lt;br /&gt;
      System.out.println(&amp;quot;Requesting Host  : &amp;quot; + getRequestingHost());&lt;br /&gt;
      System.out.println(&amp;quot;Requesting Port  : &amp;quot; + getRequestingPort());&lt;br /&gt;
      System.out.println(&amp;quot;Requesting Prompt : &amp;quot; + getRequestingPrompt());&lt;br /&gt;
      System.out.println(&amp;quot;Requesting Protocol: &amp;quot;&lt;br /&gt;
          + getRequestingProtocol());&lt;br /&gt;
      System.out.println(&amp;quot;Requesting Scheme : &amp;quot; + getRequestingScheme());&lt;br /&gt;
      System.out.println(&amp;quot;Requesting Site  : &amp;quot; + getRequestingSite());&lt;br /&gt;
      return new PasswordAuthentication(username, password.toCharArray());&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reading Web Pages with Nonblocking Channels ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.net.InetSocketAddress;&lt;br /&gt;
import java.net.UnknownHostException;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.CharBuffer;&lt;br /&gt;
import java.nio.channels.SelectionKey;&lt;br /&gt;
import java.nio.channels.Selector;&lt;br /&gt;
import java.nio.channels.SocketChannel;&lt;br /&gt;
import java.nio.charset.Charset;&lt;br /&gt;
import java.nio.charset.CharsetDecoder;&lt;br /&gt;
import java.nio.charset.CharsetEncoder;&lt;br /&gt;
import java.util.ConcurrentModificationException;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
public class GetWebPageApp {&lt;br /&gt;
  static Selector selector;&lt;br /&gt;
  public static void main(String args[]) throws Exception {&lt;br /&gt;
    String resource, host, file;&lt;br /&gt;
    int slashPos;&lt;br /&gt;
    resource = &amp;quot;www.jexp.ru/index.htm&amp;quot;; // skip HTTP://&lt;br /&gt;
    slashPos = resource.indexOf(&amp;quot;/&amp;quot;); // find host/file separator&lt;br /&gt;
    if (slashPos &amp;lt; 0) {&lt;br /&gt;
      resource = resource + &amp;quot;/&amp;quot;;&lt;br /&gt;
      slashPos = resource.indexOf(&amp;quot;/&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    file = resource.substring(slashPos); // isolate host and file parts&lt;br /&gt;
    host = resource.substring(0, slashPos);&lt;br /&gt;
    System.out.println(&amp;quot;Host to contact: &amp;quot;&amp;quot; + host + &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;File to fetch : &amp;quot;&amp;quot; + file + &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
    SocketChannel channel = null;&lt;br /&gt;
    try {&lt;br /&gt;
      Charset charset = Charset.forName(&amp;quot;ISO-8859-1&amp;quot;);&lt;br /&gt;
      CharsetDecoder decoder = charset.newDecoder();&lt;br /&gt;
      CharsetEncoder encoder = charset.newEncoder();&lt;br /&gt;
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);&lt;br /&gt;
      CharBuffer charBuffer = CharBuffer.allocate(1024);&lt;br /&gt;
      InetSocketAddress socketAddress = new InetSocketAddress(host, 80);&lt;br /&gt;
      channel = SocketChannel.open();&lt;br /&gt;
      channel.configureBlocking(false);&lt;br /&gt;
      channel.connect(socketAddress);&lt;br /&gt;
      selector = Selector.open();&lt;br /&gt;
      channel.register(selector, SelectionKey.OP_CONNECT&lt;br /&gt;
          | SelectionKey.OP_READ);&lt;br /&gt;
      while (selector.select(500) &amp;gt; 0) {&lt;br /&gt;
        Set readyKeys = selector.selectedKeys();&lt;br /&gt;
        try {&lt;br /&gt;
          Iterator readyItor = readyKeys.iterator();&lt;br /&gt;
          while (readyItor.hasNext()) {&lt;br /&gt;
            SelectionKey key = (SelectionKey) readyItor.next();&lt;br /&gt;
            readyItor.remove();&lt;br /&gt;
            SocketChannel keyChannel = (SocketChannel) key&lt;br /&gt;
                .channel();&lt;br /&gt;
            if (key.isConnectable()) {&lt;br /&gt;
              if (keyChannel.isConnectionPending()) {&lt;br /&gt;
                keyChannel.finishConnect();&lt;br /&gt;
              }&lt;br /&gt;
              String request = &amp;quot;GET &amp;quot; + file + &amp;quot; \r\n\r\n&amp;quot;;&lt;br /&gt;
              keyChannel.write(encoder.encode(CharBuffer&lt;br /&gt;
                  .wrap(request)));&lt;br /&gt;
            } else if (key.isReadable()) {&lt;br /&gt;
              keyChannel.read(buffer);&lt;br /&gt;
              buffer.flip();&lt;br /&gt;
              decoder.decode(buffer, charBuffer, false);&lt;br /&gt;
              charBuffer.flip();&lt;br /&gt;
              System.out.print(charBuffer);&lt;br /&gt;
              buffer.clear();&lt;br /&gt;
              charBuffer.clear();&lt;br /&gt;
            } else {&lt;br /&gt;
              System.err.println(&amp;quot;Unknown key&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
        } catch (ConcurrentModificationException e) {&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    } catch (UnknownHostException e) {&lt;br /&gt;
      System.err.println(e);&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      System.err.println(e);&lt;br /&gt;
    } finally {&lt;br /&gt;
      if (channel != null) {&lt;br /&gt;
        try {&lt;br /&gt;
          channel.close();&lt;br /&gt;
        } catch (IOException ignored) {&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;\nDone.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reading Web Pages, with Socket Channels ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.net.InetSocketAddress;&lt;br /&gt;
import java.net.UnknownHostException;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.CharBuffer;&lt;br /&gt;
import java.nio.channels.SocketChannel;&lt;br /&gt;
import java.nio.charset.Charset;&lt;br /&gt;
import java.nio.charset.CharsetDecoder;&lt;br /&gt;
import java.nio.charset.CharsetEncoder;&lt;br /&gt;
public class GetWebPageDemo {&lt;br /&gt;
  public static void main(String args[]) throws Exception {&lt;br /&gt;
    String resource, host, file;&lt;br /&gt;
    int slashPos;&lt;br /&gt;
    resource = &amp;quot;www.jexp.ru/index.htm&amp;quot;; &lt;br /&gt;
    slashPos = resource.indexOf(&amp;quot;/&amp;quot;); // find host/file separator&lt;br /&gt;
    if (slashPos &amp;lt; 0) {&lt;br /&gt;
      resource = resource + &amp;quot;/&amp;quot;;&lt;br /&gt;
      slashPos = resource.indexOf(&amp;quot;/&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    file = resource.substring(slashPos); // isolate host and file parts&lt;br /&gt;
    host = resource.substring(0, slashPos);&lt;br /&gt;
    System.out.println(&amp;quot;Host to contact: &amp;quot;&amp;quot; + host + &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;File to fetch : &amp;quot;&amp;quot; + file + &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
    SocketChannel channel = null;&lt;br /&gt;
    try {&lt;br /&gt;
      Charset charset = Charset.forName(&amp;quot;ISO-8859-1&amp;quot;);&lt;br /&gt;
      CharsetDecoder decoder = charset.newDecoder();&lt;br /&gt;
      CharsetEncoder encoder = charset.newEncoder();&lt;br /&gt;
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);&lt;br /&gt;
      CharBuffer charBuffer = CharBuffer.allocate(1024);&lt;br /&gt;
      InetSocketAddress socketAddress = new InetSocketAddress(host, 80);&lt;br /&gt;
      channel = SocketChannel.open();&lt;br /&gt;
      channel.connect(socketAddress);&lt;br /&gt;
      String request = &amp;quot;GET &amp;quot; + file + &amp;quot; \r\n\r\n&amp;quot;;&lt;br /&gt;
      channel.write(encoder.encode(CharBuffer.wrap(request)));&lt;br /&gt;
      while ((channel.read(buffer)) != -1) {&lt;br /&gt;
        buffer.flip();&lt;br /&gt;
        decoder.decode(buffer, charBuffer, false);&lt;br /&gt;
        charBuffer.flip();&lt;br /&gt;
        System.out.println(charBuffer);&lt;br /&gt;
        buffer.clear();&lt;br /&gt;
        charBuffer.clear();&lt;br /&gt;
      }&lt;br /&gt;
    } catch (UnknownHostException e) {&lt;br /&gt;
      System.err.println(e);&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      System.err.println(e);&lt;br /&gt;
    } finally {&lt;br /&gt;
      if (channel != null) {&lt;br /&gt;
        try {&lt;br /&gt;
          channel.close();&lt;br /&gt;
        } catch (IOException ignored) {&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;\nDone.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reading Web Pages with Streams ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import java.io.BufferedReader;&lt;br /&gt;
import java.io.DataInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.InputStreamReader;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
import java.io.OutputStreamWriter;&lt;br /&gt;
import java.io.PrintStream;&lt;br /&gt;
import java.io.PrintWriter;&lt;br /&gt;
import java.net.InetAddress;&lt;br /&gt;
import java.net.Socket;&lt;br /&gt;
import java.net.UnknownHostException;&lt;br /&gt;
public class GetWebPage {&lt;br /&gt;
  public static void main(String args[]) throws IOException,&lt;br /&gt;
      UnknownHostException {&lt;br /&gt;
    String resource, host, file;&lt;br /&gt;
    int slashPos;&lt;br /&gt;
    resource = &amp;quot;http://www.jexp.ru/index.htm&amp;quot;.substring(7); // skip HTTP://&lt;br /&gt;
    slashPos = resource.indexOf(&amp;quot;/&amp;quot;); &lt;br /&gt;
    if (slashPos &amp;lt; 0) {&lt;br /&gt;
      resource = resource + &amp;quot;/&amp;quot;;&lt;br /&gt;
      slashPos = resource.indexOf(&amp;quot;/&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    file = resource.substring(slashPos); // isolate host and file parts&lt;br /&gt;
    host = resource.substring(0, slashPos);&lt;br /&gt;
    System.out.println(&amp;quot;Host to contact: &amp;quot;&amp;quot; + host + &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;File to fetch : &amp;quot;&amp;quot; + file + &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
    MyHTTPConnection webConnection = new MyHTTPConnection(host);&lt;br /&gt;
    if (webConnection != null) {&lt;br /&gt;
      BufferedReader in = webConnection.get(file);&lt;br /&gt;
      String line;&lt;br /&gt;
      while ((line = in.readLine()) != null) { // read until EOF&lt;br /&gt;
        System.out.println(line);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;\nDone.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  static class MyHTTPConnection {&lt;br /&gt;
    public final static int HTTP_PORT = 80;&lt;br /&gt;
    InetAddress wwwHost;&lt;br /&gt;
    DataInputStream dataInputStream;&lt;br /&gt;
    PrintStream outputStream;&lt;br /&gt;
    public MyHTTPConnection(String host) throws UnknownHostException {&lt;br /&gt;
      wwwHost = InetAddress.getByName(host);&lt;br /&gt;
      System.out.println(&amp;quot;WWW host = &amp;quot; + wwwHost);&lt;br /&gt;
    }&lt;br /&gt;
    public BufferedReader get(String file) throws IOException {&lt;br /&gt;
      Socket httpPipe;&lt;br /&gt;
      InputStream in;&lt;br /&gt;
      OutputStream out;&lt;br /&gt;
      BufferedReader bufReader;&lt;br /&gt;
      PrintWriter printWinter;&lt;br /&gt;
      httpPipe = new Socket(wwwHost, HTTP_PORT);&lt;br /&gt;
      if (httpPipe == null) {&lt;br /&gt;
        return null;&lt;br /&gt;
      }&lt;br /&gt;
      // get raw streams&lt;br /&gt;
      in = httpPipe.getInputStream();&lt;br /&gt;
      out = httpPipe.getOutputStream();&lt;br /&gt;
      // turn into useful ones&lt;br /&gt;
      bufReader = new BufferedReader(new InputStreamReader(in));&lt;br /&gt;
      printWinter = new PrintWriter(new OutputStreamWriter(out), true);&lt;br /&gt;
      if (in == null || out == null || bufReader == null || printWinter == null) {&lt;br /&gt;
        System.out.println(&amp;quot;Failed to open streams to socket.&amp;quot;);&lt;br /&gt;
        return null;&lt;br /&gt;
      }&lt;br /&gt;
      // send GET request&lt;br /&gt;
      System.out.println(&amp;quot;GET &amp;quot; + file + &amp;quot; HTTP/1.0\n&amp;quot;);&lt;br /&gt;
      printWinter.println(&amp;quot;GET &amp;quot; + file + &amp;quot; HTTP/1.0\n&amp;quot;);&lt;br /&gt;
      // read response until blank separator line&lt;br /&gt;
      String response;&lt;br /&gt;
      while ((response = bufReader.readLine()).length() &amp;gt; 0) {&lt;br /&gt;
        System.out.println(response);&lt;br /&gt;
      }&lt;br /&gt;
      return bufReader; &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Save binary file from web ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.net.MalformedURLException;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
import java.net.URLConnection;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    try {&lt;br /&gt;
      URL root = new URL(&amp;quot;http://&amp;quot;);&lt;br /&gt;
      saveBinaryFile(root);&lt;br /&gt;
    } catch (MalformedURLException e) {&lt;br /&gt;
      System.err.println(&amp;quot;not URL I understand.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void saveBinaryFile(URL u) {&lt;br /&gt;
    int bufferLength = 128;&lt;br /&gt;
    try {&lt;br /&gt;
      URLConnection uc = u.openConnection();&lt;br /&gt;
      String ct = uc.getContentType();&lt;br /&gt;
      int contentLength = uc.getContentLength();&lt;br /&gt;
      if (ct.startsWith(&amp;quot;text/&amp;quot;) || contentLength == -1) {&lt;br /&gt;
        System.err.println(&amp;quot;This is not a binary file.&amp;quot;);&lt;br /&gt;
        return;&lt;br /&gt;
      }&lt;br /&gt;
      InputStream stream = uc.getInputStream();&lt;br /&gt;
      byte[] buffer = new byte[contentLength];&lt;br /&gt;
      int bytesread = 0;&lt;br /&gt;
      int offset = 0;&lt;br /&gt;
      while (bytesread &amp;gt;= 0) {&lt;br /&gt;
        bytesread = stream.read(buffer, offset, bufferLength);&lt;br /&gt;
        if (bytesread == -1)&lt;br /&gt;
          break;&lt;br /&gt;
        offset += bytesread;&lt;br /&gt;
      }&lt;br /&gt;
      if (offset != contentLength) {&lt;br /&gt;
        System.err.println(&amp;quot;Error: Only read &amp;quot; + offset + &amp;quot; bytes&amp;quot;);&lt;br /&gt;
        System.err.println(&amp;quot;Expected &amp;quot; + contentLength + &amp;quot; bytes&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      String theFile = u.getFile();&lt;br /&gt;
      theFile = theFile.substring(theFile.lastIndexOf(&amp;quot;/&amp;quot;) + 1);&lt;br /&gt;
      FileOutputStream fout = new FileOutputStream(theFile);&lt;br /&gt;
      fout.write(buffer);&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      System.err.println(e);&lt;br /&gt;
    }&lt;br /&gt;
    return;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Web server ==&lt;/div&gt;</summary>
			</entry>

	</feed>