Uniform Resource Locator: URL Hierarchy of Classes Methods of the Class Examples of Using the Class... String getContentType; String getContentLength; long getLastModified;... Forma
Trang 1INTERNET PROTOCOLS
Trang 3Uniform Resource Locator: URL
Hierarchy of Classes
Methods of the Class
Examples of Using the Class
Trang 4Hierarchy of Classes
Trang 5URL Class
Class URL is a description of a resource location on the Internet
Complete URL: http://www.cs.joensuu.fi:1547/~john/pub/index.html
Trang 6Methods of URL Class
MalformedURLException).
Trang 7Methods of URL Class
instantiated
• An InputStream object is returned and permits to retrieve information specifed
into the URL
• If the connection fails, the exception IOException is raised.
String getContentType();
String getContentLength();
long getLastModified();
Trang 8Creating a URL Instance
The following statement creates a Java URL object:
You can also build a URL by setting each part individually:
URL u = new URL(“http”, www.cs.rpi.edu,80,”/~hollingd/”);
Trang 9Retrieving Remote Files
Rather than reading the fle from the local system, this example reads the fle from a Web server
private void showFile() {
URL url = null;
Trang 10Retrieving a Resource with the URL Class
import java.net.*;
import java.io.*;
public class FtpUrls {
public static void main(String[] args) throws IOException {
// URL url = new URL("ftp://pvtinh:Tinh7570@192.168.1.6/t.txt");
URL url = new URL("http://192.168.1.2/cackhoa.html");
InputStream uin = url.openStream();
BufferedReader in = new BufferedReader(new
Trang 11SMTP
Trang 12Format of an email
Trang 13Commands and response
Trang 14Command
Trang 15Response
Trang 16Response (con)
Trang 17Status code
The Server responds with a 3 digit code that may be followed by text info
2## - Success
3## - Command can be accepted with more information
4## - Command was rejected, but error condition is temporary
5## - Command rejected, Bad User!
Trang 18Connection establishment
Trang 19F Message transfer
Trang 20FConnection termination
Trang 21After connection, we can type the SMTP commands and then receive the responses as shown below We have shown the commands in black and the responses in color Note that we have added for clarification some comment lines, designated by the “=” sign These lines are not part of the email procedure.
Example
Trang 23============= Connection Termination===============
250 Message received: adelphia.net@mail.adelphia.net
QUIT
221 mta13.adelphia.net SMTP server closing connection
Connection closed by foreign host.
Example
Trang 24SMTP & Sending E-Mail
Trang 25Simple Mail Transfer Protocol - RFC 821
To send e-mail, you make a socket connection to port 25 , the SMTP port SMTP is the Simple Mail Transport Protocol that describes the format for e-mail messages.
Open a socket to your host.
Socket s = new Socket("mail.yourserver.com", 25);
PrintWriter out = new PrintWriter(s.getOutputStream());
Send the following information to the print stream:
HELO sending host # Domain name
MAIL FROM:< sender email address >
RCPT TO:< recipient email address >
Trang 26Sending E-Mail
private BufferedReader in;
private PrintWriter out;
private JTextField from;
private JTextField to;
private JTextArea message;
……….
public void sendMail() {
try {
Socket s = new Socket(smtpServer.getText(), 25);
out = new PrintWriter(s.getOutputStream());
Trang 28public void receive() throws IOException {
String line = in.readLine();
if (line != null) {
………
}
}
Trang 29Limitations in SMTP
Only uses NVT 7 bit ASCII format
How to represent other data types?
No authentication mechanisms
Messages are sent un-encrypted
Susceptible to misuse (Spamming,
faking sender address)
Trang 31POP3
Trang 32Receive E-Mail – POP3 (RFC 1939)
// Login by sending USER and PASS commands