📄 grabpage.java
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.net.*;
import java.io.*;
public class GrabPage {
public GrabPage (String textURL) throws IOException {
dissect (textURL);
}
protected String host, file;
protected int port;
protected void dissect (String textURL) throws MalformedURLException {
URL url = new URL (textURL);
host = url.getHost ();
port = url.getPort ();
if (port == -1)
port = 80;
file = url.getFile ();
}
public void grab () throws IOException {
connect ();
try {
fetch ();
} finally {
disconnect ();
}
}
protected Writer writer;
protected BufferedReader reader;
protected void connect () throws IOException {
Socket socket = new Socket (host, port);
OutputStream out = socket.getOutputStream ();
writer = new OutputStreamWriter (out, "latin1");
InputStream in = socket.getInputStream ();
Reader reader = new InputStreamReader (in, "latin1");
this.reader = new BufferedReader (reader);
}
protected void fetch () throws IOException {
writer.write ("GET " + file + " HTTP/1.0\r\n\n");
writer.flush ();
PrintWriter console = new PrintWriter (System.out);
String input;
while ((input = reader.readLine ()) != null)
console.println (input);
console.flush ();
}
protected void disconnect () throws IOException {
reader.close ();
}
public static void main (String[] args) throws IOException {
Reader kbd = new FileReader (FileDescriptor.in);
BufferedReader bufferedKbd = new BufferedReader (kbd);
while (true) {
String textURL;
System.out.print ("Enter a URL: ");
System.out.flush ();
if ((textURL = bufferedKbd.readLine ()) == null)
break;
try {
GrabPage grabPage = new GrabPage (textURL);
grabPage.grab ();
} catch (IOException ex) {
ex.printStackTrace ();
continue;
}
System.out.println ("- OK -");
}
System.out.println ("exit");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -