现在的位置: 首页 > 综合 > 正文

IE代理问题

2013年07月25日 ⁄ 综合 ⁄ 共 6643字 ⁄ 字号 评论关闭

检测你的IE使用的代理服务器 

 

//-----------------------------------------------
//记得引用 WinInet 单元
//-----------------------------------------------
uses
WinInet

//----------------------------------------------
//定义要使用的函数
//----------------------------------------------

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没有使用的代理服务器')
      end
    else
  Label1.Caption := ProxyServer;
  label2.Caption := IntToStr(ProxyPort);

end;

//Download http file through proxy
//--------------------------------------------------------------------------------
We can find details about Proxy Authentication and Sever Authentication methed in MSDN.
Just search for the keyword "INTERNET_OPTION_USERNAME".
Here I give a very simple example. It works in my project.
CString GeHttptFile(const char *url)
{
CString szContent;
char strProxyList[MAX_PATH], strUsername[64], strPassword[64];
//in this case "proxya" is the proxy server name, "8080" is its port
strcpy(strProxyList, "proxya:8080");
strcpy(strUsername, "myusername");
strcpy(strPassword, "mypassword");
DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
CString szServer, szObject;
INTERNET_PORT nPort;
AfxParseURL(url, dwServiceType, szServer, szObject, nPort);
CInternetSession mysession;
CHttpConnection* pConnection;
CHttpFile* pHttpFile;
pConnection = mysession.GetHttpConnection(szServer,
INTERNET_FLAG_KEEP_CONNECTION,
INTERNET_INVALID_PORT_NUMBER,
NULL, NULL);
pHttpFile = pConnection->OpenRequest("GET", szObject,
NULL, 0, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION);
//here for proxy
INTERNET_PROXY_INFO proxyinfo;
proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy = strProxyList;
proxyinfo.lpszProxyBypass = NULL;
mysession.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));
pHttpFile->SetOption(INTERNET_OPTION_PROXY_USERNAME, strUsername, strlen(strUsername)+1);
pHttpFile->SetOption(INTERNET_OPTION_PROXY_PASSWORD, strPassword, strlen(strPassword)+1);

pHttpFile->SendRequest(NULL);
DWORD nFileSize = pHttpFile->GetLength();
LPSTR rbuf = szContent.GetBuffer(nFileSize);
UINT uBytesRead = pHttpFile->Read(rbuf, nFileSize);
szContent.ReleaseBuffer();
pHttpFile->Close();
delete pHttpFile;
pConnection->Close();
delete pConnection;
mysession.Close();
return szContent;
}
程序代码

//------------------------------------------------------------------
// @ProxyType 代理类型,如HTTP,SOCKS,FTP...
// @ProxyAddr 返回代理服务器的地址
// @ProxyPort 返回代理服务器的端口
// 返回值
// 0: 无错误
// -1: 查询IE的代理失败
// -2: 无此类型的代理
// -3: 其他错误
// -4: 无代理设置信息
int GetIEProxy(CString ProxyType,CString &ProxyAddr,int &ProxyPort)
{
unsigned long nSize = 4096;
char szBuf[4096] = { 0 };

// Read IE settings
INTERNET_PROXY_INFO* pInfo = (INTERNET_PROXY_INFO*)szBuf;
if(!InternetQueryOption(NULL, INTERNET_OPTION_PROXY, pInfo, &nSize))
{
return -1; //查询IE的代理失败!
}

CString strTmp (pInfo->lpszProxy);
if (strTmp.IsEmpty()) return -4;//无代理设置信息
int nStart=0,nPos,nCurLen;

CString strProxyType = ProxyType;
strProxyType += "=";
//此时的结构如下
//socks=192.168.1.100:3080 https=192.168.1.100:808 http=192.168.1.100:808 gopher=192.168.1.100:808 ftp=192.168.1.100:808

strTmp.MakeUpper();//转化为大写
strProxyType.MakeUpper();
nCurLen = strTmp.GetLength();

nPos = strTmp.Find(strProxyType.GetBuffer(0),nStart);
if (nPos>=0)
{
strTmp = strTmp.Right(nCurLen - nPos);
strTmp.TrimLeft();
nCurLen = strTmp.GetLength();
//到此结构如下
//HTTP=192.168.1.100:808 gopher=192.168.1.100:808 ftp=192.168.1.100:808

//获取单独的协议部分
nPos = strTmp.Find(" ",nStart);
if (nPos>=0)
{
strTmp = strTmp.Left(nPos);
strTmp.TrimRight();
nCurLen = strTmp.GetLength();
//到此结构如下
//HTTP=192.168.1.100:808

//取掉协议名称部分
strTmp = strTmp.Right(nCurLen-strProxyType.GetLength());
nCurLen = strTmp.GetLength();
//到此结构如下
//192.168.1.100:808
nPos = strTmp.Find(":",nStart);
if (nPos>0)//必须要大于0
{
ProxyAddr = strTmp.Left(nPos);

strTmp = strTmp.Right(nCurLen-nPos-1);
strTmp.TrimLeft();
strTmp.TrimRight();

ProxyPort = atoi(strTmp);
return 0;
}
else
{
return -3;
}

}
else
{
return -3;
}
}
else
{
return -2;//无此类型的代理
}

}

你这个是旧的方法(IE4 and earlier)了,从IE5开始,最好使用
INTERNET_OPTION_PER_CONNECTION_OPTION来Invoke InternetQueryOption,详细内容可以参考MS Q226473

如何编程动态改变IE的代理服务器设置, 并且使之马上生效!

用到的关键函数是wininet库里面的InternetSetOption. msdn里面有对它详细的介绍, 可以自己去看看. 当把参数dwOption设置为INTERNET_OPTION_SETTINGS_CHANGED的时候. 他就会促使IE在下一次打开网页的时候重新到注册表里面去取代理的设置信息. 所以我们就可以先将注册表里面的代理信息更改掉, 然后调用InternetSetOption函数, 从而达到使自己想要的代理设置马上生效的目的.

下面的函数可以实现改变IE的http代理服务器设置的目的. 里面加了少许的注释以帮助大家理解.

BOOL SetHttpProxy(CString ip, UINT port)
{

CString l_just;
l_just.Format("http=%s:%d", ip.LockBuffer(), port);

//下面的代码将注册表项HKEY_CURRENT_USER//Software//Microsoft//Windows//CurrentVersion//Internet Settings//ProxyServer
//的内容取出来
HKEY hKeyIn = HKEY_CURRENT_USER, hKeyOut;
if( ERROR_SUCCESS != RegOpenKeyEx(hKeyIn, "Software//Microsoft//Windows//CurrentVersion//Internet Settings", 0, KEY_CREATE_LINK | KEY_WRITE | KEY_READ | KEY_NOTIFY, &hKeyOut))
{
return FALSE;
}

ULONG regsize = 0;
if(ERROR_SUCCESS != RegQueryValueEx(hKeyOut, "ProxyServer", NULL, NULL, NULL, ®size))
{
return FALSE;
}

LPBYTE pValue = new BYTE[regsize];
memset(pValue, 0x00, regsize);

if(ERROR_SUCCESS != RegQueryValueEx(hKeyOut, "ProxyServer", NULL, NULL, pValue, ®size))
{
return FALSE;
}

CString oldproxy((char *)pValue);
delete [] pValue;
pValue = NULL;

//从注册表中读出来的数据格式为:http=111.111.111.111:80;ftp=222.222.222.222:21;......,
//如果你只想改变http的代理的话, 就只要把其中的111.111.111.111:80换成你想要的代理就行了,
//类似的你可以改变其他的代理.

//下面的代码就替换http代理成为参数所指定的代理.
int pos = 0;
//如果没有字符串中没有找到"http="说明用户没有设置http代理,这时候直接加在最前面.
if(-1 == (pos = oldproxy.Find("http=")))
{
pos = 0;
}

int pos1 = 0;
if(-1 == (pos1 = oldproxy.Find(";", pos)))
{
pos1 = oldproxy.GetLength();
}

oldproxy.Delete(pos, pos1 - pos);
oldproxy.Insert(pos, l_just);

if(ERROR_SUCCESS != RegSetValueEx(hKeyOut, "ProxyServer", 0, REG_SZ, (const unsigned char *)oldproxy.LockBuffer(), oldproxy.GetLength() + 1))
{
return FALSE;
}

RegCloseKey(hKeyOut);

//使设置生效
if(!InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0))
{
return FALSE;
}

return TRUE;
}

抱歉!评论已关闭.