function GetProxyInformation: string; var ProxyInfo: PInternetProxyInfo; Len: LongWord; begin Result := ''; Len := 4096; GetMem(ProxyInfo, Len); try if InternetQueryOption(nil, INTERNET_OPTION_PROXY, ProxyInfo, Len) then if ProxyInfo^.dwAccessType = INTERNET_OPEN_TYPE_PROXY then begin Result := ProxyInfo^.lpszProxy end; finally FreeMem(ProxyInfo); end; end;
procedure GetProxyServer(protocol: string; var ProxyServer: string;var ProxyPort: Integer); var i: Integer; proxyinfo,ps:string; begin ProxyServer := ''; ProxyPort := 0; proxyinfo := GetProxyInformation; if proxyinfo = '' then Exit; protocol := protocol + '='; i := Pos(protocol, proxyinfo); if i > 0 then begin Delete(proxyinfo, 1, i + Length(protocol)); i := Pos(';', ProxyServer); if i > 0 then proxyinfo := Copy(proxyinfo, 1, i - 1); end; i := Pos(':', proxyinfo); if i > 0 then begin ProxyPort:=StrToIntDef(Copy(proxyinfo, i + 1, Length(proxyinfo) - i), 0); ProxyServer:=Copy(proxyinfo, 1, i - 1) end; end;
procedure TForm1.Button1Click(Sender: TObject); var ProxyServer: string; ProxyPort: Integer; begin GetProxyServer('http', ProxyServer, ProxyPort); if ProxyPort=0 then begin ShowMessage('你的IE没有使用的代理服务器'); Edit1.Text:=''; Edit2.Text:=''; end else begin Edit1.Text:= ProxyServer; //代理服务器地址 Edit2.Text :=IntToStr(ProxyPort); //代理端口 end; end;
设置IE 代理
user Registry
procedure SetIEProxyServer(ProxyIP:string;ProxyPort:string); var zc:TRegistry; begin zc:=TRegistry.Create; zc.RootKey:=HKEY_CURRENT_USER; zc.OpenKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings',True); zc.WriteInteger('ProxyEnable',1); if (ProxyIP='') or (ProxyPort='') then begin Application.MessageBox('请输入IP地址和端口','Error',MB_ICONHAND); Exit; end; zc.WriteString('ProxyServer',ProxyIP+':'+ProxyPort); if zc.OpenKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings',True) then begin Form1.Label3.Font.Color:=clRed; Form1.Caption:=ProxyIP+'代理成功'; end else begin Form1.Label3.Caption:=ProxyIP+'代理失败'; end; zc.Free; end;
procedure DeleteIEProxyServer(); var zc:TRegistry; begin zc:=TRegistry.Create; zc.RootKey:=HKEY_CURRENT_USER; zc.OpenKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings',True); if zc.DeleteValue('ProxyEnable')=True then begin ShowMessage('取消IE代理成功'); end else ShowMessage('当前IE没有代理'); zc.Free; end;
procedure TForm1.Button3Click(Sender: TObject);
begin DeleteIEProxyServer(); end;
procedure TForm1.Button1Click(Sender: TObject); begin SetIEProxyServer(Edit1.text,Edit2.Text); end;