CJC Delphi 技巧馆's Archiver

cjc Posted at 2013-7-1 20:46

让TWebBrowser使用proxy代理,但不修改internet 设置的方法 (2013.7.1)

让TWebBrowser使用proxy代理,但不修改internet 设置的方法 (2013.7.1)

Let TWebBrowser using owner proxy.

Enable:
   SetProxy('127.0.0.1:8080');

Disable:
   SetProxy('');

procedure SetProxy(UserAgent, ProxyServer: string);
type
  INTERNET_PER_CONN_OPTION = record
    dwOption: DWORD;
    Value: record
      case Integer of
        1: (dwValue: DWORD);
        2: (pszValue: PAnsiChar);
        3: (ftValue: TFileTime);
    end;
  end;
  LPINTERNET_PER_CONN_OPTION = ^INTERNET_PER_CONN_OPTION;
  INTERNET_PER_CONN_OPTION_LIST = record
    dwSize: DWORD;
    pszConnection: LPTSTR;
    dwOptionCount: DWORD;
    dwOptionError: DWORD;
    pOptions: LPINTERNET_PER_CONN_OPTION;
  end;
  LPINTERNET_PER_CONN_OPTION_LIST = ^INTERNET_PER_CONN_OPTION_LIST;
var
  list: INTERNET_PER_CONN_OPTION_LIST;
  dwBufSize: DWORD;
  hInternet: Pointer;
  Options :array[1..3] of INTERNET_PER_CONN_OPTION;
const
  INTERNET_PER_CONN_FLAGS = 1;
  INTERNET_PER_CONN_PROXY_SERVER = 2;
  INTERNET_PER_CONN_PROXY_BYPASS = 3;
  INTERNET_PER_CONN_AUTOCONFIG_URL = 4;
  INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5;
  PROXY_TYPE_DIRECT = $00000001;   // direct to net
  PROXY_TYPE_PROXY = $00000002;   // via named proxy
  PROXY_TYPE_AUTO_PROXY_URL = $00000004;   // autoproxy URL
  PROXY_TYPE_AUTO_DETECT = $00000008;   // use autoproxy detection
  INTERNET_OPTION_REFRESH = 37;
  INTERNET_OPTION_PER_CONNECTION_OPTION = 75;
  INTERNET_OPTION_SETTINGS_CHANGED = 39;
begin
  dwBufSize := sizeof(list);

  // Fill out list struct.
  list.dwSize := sizeof(list);

  // NULL == LAN, otherwise connectoid name.
  list.pszConnection := Nil;

  // Set three options.
  list.dwOptionCount:= High(Options); // the highest index of the array (in this case 3)
  // list.pOptions:= Allocmem(sizeof(INTERNET_PER_CONN_OPTION) * 3);

  // Make sure the memory was allocated.
  //if list.pOptions = nil then
  //  Exit;

  // Set flags.
  Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
  Options[1].Value.dwValue := PROXY_TYPE_DIRECT or PROXY_TYPE_PROXY;

  // Set proxy name.
  //inc(list.pOptions);
  Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
  Options[2].Value.pszValue := PChar(ProxyServer);

  // Set proxy override.
  //inc(list.pOptions);
  Options[3].dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
  //list.pOptions^.Value.pszValue  := 'local';
  Options[3].Value.pszValue  := '<local>'; // '<local>' instead of 'local'

  // Set the options on the connection.
  //Dec(list.pOptions, 2);

  list.pOptions := @Options;

  hInternet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
  InternetSetOption(hInternet, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
  InternetSetOption(hInternet, INTERNET_OPTION_REFRESH, nil, 0);
  InternetCloseHandle(hInternet);

  // Free the allocated memory.
  //FreeMem(list.pOptions);
end;

参考资料
http://support.microsoft.com/default.aspx?scid=kb;en-us;226473

Page: [1]
CJC好玩背单词最新版本 - 是目前最好的背单词软件,游戏好玩,功能强大,词库众多,还有真人语音,免费下载,免费试用 !

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.