Readers and Writers for all text based I/O
readLine()
is a very bad idea
PrintStream
is unofficially deprecated and
PrintWriter
should be
Supports
Secure Hypertext Transfer Protocol (https)
Secure Sockets Layer version 3 (SSL3)
Transport Layer Security (TLS)
Java 1.2 and later
http://java.sun.com/products/jsse/
Export restricted
Digital IDs are required for authenticated connections
import java.net.*;
import java.io.*;
import java.security.*;
import javax.net.ssl.*;
public class HTTPSClient {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java HTTPSClient host");
return;
}
int port = 443; // default https port
String host = args[0];
try {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLSocketFactory factory
= (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
Writer out = new OutputStreamWriter(socket.getOutputStream());
// https requires the full URL in the GET line
out.write("GET http://" + host + "/ HTTP/1.1\r\n");
out.write("\r\n");
out.flush();
// read response
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
int c;
while ((c = in.read()) != -1) {
System.out.write(c);
}
out.close();
in.close();
socket.close();
}
catch (IOException e) {
System.err.println(e);
}
}
}
Added in Java 1.2 as a complement to URLEncoder
Has a single static URLDecoder.decode()
method that
decodes an x-www-form-urlencoded string
String input
= "http://www.altavista.com/cgi-bin/query?"
+ "pg=q&kl=XX&stype=stext&q=%2B%22Java+I%2FO%22&search.x=38&search.y=3";
try {
String output = URLDecoder.decode(input);
System.out.println(output);
}
In JFC 1.1.1 (included in Java 1.2.2) and later, Sun provides classes for basic HTML parsing and display.
The javax.swing.text.html.parser
package can be used to read HTML documents
The javax.swing.text.html
package allows you to display basic HTML in
your JFC based applications.
import javax.swing.text.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
public class OReillyHomePage {
public static void main(String[] args) {
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
try {
jep.setPage("http://www.oreilly.com");
}
catch (IOException e) {
jep.setContentType("text/html");
jep.setText("<html>Could not load http://www.oreilly.com </html>");
}
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("O'Reilly & Associates");
// Next line requires Java 1.3
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setSize(512, 342);
f.show();
}
}
Link action
JavaScript
Flash
QuickTime
etc.
Java 1.1 provides methods to set these options:
TCP_NODELAY
SO_TIMEOUT
SO_LINGER
Java 1.2 adds two more options:
SO_SNDBUF
SO_RCVBUF
Java 1.3 adds one more:
SO_KEEPALIVE
Standard extension written in pure Java
Uses factory design pattersn to handle mutliple protocols including:
SMTP
POP
IMAP
Others
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Assimilator {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put("mail.host", "mail.cloud9.net");
Session mailConnection = Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
Address bill = new InternetAddress("god@microsoft.com",
"Bill Gates");
Address elliotte = new InternetAddress("elharo@metalab.unc.edu");
msg.setContent("Resistance is futile. You will be assimilated!",
"text/plain");
msg.setFrom(bill);
msg.setRecipient(Message.RecipientType.TO, elliotte);
msg.setSubject("You must comply.");
Transport.send(msg);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Asynchronous, polling I/O
XML parser bundled with the JDK
IP layer access
Non-IP protocols
This presentation: http://www.ibiblio.org/xml/slides/iw2000/whatsnew/
Java Network Programming, 2nd Edition