Delphi 設置WebBrowser 代理服務器 與 UserAgent

uses UrlMon, WinInet;

{-------------------------------------------------------------------------------
  過程名:    SetProcessProxy
  做者:      kelei
  日期:      2013.08.03
  參數:      aProxyServer代理服務器; aProxyPort代理服務器端口
  返回值:    True設置成功
  SetProcessProxy('127.0.0.1', 80);
-------------------------------------------------------------------------------}
function SetProcessProxy(const aProxyServer: string; const aProxyPort: Integer): Boolean;
var
  vProxyInfo: TInternetProxyInfo;
begin
  vProxyInfo.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
  vProxyInfo.lpszProxy := PChar(Format('http=%s:%d', [aProxyServer, aProxyPort]));
  vProxyInfo.lpszProxyBypass := PChar('');
  Result := UrlMkSetSessionOption(INTERNET_OPTION_PROXY, @vProxyInfo, SizeOf(vProxyInfo, 0) = S_OK;
end;

{-------------------------------------------------------------------------------
  過程名:    SetProcessUserAgent
  做者:      kelei
  日期:      2013.08.03
  參數:      aUserAgent HTTP請求頭UserAgent內容
  返回值:    True設置成功
  SetProcessUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
-------------------------------------------------------------------------------}
function SetProcessUserAgent(const aUserAgent: string): Boolean;
begin
  Result := UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, PChar(aUserAgent), Length(aUserAgent), 0) = S_OK;
end;
相關文章
相關標籤/搜索