//如何知道使用了代理服务器的正确IP地址
uses WinSock;
procedure TForm1.Button1Click(Sender: TObject);
var
wVersion: Word;
WSData: TWSAData;
ASocket: TSocket;
ASockAddr: TSockAddr;
aRecvBuf: array[0..332] of Char;
sCmd: string;
I: Integer;
begin
wVersion := MakeWord(2, 0);
case WSAStartup(wVersion, WSData) of
WSASYSNOTREADY:
end;
ASocket := Socket(AF_INET, SOCK_STREAM, 0);
ASockAddr.sin_family := AF_INET;
ASockAddr.sin_port := hToNs(80);
StrPCopy(aRecvBuf, Edit1.Text); // Edit1.Text = Proxy Address
ASockAddr.sin_addr.S_addr := INET_ADDR(aRecvBuf);
if Connect(ASocket, ASockAddr, SizeOf(ASockAddr)) <> 0 then
begin
CloseSocket(ASocket);
ShowMessage('Error');
Exit;
end;
sCmd := 'GET http://www.gislab.ecnu.edu.cn/delphibbs/main.htm';
StrPCopy(aRecvBuf, sCmd);
I := Length(sCmd);
aRecvBuf := #13; Inc(I);
aRecvBuf := #10; Inc(I);
aRecvBuf := #13; Inc(I);
aRecvBuf := #10; Inc(I);
aRecvBuf := #0;
Send(ASocket, aRecvBuf, I, 0);
Recv(ASocket, aRecvBuf, SizeOf(aRecvBuf) - 1, 0);
I := 0;
while I < 10 do
begin
I := I + 1;
if ((aRecvBuf = '') and (aRecvBuf[I + 1] = '2')
and (aRecvBuf[I + 2] = '0') and (aRecvBuf[I + 3] = '0')
and (aRecvBuf[I + 4] = '')) then
begin
I := 200;
Break;
end;
end;
if I <> 200 then
begin
CloseSocket(ASocket);
ShowMessage('Proxy Error'); // may be Access Denied
Exit;
end;
ShowMessage('OK'); // proper proxy address
end;