(Score:★★★, Reply: 11, read: 2078) During work, a proxy server with complex time control conditions is required, so it can only be self-edited. Find some source code of the proxy server developed with serversocket and clientsocket from the network, which is complicated. Therefore, we want to use the idmappedporttc development proxy server in the Indy control group. The example with Delphi demonstrates a static proxy, such as www.borland.com In the example, while what is needed in actual applications is a dynamic proxy. This article describes how to use the Indy idmappedporttcp control to implement a dynamic HTTP proxy server. Analyze the source code of indy9 and find that the netdata attribute stores the user's request content and server response information, the onexecute event is executed after the user request is received and before the request is forwarded to the web server. Therefore, you can write some code to change the proxy host based on your request in the onexecute event, so that you can implement dynamic proxy. The Code is as follows: Procedure tform1.idmappedporttcp1execute (athread: tidmappedportthread ); VaR Requesthost: string; Requestport: integer; Begin // Change the connection Idlock. acquire; Try Requesthost: = gethost (athread. netdata ); Requestport: = getport (athread. netdata ); If (requesthost <> idmappedporttcp1.mappedhost) or (Requestport <> idmappedporttcp1.mappedport) then Begin Idmappedporttcp1.mappedhost: = requesthost; Idmappedporttcp1.mappedport: = requestport; Tidtcpclient (athread. outboundclient). HOST: = requesthost; Tidtcpclient (athread. outboundclient). Port: = requestport; Tidtcpclient (athread. outboundclient). Disconnect; Tidtcpclient (athread. outboundclient). Connect (athread. connecttimeout ); End; // changeconnect Finally Idlock. release; End; End; In actual application, an error occurs when accessing www.163.com and www.sina.com.cn. After analysis, it is found that the browser request format needs to be adjusted to delete the host name in the GET request. Add a line in the event to change the Request Code, as shown below: // Change request Athread. netdata: = delhostofurl (athread. netdata, requesthost, requestport ); The above method is very simple to implement the HTTP Proxy Server. Do not believe it. An important experience of this work is that it is more effective to analyze the source code than to read the manual. Long live open source code! Appendix: Code of the three user-defined functions required by this program. 1. Get the Host Name Function tform1.gethost (URL: string): string; VaR Luri: tiduri; Begin Luri: = tiduri. Create (URL ); Result: = Luri. Host; Luri. Free; End; // gethost 2. Get the port number Function tform1.getport (URL: string): integer; VaR Luri: tiduri; Begin Luri: = tiduri. Create (URL ); If length (Luri. Port) <> 0 then Result: = strtoint (Luri. Port) Else Result: = 80; Luri. Free; End; // getport 3. Delete the host string in the URL Function tform1.delhostofurl (URL, host: string; port: integer): string; VaR S: string; Begin Result: = URL; S: = 'HTTP: // '+ host; If Port <> 80 then S: = S + ':' + inttostr (port ); Delete (result, pos (S, result), length (s )); End; // delhostofurl 2003-5-19 21:06:00 |