0

I need a little help:

uses wininet, urlmon;
....

var proxy_info : PInternetProxyInfo;
....

begin
  user:='mycooluser';
  pass:='mycoolpass';
  UserAgent:='MSIE';
  New (proxy_info);
  proxy_info^.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
  proxy_info^.lpszProxy := PAnsiChar('XXX.XXX.XXX.XXX:ZZZZ');
  proxy_info^.lpszProxyBypass := PAnsiChar('');
  UrlMkSetSessionOption(INTERNET_OPTION_PROXY_USERNAME, PAnsichar(user), Length(user)+1, 0);
  UrlMkSetSessionOption(INTERNET_OPTION_PROXY_PASSWORD, PAnsichar(pass), Length(pass)+1, 0);
  UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, PChar(UserAgent), Length(UserAgent)+1, 0);
  UrlMkSetSessionOption(INTERNET_OPTION_PROXY, proxy_info, SizeOf(Internet_Proxy_Info), 0);
  Dispose(proxy_info);

  EmbeddedWB1.Navigate('http://2ip.ru');
end;

But it's doesn't work, although this proxy 100% working if its just specify in the IE settings.

Red October
  • 689
  • 2
  • 12
  • 31
  • 2
    "But it's doesn't work" is meaningless, unless you tell us what that means. We can't see your screen or read your mind - you need to explain "doesn't work". You also need to actually ask a question - there's not one anywhere in your post. – Ken White Oct 04 '12 at 20:00
  • 1
    Of course, you're right, sorry. I tried many different proxy, but with the same results: 1. Proxies without authentication are works well 2. Proxies with authentication don't allow the browser to go to the requested page - just shows "can not display the page". – Red October Oct 05 '12 at 00:58
  • Edit your post, and provide the information there so people can see it. You still haven't asked an actual question, either. – Ken White Oct 05 '12 at 01:03
  • 1
    @KenWhite He is asking why the code won't work with proxies that need authentication. Is that enough for you? –  Jul 22 '13 at 11:21
  • @RedOctober Have you figured a solution to this? If so please post it as answer. –  Jul 22 '13 at 11:22
  • Did you set the Silent:= True? Probably this is the problem. – Gabriel Feb 05 '15 at 21:45
  • Note to all readers: this piece of code changes the proxy for ALL internet explorer sessions in the system (not only for the 'local' TWebBrowser) – Gabriel Feb 24 '15 at 14:42

1 Answers1

0
unit Unit1;

// Code By Alireza Talebi  , asiapardaz.blogfa.com  ,  alireza.talebi90@yahoo.com
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, urlmon, wininet, StdCtrls, OleCtrls, SHDocVw, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit; // Proxy Address
    Edit2: TEdit; // Port
    Edit3: TEdit; // Web Address
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  i:Integer;
implementation

{$R *.dfm}

procedure proxy(text:string);
var PIInfo: PInternetProxyInfo;
begin
New(PIInfo);
PIInfo^.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
PIInfo^.lpszProxy:=(PAnsiChar(text));
PIInfo^.lpszProxyBypass := PChar('');
UrlMkSetSessionOption(INTERNET_OPTION_PROXY, piinfo, SizeOf(Internet_Proxy_Info), 0);
Dispose(PIInfo);
end;

procedure DeleteIECache;
var
  lpEntryInfo: PInternetCacheEntryInfo;
  hCacheDir: LongWord;
  dwEntrySize: LongWord;
begin
  dwEntrySize := 0;
  FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
  GetMem(lpEntryInfo, dwEntrySize);
  if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
  hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
  if hCacheDir <> 0 then 
  begin
    repeat
      DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName);
      FreeMem(lpEntryInfo, dwEntrySize);
      dwEntrySize := 0;
      FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize);
      GetMem(lpEntryInfo, dwEntrySize);
      if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
    until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize);
  end;
  FreeMem(lpEntryInfo, dwEntrySize);
  FindCloseUrlCache(hCacheDir);
end;

procedure EndBrowserSession;
begin
  InternetSetOption(nil, INTERNET_OPTION_END_BROWSER_SESSION, nil, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DeleteIECache;
EndBrowserSession;
proxy(Trim(Edit1.Text)+':'+Trim(Edit2.Text)); // Proxy:Port
WebBrowser1.Navigate(Trim(Edit3.Text));
end;

end.
  • Instead of or in addition to just writing all the code, it would be nice if you could give some insights into why your solution is working. – simbabque Jan 30 '14 at 00:07