如何动态设置浏览器的代理服务器地址/端口(编程实现)?(100分)

  • 主题发起人 主题发起人 zbwsh
  • 开始时间 开始时间
Z

zbwsh

Unregistered / Unconfirmed
GUEST, unregistred user!
如何动态设置浏览器的代理服务器地址/端口(编程实现)?
 
虽然并没有试过,但我想一般情况下这写信息肯定会写在注册表里,找一下,记住位置,
下次直接读取就行了。因为我这里是通过网关出去的,所以没法试了
 
请各位帮帮忙
 
在注册表里,
[HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Internet Settings]

"ProxyEnable"=hex:01,00,00,00 是否使用代理服务器
"ProxyServer"="192.168.1.104:80" 代理服务器的IP地址及端口

然后
var
buf:pchar;
...
getmem(buf,SizeOfBuf);//
strpcopy(buf,'注册表的主键,例如Software/Microsoft/windows/currentversion/Explorer/ToolBar');
SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,buf);
//通知系统刷新响应的键值。如果不行就重启吧
 
是否有更好的方法,希望不是修改注册表!
 
兄弟啊
实现这种功能能想到的办法应该就只有注册表吧,除了这个还能想到什么方法?
 
不过听说又是不能立即生效,请问是是不是?
 
请各位帮帮忙
 
你可以创建本机器的一个代理软件,地址是127.0.0.1 ,端口任意,而你的代理软件用的
代理服务器地址为动态设置就行了,

参考软件MultProxy
 
MultProxy哪里有??
WIN2000下如何修改注册表?
 
不用重起ie和机器,用程序设置立即生效:
user urlmon,wininet;

procedure qiehuanproxy(const proxyname:string);
var info:TInternetProxyInfo;
lngth:integer;
h:integer;
proxy :pChar;
bypass :PChar;
begin
proxy:=pchar(proxyname);//proxy address:port
bypass:=pChar('');
info.dwAccessType := INTERNET_OPEN_TYPE_PROXY ;
info.lpszProxy :=proxy;
info.lpszProxyBypass:=bypass;
lngth:=sizeof(info);
h:=UrlMkSetSessionOption(INTERNET_OPTION_PROXY ,@info,lngth,0);
end;


 
http://www.multiproxy.org/
 
to feijun :
为何无效??
 
端口怎么加步进去
 
proxy:=pchar('127.0.0.1:8080');
 
to feijun :
还是无效??

 
我只想知道用自己的程序改后不用重启浏览器,代理立即生效的方法。
 
我在国外网站也查过相关问题,结果是:无法立即生效。

具体的修改方法如下:
首先将下面的文件保存为WininetAppend.pas,是我改写了
C++Builder关于IE5的一些东西,Delphi5中没有这些。

unit WininetAppend;

interface

uses
Windows;

type
PInternetPerConnOption = ^INTERNET_PER_CONN_OPTION;
INTERNET_PER_CONN_OPTION = record
dwOption: DWORD;
case Integer of
0: (dwValue: DWORD);
1: (pszValue:LPTSTR);
2: (ftValue: FILETIME);
end;

PInternetPerConnOptionList = ^INTERNET_PER_CONN_OPTION_LIST;
INTERNET_PER_CONN_OPTION_LIST = record
dwSize :DWORD;
pszConnection :LPTSTR;
dwOptionCount :DWORD;
dwOptionError :DWORD;
pOptions :PInternetPerConnOption;
end;

//
// Options used in INTERNET_PER_CONN_OPTON struct
//
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;

//
// PER_CONN_FLAGS
//
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

//
// PER_CONN_AUTODISCOVERY_FLAGS
//
AUTO_PROXY_FLAG_USER_SET = $00000001; // user changed this setting
AUTO_PROXY_FLAG_ALWAYS_DETECT = $00000002; // force detection even when its not needed
AUTO_PROXY_FLAG_DETECTION_RUN = $00000004; // detection has been run
AUTO_PROXY_FLAG_MIGRATED = $00000008; // migration has just been done
AUTO_PROXY_FLAG_DONT_CACHE_PROXY_RESULT = $00000010; // don't cache result of host=proxy name
AUTO_PROXY_FLAG_CACHE_INIT_RUN = $00000020; // don't initalize and run unless URL expired
AUTO_PROXY_FLAG_DETECTION_SUSPECT = $00000040; // if we're on a LAN & Modem, with only one IP, bad?!?

INTERNET_OPTION_PER_CONNECTION_OPTION = 75;

implementation

end.

主程序中:
uses
Wininet, WininetAppend;


function TfrmMain.GetProxyInfo(var AutoDetect,AutoProxyUrl,
DirectConnect,ProxyConnect:boolean;var AutoProxyUrlAddress,
ByPassAddress,ProxyServer:String):boolean;
var
nSize : DWORD;
List : INTERNET_PER_CONN_OPTION_LIST;
Option: array[0..3] of INTERNET_PER_CONN_OPTION;
begin
nSize := sizeof(INTERNET_PER_CONN_OPTION_LIST);

Option[0].dwOption := INTERNET_PER_CONN_AUTOCONFIG_URL;
Option[1].dwOption := INTERNET_PER_CONN_FLAGS;
Option[2].dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
Option[3].dwOption := INTERNET_PER_CONN_PROXY_SERVER;

List.dwSize := sizeof(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection := nil;
List.dwOptionCount := 4;
List.dwOptionError := 0;
List.pOptions := @Option;

result := InternetQueryOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @List, nSize);
if not result then exit;

if(Option[0].pszValue <> nil) then
begin
AutoProxyUrlAddress := StrPas(Option[0].pszValue);
GlobalFreePtr(Option[0].pszValue);
end
else
AutoProxyUrlAddress := '';

DirectConnect := (Option[1].dwValue and PROXY_TYPE_DIRECT ) = PROXY_TYPE_DIRECT;
ProxyConnect := (Option[1].dwValue and PROXY_TYPE_PROXY ) = PROXY_TYPE_PROXY;
AutoProxyUrl := (Option[1].dwValue and PROXY_TYPE_AUTO_PROXY_URL) = PROXY_TYPE_AUTO_PROXY_URL;
AutoDetect := (Option[1].dwValue and PROXY_TYPE_AUTO_DETECT) = PROXY_TYPE_AUTO_DETECT;

if(Option[2].pszValue <> nil) then
begin
ByPassAddress := StrPas(Option[2].pszValue);
GlobalFreePtr(Option[2].pszValue);
end
else
ByPassAddress := '';

if(Option[3].pszValue <> nil) then
begin
ProxyServer := StrPas(Option[3].pszValue);
GlobalFreePtr(Option[3].pszValue);
end
else
ProxyServer := '';
end;

function TfrmMain.SetProxyInfo(const ProxyConnect:boolean;const ByPassAddress,ProxyServer:String):boolean;
var
nSize : DWORD;
List : INTERNET_PER_CONN_OPTION_LIST;
Option: array[0..2] of INTERNET_PER_CONN_OPTION;
begin
nSize := sizeof(INTERNET_PER_CONN_OPTION_LIST);

Option[0].dwOption := INTERNET_PER_CONN_FLAGS;
if ProxyConnect then
Option[0].dwValue := PROXY_TYPE_PROXY
else
Option[0].dwValue := PROXY_TYPE_DIRECT;

Option[1].dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
Option[1].pszValue := PChar(ByPassAddress);

Option[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
Option[2].pszValue := PChar(ProxyServer);

List.dwSize := sizeof(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection := nil;
List.dwOptionCount := 3;
List.dwOptionError := 0;
List.pOptions := @Option;

result := InternetSetOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @List, nSize);
end;

//ProxyServer格式: 127.0.0.1:8080
 
后退
顶部