Use C # To implement Web Proxy Server 4

Source: Internet
Author: User
2) use the proxy class to implement Web Proxy

The following describes how to use the proxy class to implement the Web Proxy program. The proxy class is defined in the namespace WebProxy:

1. Open the class1.cs file in the Visual Studio. NET code editor and enter the code editing interface of class1.cs.

2. Import the following namespace 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. Add the following code to the main function. The following code uses the proxy class to implement the Web Proxy program:

Const int Port = 8000;

// Define the port number

Tcplistener = new tcplistener (port );

Console. writeline ("listening port:" + port. tostring ());

Tcplistener. Start ();

// Listening port number

While (true)

{

Socket socket = tcplistener. acceptsocket ();

// Obtain the scoket instance for transmitting and receiving data

Proxy proxy = new Proxy (socket );

// Proxy class instantiation

Thread thread = new Thread (new ThreadStart (proxy. Run ));

// Create a thread

Thread. Start ();

// Start the thread

}

Save all the steps above, so that a simple Web proxy is completed. The Web agent listens on the 8000 port number.

  (3) test web code programs

The Web Proxy program can be implemented through two computers. One computer runs the Web Proxy program to act as the Web Proxy Server, and the other computer acts as the client to browse the Web page through the Web Proxy Server. After determining that the Web Proxy software is running, you need to make necessary settings for the client:

1. Open IE browser.

2. select Tools and Internet Options, select the connection page in the pop-up Internet Options dialog box, and click the LAN Settings button, in the "LAN Settings" dialog box that appears, select "use proxy server (X) for LAN (these settings are not applied to dial-up and VPN connections, enter the IP address of the proxy server in the "Address" text box, for example, "10.138.198.213", and "8000" in the "Port" text box, as shown in 3:

Now the client settings are complete. After the computer whose IP address is "10.138.198.213" has run the Web Proxy program described above, open the IE browser of the client and enter the URL to browse, you can browse the Web page through the Web Proxy Server. Figure 4 shows the Web Proxy service running interface on the server:

Iv. Summary

So far, a simple web proxy service software is basically complete. Although the implementation principle of the proxy service is relatively simple, the specific implementation is still cumbersome. Network Proxy is a complex topic for implementing rich content. The agent service software introduced in this section can only be a small part, regardless of the protocol type or implementation function. We hope that you can use this article and other relevant knowledge to create more powerful functions, higher security, and more stable network proxy service programs.

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.