Use C # To implement Web Proxy Server 3

Source: Internet
Author: User
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, 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 proxy service software sends the received information to the client.

}

Catch (exception exc2)

{

Console. writeline (exc2.tostring ());

}

}

  

// Receives the 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;

}

  

// Send the 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 ("Number of transmitted Bytes:" + length. ToString ());

S. Send (Buffer, length, 0 );

}

So far, the Proxy class definition process has been completed.

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.