The Web| Server Agent service program is a widely used network application. There are many types of agents, according to the different protocol can be divided into HTTP Proxy service program, FTP Proxy service program, and so on, and the server running the Agent service program is called the HTTP proxy server and FTP proxy server. This article will introduce the Web Proxy service agent is the HTTP protocol.
First, the advantages of network Agent Program
Agent service is the role of a bridge, it is a transit point of network information. Application of Agent services in the network is generally based on the following reasons:
(1) Make full use of IP address resources. In the local area network, the general external IP address is very limited, in order to ensure that the host within the LAN can access Internet resources, through the network agent can be achieved.
(2) To ensure network security. A network proxy can act as a firewall between the intranet and the Internet by filtering IP addresses and restricting access to external resources by certain IP addresses.
(3) can effectively hide their own IP address and host name. Since all requests for external networks are implemented through proxy servers, the destination host can only know the IP address of the proxy server.
Second, the type of network agent and realization principle
The network Proxy service according to the work level, generally may divide into the application layer proxy, the Transport layer proxy and the SOCKS proxy. The application-tier proxy, which works on the application layer of the TCP/IP reference model, supports proxies for application-layer protocols such as HTTP and FTP. It provides the most control, but not flexible, must have the appropriate protocol support. If the protocol does not support proxies (such as SMTP and POP), it can only be agents at the application level, or transport layer. The transport layer agent interacts directly with the TCP layer and is more flexible. Requires the proxy server to have a partial true server function: Listen for a specific TCP or UDP port, and the receiving client's request responds to the client at the same time. Another agent needs to change the IP stack of the client, that is, the socks agent. It is the most powerful and flexible agent standard protocol available. Sock V4 allows a client within the proxy server to fully connect to an external server, sock V5 increases the authorization and authentication of the client, so it is a more secure proxy. The agent that is described later in this section is a proxy on the application tier that is represented by HTTP, which is often seen as a Web proxy.
As mentioned above, the network proxy is a bridge between the client (the computer that requires the proxy) and the server side (the server that provides access to the resource). To achieve this function of the bridge, the network agent must meet the following conditions, in fact, is also the operation of the proxy service process:
(1) Receive and parse client requests.
(2) Create a new connection to the server and forward the client's request information.
(3) Receive server feedback information.
(4) Explain the response of the server and pass the response back to the client.
Although the network agent has many advantages, but because of using the proxy, all of their requests to the network through the broker server this intermediary to achieve, so it is possible to encounter malicious people listening to your input content. Similarly, if you choose a proxy server with a relatively small bandwidth, using a proxy can also reduce network speed.
In a word, the use of agents is both pros and cons, users should be based on their own circumstances to decide. However, it is important to choose a good proxy server.
C # Implementation of Web Proxy service program
After the introduction of the above, I think we should have a basic understanding of agency services, let us through an example to deeply understand how to use C # to implement Web Proxy services. The functional order of the Web Proxy service is this:
(1) A listening port waiting for Web request information sent by the client browser.
(2) After receiving the client Web request information, resolves the address of the destination Web server and creates an instance of the socket and connects the Web server with this instance.
(3) Through the creation of the socket to transfer the client's Web request packets to the Web server's 80 port.
(4) Receive the page data returned by the Web server.
(5) Transmit the received data to the client, thereby implementing the Web Proxy.
A client browsing a Web address may have to send a lot of Web request information (such as images in a Web page, Flash, and so on), and for faster and more accurate processing of this information, Web Proxy services often use multithreading to process each Web request. Attentive readers may find that the proxy server software uses two sockets for each client's Web request, one for receiving/transmitting the client, and one for communicating with the Web server. In order to differentiate these two sockets, we call the "service socket" of the server dialog and the client machine dialog called "Customer Socket".
The following is the beginning of the authoring of Web Proxy service programs. This instance contains three parts:
1. Create a Web proxy class.
The instantiation of the class of the 2.WEB Proxy service.
3. How to implement a Web proxy service through an instance of this Web proxy class.
(i) Create a Web proxy class
The specific steps are as follows:
1. Start Visual Studio.NET, select File, new, Project menu, and in the New Project dialog box, set the project type to Visual C # project, set the template to Windows application, and in the name Enter "WebProxy" in the text box and enter "E:vs" in the Position text box. NET project, and then click the OK button, so the project is set up.
2. Select menu item, add Class, and the Add New Item dialog box pops up.
3. Set "Template" to "class".
4. Enter "Proxy" in the Name text box and click the Open button
5. In the Solution Explorer window, double-click the Proxy.cs file to enter the editing bounds of the Proxy.cs file
6. At the beginning of the Proxy.cs source file, add the following code, which is the namespace to be used in the import Proxy.cs:
Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
Using System.IO;
7. Replace the default constructor with the following constructor. The following code creates a constructor in the proxy class. The proxy class has only one constructor, and this constructor has only one parameter, which is the socket object, which is used primarily for data exchange with the client and is a "customer socket":
Public Proxy (socket socket)
{
//
TODO: Add constructor logic here
//
This.clientsocket = socket;
}
8. Add the following code to the code area where the proxy class is defined, and the following code is a variable that defines the use of the proxy class, which is primarily used in the later definition of the Run method.
Socket Clientsocket;
byte[] Read = new byte[1024];
Defines a space for storing request packets from the client
Byte [] Buffer = null;
Encoding ASCII = encoding.ascii;
Set code
byte[] recvbytes = new byte[4096];
Defines a space for storing data returned by the Web server
9. Create the Run method in the proxy class. The Run method is the only method in the proxy class. The function is to receive HTTP requests from the client and transfer them to the Web server, and then receive the feedback from the Web server and deliver the data to the client. In order to implement these two different aspects of data transfer, the Run method is implemented through two socket instances. When writing the Run method, be aware of the following two points:
(1) Because HTTP is built on top of the TCP protocol, the socket instance created should use the TCP protocol. The following code is to create a socket instance that can transfer HTTP request commands to the Web server and receive feedback from the Web server:
Socket ipsocket = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);
(2) Another socket is in the agent service program listening to the port number, receive the connection request, so you should use this socket as a parameter, using the constructor in the proxy class to create a proxy instance. This socket implementation receives HTTP request information from the client and transmits the data to the client.
Socket creation and use is the key to implementing Web proxy software. After the constructor code, enter the following code:
public void Run ()
{
String clientmessage = "";
Storing HTTP request strings from the client
String URL = "";
Store resolved address request information
int bytes = Readmessage (read, ref clientsocket, ref clientmessage);
if (bytes = 0)
{
return;
}
int index1 = Clientmessage. IndexOf (");
int index2 = Clientmessage. IndexOf (", index1 + 1);
if ((index1 = = 1) | | (Index2 = = 1))
{
throw new IOException ();
}
String part1 = Clientmessage. Substring (index1 + 1, index2-index1);
int index3 = Part1. IndexOf ('/', index1 + 8);
int index4 = Part1. IndexOf (", index1 + 8);
int index5 = INDEX4-INDEX3;
URL = Part1. Substring (index1 + 4, part1. LENGTH-INDEX5)-8);
Try
{
Iphostentry iphost = Dns.resolve (URL);
Console.WriteLine ("Remote Host Name:" + iphost.hostname);
string [] aliases = iphost.aliases;
ipaddress[] address = iphost.addresslist;
Console.WriteLine ("Web server IP Address: + address[0]");
Resolves the address of the server to be accessed
IPEndPoint IPEndPoint = new IPEndPoint (address[0], 80);
Socket ipsocket = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);
To create a socket object that connects to the Web server side
Ipsocket.connect (IPEndPoint);
Socket Connected Web server
if (ipsocket.connected)
Console.WriteLine (the Socket is connected properly!) ");
string get = Clientmessage;
byte[] Byteget = ASCII. GetBytes (get);
Ipsocket.send (Byteget, byteget.length, 0);
Proxy access software for server-side transfer of HTTP request commands
Int32 rbytes = ipsocket.receive (recvbytes, recvbytes.length, 0);
Proxy access software receives feedback from the Web server side
Console.WriteLine ("Bytes Received:" + rbytes.tostring ());
String strretpage = null;
Strretpage = Strretpage + ASCII. GetString (recvbytes, 0, rbytes);
while (Rbytes > 0)
{
Rbytes = Ipsocket.receive (recvbytes, recvbytes.length, 0);
Strretpage = Strretpage + ASCII. GetString (recvbytes, 0, rbytes);
}
Ipsocket.shutdown (Socketshutdown.both);
Ipsocket.close ();
SendMessage (Clientsocket, strretpage);
The Agent service software transmits the received information to the client
}
catch (Exception Exc2)
}
Receive HTTP request data from the client
private int Readmessage (byte [] ByteArray, ref Socket S, ref String Clientmessage)
{
int bytes = S.receive (ByteArray, 1024, 0);
String messagefromclient = Encoding.ASCII.GetString (ByteArray);
Clientmessage = (String) messagefromclient;
return bytes;
}
Transfer data from the Web server to the client
private void SendMessage (Socket s, string message)
{
Buffer = new Byte[message. Length + 1];
int length = ASCII. GetBytes (message, 0, message. Length, Buffer, 0);
Console.WriteLine ("Bytes transmitted:" + length.) ToString ());
S.send (Buffer, length, 0);
}
At this point, the definition process of the proxy class is complete.
(b) Using proxy classes to implement Web Proxy
The following is a concrete implementation step of implementing a Web Proxy using the proxy class, which is defined in the namespace WebProxy:
1. In Visual Studio. NET code Editor to open the Class1.cs file and enter the Class1.cs code editing interface.
2. Import the following namespaces at the beginning of the Class1.cs source file:
Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
Using System.IO;
Using System.Threading;
Using WebProxy;
3. To add the following code to the main function, the following code uses the proxy class to implement the Web proxy:
const int port = 8000;
Define port number
TcpListener TcpListener = new TcpListener (port);
Console.WriteLine ("Listener Port Number:" + port.) ToString ());
TcpListener. Start ();
Listening port number
while (true)
{
Socket socket = TcpListener. AcceptSocket ();
And get the Scoket instance of transmitting and receiving data
Proxy proxy = new proxy (socket);
Proxy class instantiation
Thread thread = new Thread (new ThreadStart (proxy). Run));
Creating Threads
Thread. Start ();
Start thread
}
Save all the steps above, so a simple Web proxy is done. This web Agent listens on a 8000 port number.
(iii) test the Web Code program
A Web proxy can be implemented through two computers, one of which runs a Web proxy as a Web proxy server, and another computer that acts as a client and browses through a Web proxy server. After you have determined that the Web proxy software is running, you need to make the necessary settings for the client:
1. Open IE Browser.
2. Select "Tools" in turn, "Internet Options, select the Connections page in the Pop-up Internet Options dialog box, click the LAN Settings button in the pop-up local area network (LAN) Settings dialog box, select Use Proxy server for LAN (X), ( These settings do not apply to dial-up and VPN connections), and in the Address text box, enter the IP address of the proxy server, such as "10.138.198.213", and enter "8000" in the Port text box.
The client's settings are now complete. After determining that the computer with the IP address "10.138.198.213" has already run the Web proxy program described above, open the client's IE browser and enter the URL to browse through the Web Proxy server.
Iv. Summary
At this point a simple Web Proxy service software, even if the basic completion. Although the principle of the implementation of proxy services is relatively simple, but the implementation of the specific is very cumbersome. Network proxy is a rich and complex topic, this section describes the agent service software, regardless of the implementation of the Protocol type, or implementation of the function, can only be a small part. I hope you can through the introduction of this article, combined with other relevant knowledge, to create a more powerful, more secure, using more stable network Agent service program.