怎样通过编程在IE浏览器上添加代理服务器(以及一系列操作)?

richardxulf 2003-01-22 03:16:26
因为我们的网站需要走一个代理服务器,我想通过编程在浏览器上添加代理服务器 以及一系列本来应该手工的操作。请指教!谢谢
...全文
150 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
masterz 2003-02-05
  • 打赏
  • 举报
回复
Change Proxy of Internet Settings

--------------------------------------------------------------------------------
This article was contributed by Onega .
Introduction
Changing proxy settings of IE is a common requirement for me. Then I got the idea of writing a tool myself at last. I have not found clear instruction on this purpose. Many articles recommend to modify registry directly, but unfortunately their instruction is not enough. Most of them direct me to modify the following values in registry

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"ProxyEnable"=dword:00000001
"ProxyServer"=":"
"ProxyOverride"=""
"DisablePasswordCaching"=dword:00000001

I tested it and find that it does not work at least on my computer.( I access internet by ADSL connection.) So I backup registry and modify proxy settings via Internet Explorer, finding that
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections]

is also changed. so I write the following code snippet to change proxy settings:
void ShowError(long lerr)
{
LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
lerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
return;
}
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}
void CieproxyDlg::OnBnClickedOk()
{//set proxy server
UpdateData();
GetDlgItemText(IDC_EDIT1,m_sIEProxy);
HKEY hk;
LONG lret=RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
NULL,KEY_WRITE|KEY_SET_VALUE,&hk);
if(lret==ERROR_SUCCESS&&NULL!=hk)
{
TCHAR* pbuf=m_sIEProxy.GetBuffer(1);
lret=RegSetValueEx( hk,"ProxyServer",NULL,REG_SZ,pbuf,m_sIEProxy.GetLength());
DWORD dwenable=1;
lret=RegSetValueEx(hk,"ProxyEnable",NULL,REG_DWORD,(LPBYTE)&dwenable,sizeof(dwenable));
RegCloseKey(hk);
}
const TCHAR* keyname3=_T("software\\Microsoft\\windows\\currentversion\\Internet Settings\\Connections");
lret=RegOpenKeyEx(HKEY_CURRENT_USER,keyname3,NULL,KEY_READ|KEY_WRITE|KEY_SET_VALUE,&hk);
if(lret==ERROR_SUCCESS&&NULL!=hk)
{
DWORD dwtype;
char pbuf[256];
DWORD dwlen=sizeof(pbuf);
const char* valname="Connection to adsl3";
lret=RegQueryValueEx(hk,valname,NULL,&dwtype,pbuf,&dwlen);
if(lret!=ERROR_SUCCESS)
{
ShowError(lret);
}
pbuf[8] = 3;//enable proxy
pbuf[4]=pbuf[4]+1;
const char* p=m_sIEProxy.GetBuffer(1);
memcpy(pbuf+16,p,m_sIEProxy.GetLength());
char c=0;
for(int i=m_sIEProxy.GetLength();i<20;i++)
pbuf[16+i]=c;
m_sIEProxy.ReleaseBuffer();
lret=RegSetValueEx(hk,valname,NULL,REG_BINARY,pbuf,dwlen);
RegCloseKey(hk);
}
DWORD dwret;
SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,NULL,NULL,SMTO_NORMAL,1000,&dwret);
}

void CieproxyDlg::OnBnClickedDisableProxy()
{
UpdateData();
GetDlgItemText(IDC_EDIT1,m_sIEProxy);
HKEY hk;
LONG lret=RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
NULL,KEY_WRITE|KEY_SET_VALUE,&hk);
if(lret==ERROR_SUCCESS&&NULL!=hk)
{
DWORD dwenable=0;
lret=RegSetValueEx(hk,"ProxyEnable",NULL,REG_DWORD,(LPBYTE)&dwenable,sizeof(dwenable));
RegCloseKey(hk);
}
const TCHAR* keyname3=_T("software\\Microsoft\\windows\\currentversion\\Internet Settings\\Connections");
lret=RegOpenKeyEx(HKEY_CURRENT_USER,keyname3,NULL,KEY_READ|KEY_WRITE|KEY_SET_VALUE,&hk);
if(lret==ERROR_SUCCESS&&NULL!=hk)
{
DWORD dwtype;
char pbuf[256];
DWORD dwlen=sizeof(pbuf);
const char* valname="Connection to adsl3";
lret=RegQueryValueEx(hk,valname,NULL,&dwtype,pbuf,&dwlen);
if(lret!=ERROR_SUCCESS)
{
ShowError(lret);
}
pbuf[8] = 1;//enable proxy
pbuf[4]=pbuf[4]+1;
lret=RegSetValueEx(hk,valname,NULL,REG_BINARY,pbuf,dwlen);
RegCloseKey(hk);
}
DWORD dwret;
SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,NULL,NULL,SMTO_NORMAL,1000,&dwret);
}

Problem of above code is that existing Internet Explorer instances don't know the change of settings. What is more, changing registry directly is not an elegant method. Then the following must be more attractive :
needed head file and library
#include "Wininet.h"
#pragma comment(lib,"wininet")
...
BOOL SetConnectionOptions(LPCTSTR conn_name,LPCTSTR proxy_full_addr)
{
//conn_name: active connection name.
//proxy_full_addr : eg "210.78.22.87:8000"
INTERNET_PER_CONN_OPTION_LIST list;
BOOL bReturn;
DWORD dwBufSize = sizeof(list);
// Fill out list struct.
list.dwSize = sizeof(list);
// NULL == LAN, otherwise connectoid name.
list.pszConnection = conn_name;
// Set three options.
list.dwOptionCount = 3;
list.pOptions = new INTERNET_PER_CONN_OPTION[3];
// Make sure the memory was allocated.
if(NULL == list.pOptions)
{
// Return FALSE if the memory wasn't allocated.
OutputDebugString("failed to allocat memory in SetConnectionOptions()");
return FALSE;
}
// Set flags.
list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT |
PROXY_TYPE_PROXY;

// Set proxy name.
list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
list.pOptions[1].Value.pszValue = proxy_full_addr;//"http://proxy:80";

// Set proxy override.
list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
list.pOptions[2].Value.pszValue = "local";

// Set the options on the connection.
bReturn = InternetSetOption(NULL,
INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);

// Free the allocated memory.
delete [] list.pOptions;
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0);
return bReturn;
}
BOOL DisableConnectionProxy(LPCTSTR conn_name)
{
//conn_name: active connection name.
INTERNET_PER_CONN_OPTION_LIST list;
BOOL bReturn;
DWORD dwBufSize = sizeof(list);
// Fill out list struct.
list.dwSize = sizeof(list);
// NULL == LAN, otherwise connectoid name.
list.pszConnection = conn_name;
// Set three options.
list.dwOptionCount = 1;
list.pOptions = new INTERNET_PER_CONN_OPTION[list.dwOptionCount];
// Make sure the memory was allocated.
if(NULL == list.pOptions)
{
// Return FALSE if the memory wasn't allocated.
OutputDebugString("failed to allocat memory in DisableConnectionProxy()");
return FALSE;
}
// Set flags.
list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT ;
// Set the options on the connection.
bReturn = InternetSetOption(NULL,
INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
// Free the allocated memory.
delete [] list.pOptions;
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0);
return bReturn;
}
Their usage is very straightforward:
//set proxy
const char* connection_name="Connection to adsl3";
SetConnectionOptions(connection_name,"62.81.236.23:80");
//disable proxy
DisableConnectionProxy(connection_name);

microyzy 2003-01-28
  • 打赏
  • 举报
回复
gz
fuq0 2003-01-22
  • 打赏
  • 举报
回复
gz
punks 2003-01-22
  • 打赏
  • 举报
回复
…………回复失败了…………
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
这个节点下有IE代理服务器的设置,改注册表就可以了。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧