Scalabium Software |
|
Knowledge for your independence'. | |
Home Delphi and C++Builder tips |
#161: How can I read the proxy information? |
|
most tasks that works with internet, requires to read the current proxy settings from computer before connect to remote server. Today I want to show two way to read this information. 1. to use registry var Handle: HKey; Buffer: array[0..256] of Char; BufSize: Integer; DataType: Integer; begin if RegOpenKeyEx(HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings', 0, KEY_READ, Handle) = ERROR_SUCCESS then begin BufSize := SizeOf(Buffer); DataType := reg_sz; if RegQueryValueEx(Handle, 'ProxyServer', nil, @DataType, @Buffer, @BufSize) = ERROR_SUCCESS then ProxyServer := Buffer; BufSize := SizeOf(Integer); if RegQueryValueEx(Handle, 'ProxyEnable', nil, @DataType, @i, @BufSize) = ERROR_SUCCESS then UseProxyServer := (i <> 0); RegCloseKey(Handle); end; end; 2. to use WinInet library var ProxyInfo: PInternetProxyInfo; Len: LongWord; begin Len := 4096; GetMem(ProxyInfo, Len); try if InternetQueryOption(nil, INTERNET_OPTION_PROXY, ProxyInfo, Len) then if ProxyInfo^.dwAccessType = INTERNET_OPEN_TYPE_PROXY then begin UseProxyServer := True; ProxyServer := ProxyInfo^.lpszProxy end finally FreeMem(ProxyInfo); end; end; Now if proxy used, we'll receive in ProxyServer variable similar string: "ftp=proxy.domain.com:8082;gopher=proxy.domain.com:8083;http=proxy.domai n.com:8080;https=proxy.domain.com:8081" where domain.com is our proxy.
So as you see, now we must parse this string for correct protocol (http/ftp etc) and to get the proxy server and port number. For example: if UseProxyServer and (ProxyServer <> '') then begin i := Pos('http=', ProxyServer); if (i > 0) then begin Delete(ProxyServer, 1, i+5); j := Pos(';', ProxyServer); if (j > 0) then ProxyServer := Copy(ProxyServer, 1, j-1); end; i := Pos(':', ProxyServer); if (i > 0) then begin ProxyPort := StrToIntDef(Copy(ProxyServer, i+1, Length(ProxyServer)-i), 0); ProxyServer := Copy(ProxyServer, 1, i-1) end end;
|
|
Copyright© 1998-2024, Scalabium
Software. All rights reserved. |