Use C # To implement Web Proxy Server 2

Source: Internet
Author: User
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

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.