KHKJH
KHKJH
programming
Network programming
in .Net Framework
Use of network
protocols HTTP,
SMTP, FTP
Contents
1. Review of HTTP protocol............................................. 4
1.1. Example of HTTP-request.............................................8
1.2. Example of HTTP-response..........................................8
2. Classes for Working with the HTTP............................. 9
2.1. Sending Requests Using HttpWebRequest Class........9
2.2. Receiving the Replies Using HttpWebResponse
Class.......................................................................................10
2.3. Setting Up Query Headings .......................................10
2.4. Reading Reply Headings..............................................11
2.5. Use of WebClient class.................................................12
3. Working with Electronic Mails................................... 16
2.1. Overview of post office protocols
SMTP, POP3, IMAP............................................................16
Contents
3
Lesson 6
4
1. Review of HTTP Protocol
5
Lesson 6
6
1. Review of HTTP Protocol
7
Lesson 6
8
2. Classes for Working with the HTTP
HttpWebRequest reqw=(HttpWebRequest)HttpWebRequest.
Create("http://itstep.org");
reqw.GetResponse();
HttpWebRequest reqw=(HttpWebRequest)HttpWebRequest.
Create("http://itstep.org");
HttpWebResponse resp= (HttpWebResponse)
reqw.GetResponse();// create a response object
StreamReader sr=new StreamReader(resp.
GetResponseStream(),Encoding.Default);//create a
//stream for reading a response
Console.WriteLine(sr.ReadToEnd()); // to display on a
//screen everything that can be read
sr.Close();
10
2. Classes for Working with the HTTP
11
Lesson 6
12
2. Classes for Working with the HTTP
13
Lesson 6
14
2. Classes for Working with the HTTP
}
HttpWebResponse rez = (HttpWebResponse)
req.GetResponse();
StreamReader sr = new StreamReader(rez.
GetResponseStream(), Encoding.Default);
response.Text = sr.ReadToEnd();
}
}
}
15
Lesson 6
3. Working with
Electronic Mails
2.1. Overview of Post Office Protocols
SMTP, POP3, IMAP
Today sending and receiving electronic mails is performed
by means of post office protocols (POP) that use TCP/IP in
the capacity of transport protocol. To send electronic mails
we use POP SMTP, and we use POP3 for receiving thereof.
Existing IMAP protocol cannot manage both tasks, but most
often we use it instead of POP3.
16
3. Working with Electronic Mails
17
Lesson 6
open mail.olgs.gov 25
Trying...Connect to mail.olgs.gov
220 mail.olgs.gov - Server ESMTP (PMDF V4.3-10 #2381)
hello mysite.com
250 mail.olgs.gov OK, mysite.com
mail from:<[email protected]>
250 Address Ok.
data
354 Enter mail, end with a single ".".
SUBJECT: E-mail chapter
Ljonja, thanks for the live
.
250 OK
quit
221 Bye received. Goobye
Command
No. Description
(and its 4-letter variant)
1 HELLO (HELO) SMTP-client identification
2 MAIL (MAIL) Initiates mail transaction (mail delivery)
to mailboxes
3 RECEPIENT (RCPT) Recipient identification. Several
commands per session are allowed
4 DATA (DATA) Beginning of mail data
5 SEND (SEND) Initiates mail transaction to the terminals
(outdated)
18
3. Working with Electronic Mails
Command
No. Description
(and its 4-letter variant)
6 SEND или MAIL (SOML) If a recipient is active mails are sent to
the terminal, otherwise to a mailbox
7 SEND и MAIL (SAML) Receives mails on the terminal and mailbox
8 RESET (RSET) Breaks up a current mail transaction
9 WERIFY (WRFY) Requites a recipient to verify that its
argument is a true name.
10 EXPAND (EXPN) Command to a SMTP-recipient to verify
if an argument is an address of mail-out
and if yes it returns a message address
11 HELP (HELP) Command to SMTP-recipient to return
inquiry-message about its commands
12 NOOP (NOOP) Requires from a sender not to perform
any actions, but to give a response ОК. It
is mainly used for testing
13 QUIT (QUIT) Requires to give a response ОК and
close the current connection
14 TURN (TURN) Command to SMTP-recipient either to
give ОК and change the roles, i.e. to
become a STMP- recipient, or send a re-
ject-message and remain in the role of
an SMTP-recipient
19
Lesson 6
Class Description
1 MailMessage Electronic mail message
2 SmtpClient Realizes sending MailMessage through SMTP
3 MailAttacment Attachment within a mail message
Перечисление Description
1 MailEncoding Encoding type Base46 or UUEncode
2 MailFormat Message format Text or HTML
3 MailPriopity High, Medium, Low – message priority
20
3. Working with Electronic Mails
21
Lesson 6
22
3. Working with Electronic Mails
Method Purpose
Send Mail sending
SendAsync Non-blocking mail sending
SendAsyncCancel Terminating an mail sending non-blocking
operation
Property Description
EnableSsl Encoding use
Host String — server address
Port Integer — port number
TimeOut Command Send terminating timeout
Событие Description
SendCompleted Terminating an asynchronous mail sending
operation
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace Mailer
{
class Program
{
string prompt(string query)
{
Console.WriteLine(query);
return Console.ReadLine();
}
static void Main(string[] args)
{
23
Lesson 6
void Dialog()
{
to = prompt("Input a recipient address:");
from = prompt("Input a sender’s address:");
subject = prompt("Input a subject");
body = prompt("Input message text:");
server = prompt("Input a server address:");
}
client.UseDefaultCredentials = true;
try
{
client.Send(message);
Console.WriteLine("Message sent");
}
24
3. Working with Electronic Mails
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
namespace MailSender
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
25
Lesson 6
26
3. Working with Electronic Mails
27
Lesson 6
C: USER MonstrVB
S: +OK MonstrVB is a real hoopy frood
C: PASS mymail
S: +OK MonstrVB’s maildrop has 2 messages (320
octets)
C: QUIT
S: +OK dewey POP3 server signing off
Protocol Commands
Protocol commands
APOP [name] [digest]
A command implying sending user’s name and encoded
password (digest) to a server.
USER [name]
Sends user’s name to a server.
PASS [password]
Sends a mailbox password to a server
DELE [N messages]
Server flags an indicated message for deletion. Flagged
for deletion messages are removed only after the transaction
28
3. Working with Electronic Mails
29
Lesson 6
4. Use of FTP
Overview
FTP means a user interface that realizes ARPANET standard
file transfer protocol. This program allows user transferring
files between two computers interconnected by LAN or WAN.
Wherein, computer platforms can be of different types. This
is the main feature of FTP within the network.
If your system has an FTP and connected to the Internet,
you will get an access to a great number of archives stored
within the other systems.
FTP Terminology
While working with FTP we use a client-server model.
A client sends queries and receives responses along two ports of
TCP-connection (20 — data transfer port and 21 — command
transfer port). Remote directory is a file catalogue stored at
the server. A deleted file is a file on a server. Local file is a file
within a client file system.
Some FTP-commands:
dir [deleted_directory] [local_file]
ls [deleted_directory] [local_file]
Displays a list of files within a directory either for a stan-
dard output, or into this file if there is indicated a name of
a local file.
get [deleted_file] [local_file]
Requests sending the deleted file copies to your computer.
30
4. Use of FTP
31
Lesson 6
32
4. Use of FTP
33
Lesson 6
System.Object
System.MarshalByRefObject
System.NetWebResponse
System.Net.FtpWebResponse
34
4. Use of FTP
string FileName="ftp://glamurconcurs.com/lucie.jpg";
//create an FTP- query entity
FtpWebRequest request = (FtpWebRequest)WebRequest.
Create(FileName);
//set up an FTP-command
request.Method = WebRequestMethods.Ftp.DeleteFile;
//execute a query and get a result
FtpWebResponse response = (FtpWebResponse)
request.GetResponse();
//process a result
Console.WriteLine("Delete status: {0}",response.
StatusDescription);
//close a session
response.Close();
35
Lesson 6
5. Exam Tasks
Variants of Examination Home Assignment (select one):
1. Build web-site tree. Restrict the references to other sites
by 1 level. Consider the possibility of self-references.
2. Load a web-site. Downloads by the references to the other
site are not allowed. Consider the possibility of self-ref-
erences.
3. Write a chat (message exchange software between several
users) on the basis of the sockets with an allocated server
4. Write a chat (message exchange software between several
users) on the basis of the datagrams
5. Write a program of network checkers game with a possi-
bility of fan presence.
6. Loading program from FTP-server with a possibility of
resume of an interrupted load in a particular time interval
of changes of loaded files on a server (updating)
7. Write a remote-access server of several clients for the base
MS ACCESS and the simplest client with a SQL-query
editor.
36
5. Exam Tasks
37
STEP IT Academy, www.itstep.org
All rights to protected pictures, audio, and video belong to their authors or
legal owners.
Fragments of works are used exclusively in illustration purposes to the extent
justified by the purpose as part of an educational process and for educational
purposes in accordance with Articles 21 and 23 of the Law of Ukraine “On
Copyright and Related Rights”. The extent and method of cited works are in
conformity with the standards, do not conflict with a normal exploitation of the
work, and do not prejudice the legitimate interests of the authors and
rightholders. Cited fragments of works can be replaced with alternative, non-
protected analogs, and as such correspond the criteria of fair use.
All rights reserved. Any reproduction, in whole or in part, is prohibited.
Agreement of the use of works and their fragments is carried out with the
authors and other right owners. Materials from this document can be used
only with resource link.
Liability for unauthorized copying and commercial use of materials is defined
according to the current legislation of Ukraine.