C # network programming-implement Web proxy Functions

Source: Internet
Author: User

Proxy service is a widely used network application. There are many types of proxies, which can be divided into HTTP proxy service programs and FTP Proxy service programs according to different protocols, the server running the proxy service program is also called the HTTP Proxy server and the FTP proxy server. This article describes the Web Proxy service program proxy is the HTTP protocol.
I. Advantages of Network Proxy
The proxy service acts as a bridge and acts as a transit station for network information. In the network, the application proxy service is generally based on the following reasons:
(1) Make full use of IP Address resources. In a LAN, the external IP addresses are usually very limited. To ensure that all hosts in the LAN can access Internet resources, you can use network proxy.
(2) ensures network security. A network proxy can act as a firewall between the Intranet and the Internet. By filtering IP addresses, it limits the access of some IP addresses to external resources.
(3) It can effectively hide its own IP address and host name. Because all requests to the Internet are implemented through the proxy server, the target host can only know the IP address of the proxy server.
(4) improve the network speed. Usually, the proxy server has a large hard disk buffer, which stores data in the world. When you access the same data again, you can directly retrieve information from the buffer to speed up access.
Ii. Network Proxy types and implementation principles
The network proxy service can be divided into Application Layer proxy, transport layer proxy, and SOCKS proxy according to the working level. The Application Layer proxy works on the application layer of the TCP/IP Reference Model. It supports proxy for the application layer protocol (such as HTTP and FTP. It provides the most control, but is not flexible, and must have corresponding protocol support. If the Protocol does not support proxies (such as SMTP and POP), you can only use the following proxies at the application layer, that is, transport layer proxies. The Transport Layer proxy interacts directly with the TCP layer, making it more flexible. The proxy server is required to have some real server functions: Listening to specific TCP or UDP ports, receiving client requests and sending corresponding responses to the client at the same time. Another proxy needs to change the client's IP stack, that is, the SOCKS proxy. It is the most powerful and flexible agent standard protocol available. SOCK V4 allows clients inside the proxy server to completely connect to external servers. SOCK V5 adds authorization and authentication to clients, so it is a highly secure proxy. The proxy described later in this section is a kind of proxy at the application layer. The proxy protocol is HTTP, that is, the Web proxy that is frequently seen.
As mentioned above, the network proxy is a bridge connecting the client (the computer that requires proxy) and the server (the server that provides resource access. To implement this bridge function, the network proxy must meet the following conditions and is actually the process of running the proxy service:
(1) receive and parse client requests.
(2) create a new connection to the server and forward the request information of the client.
(3) receive the server feedback.
(4) interpret the server response and send the response back to the client.
Figure 1 is a typical network proxy service model:

  
Although the network proxy has many advantages, after using the proxy, all your requests to the network are implemented through the proxy server man-in-the-middle, therefore, malicious people may listen to your input. Similarly, if the selected proxy server has a small bandwidth, using the proxy will also reduce the network speed.
All in all, there are advantages and disadvantages of using proxy. Users should decide based on their own situations. However, it is very important to select a good proxy server.
III. C # implement Web proxy service programs
After the above introduction, I think you should have a basic understanding of the proxy service. Let's use an instance to learn more about how to use C # To implement the Web Proxy service. The functional order of the Web Proxy service is as follows:
(1) listen on the port and wait for the Web request information sent by the client browser.
(2) After receiving the Web request information from the client, parse the address of the target Web server, create a Socket instance, and connect the instance to the Web server.
(3) Transmit the Web request data packet of the client to port 80 of the Web server through the created Socket.
(4) receive page data returned by the Web server.
(5) Send the received data to the client to implement Web proxy.
The client may send a lot of Web request information (such as images and Flash files on webpages) to browse a Web address. to process the information more quickly and accurately, web Proxy service programs usually use multiple threads to process each Web request. Careful readers may find that the proxy server software uses two sockets to process the Web request information of each client. One is used to receive/transmit the information of the client, one is to communicate with the Web server. To distinguish these two sockets, we call them "service Socket" to talk to the server and "customer Socket" to talk to the client machine ".
The following describes how to compile the Web Proxy service program. This instance contains three parts:
1. Create a Web proxy class.
2. instantiation of the Web Proxy service class.
3. How to Implement the Web Proxy service through this Web proxy instance.
(1) create a Web proxy class
The procedure is as follows:
1. start Visual Studio.. Net, select the "file", "new", and "project" menus, and set "project type" to "Visual C # Project" in the "New Project" dialog box ", set "template" to "Windows application", enter "WebProxy" in the "name" text box, and enter "E: VS." in the "location" text box.. NET project, and then click OK to create the project.
2. Select "project" and "add class" in sequence. The "Add new project" dialog box is displayed.
3. Set "template" to "class ".
4. Enter "Proxy" in the "name" text box and click "open", as shown in Figure 2:
  
5. In the Solution Explorer window, double-click the Proxy. cs file to enter the edit field of the Proxy. cs file.
6. Add the following code at the beginning of the Proxy. cs source file:
Using System;
Using System. Net;
Using System. Net. Sockets;
Using System. Text;
Using System. IO;
7. Use the following constructor to replace the default 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. this parameter is a Socket object, which is mainly used to exchange data with the client. It is a "customer Socket ":
Public Proxy (Socket socket)
{
//
// TODO: add the constructor logic here
//
This. clientSocket = socket;
}

 

8. Add the following code to the definition Proxy code area. The following code defines some variables used in the Proxy class. These variables are mainly used in the subsequent definition of the Run method.
Socket clientSocket;
Byte [] read = new byte [1024];
// Define a space to store the request data packets from the client
Byte [] Buffer = null;
Encoding ASCII = Encoding. ASCII;
// Set the Encoding
Byte [] RecvBytes = new Byte [1, 4096];
// Define a space to store the 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, send them to the Web server, then receive feedback data from the Web server, and send the data to the client. In order to implement data transmission in these two different aspects, the Run method is implemented through two Socket instances. When writing the Run method, pay attention to the following two points:
(1) Because HTTP is built on the TCP protocol, the created Socket instance should use the TCP protocol. The following code creates a Socket instance that can send 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 obtained when the Proxy service listens to the port number and receives connection requests. Therefore, we should use this Socket as the parameter and use the constructor in the Proxy class to create a Proxy instance. This Socket receives HTTP request information from the client and transmits data to the client.
Creating and using Socket is the key to implementing Web Proxy software. Enter the following code after the constructor code:
Public void Run ()
{
String clientmessage = "";
// Store the HTTP request string from the client
String URL = "";
// Store the parsed 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]);
// Parse the server address to be accessed
IPEndPoint ipEndpoint = new IPEndPoint (address [0], 80 );
Socket IPsocket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
// Create a Socket object connecting to the Web server
IPsocket. Connect (ipEndpoint );
// Socket connects to the Web server
If (IPsocket. Connected)
Console. WriteLine ("Socket is connected correctly! ");
String GET = clientmessage;
Byte [] ByteGet = ASCII. GetBytes (GET );
IPsocket. Send (ByteGet, ByteGet. Length, 0 );
// The proxy access software sends HTTP request commands to the server
Int32 rBytes = IPsocket. Receive (RecvBytes, RecvBytes. Length, 0 );
// The proxy access software receives feedback from the Web server.
Console. WriteLine ("number of received Bytes:" + rBytes. ToString ());
String strRetPage = null;
StrRetPage = strRetPage + ASCII. GetString (RecvBytes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.