[WinHTTP API] Proxyeinstellungen lesen und interpretieren
Hallo zusammen,
Ich bin gerade ein Programm amschreiben, dass die Proxyeinstellungen liesst. Ich habe dafür in der MSDN ein bischen nachgeschaut, wie und wo man das alles Einstellen kann. Unter "Internetoptionen" in Windows ist das erste und ""Proxycfg.exe" das zweite, wenn ich das richtig verstanden habe. Das erste kann man ja grösstenteils aus der Registry lesen. Jedoch wäre es ein rissen Aufwand ein "PAC" Script Interpreter zuschreiben (bzw. geht das auch einfacher?). Das eigentliche Problem ist, was mache ich wenn ich ein PAC Script downloaden muss und dieses interpretieren muss? Daraufhin habe ich mir die WinHTTP APIs angeschaut und versuche nun damit irgendwie damit die Einstellungen zulesen und ihn dazuzubringen das PAC Script downzuloaden und zu interpretieren. Die Funktion muss aber auch funktionieren, wenn es kein PAC-Script gibt. Z.b. wenn kein Proxy benötigt wird oder wenn eine Proxyserveradresse vorliegt. Ich habe dafür ein paar WinHTTP Funktionen zu delphi interpretiert (sind richtig interpretiert). Laut diesem Artikel von MSDN, wenn ich es richtig verstanden habe, kann man mit "WinHttpGetProxyForUrl" für eine URL denn Proxy auslesen. Jedoch führt dies zu einem undokumentierten Fehler mit "ERROR_ACCESS_DENIED". Ich habe es schon als Admin ausgeführt, keine Veränderung und es liegt auch nicht an meinem Rechner. Ich habe es bei einem Kumepl getestet, der ein Proxy-PAC-Script verweendet, genau das gleiche. Irgendwo habe ich was übersehen. Kann mir jemand helfen? Hat jemand das schonmal selber gemacht? Es ist kein Service, wie bei der MSDN erwähnt. Eine ganz normale Desktop-Anwendung. Getestet auf Win7. Das Ziel ist einfach, das ich die Proxyeinstellungen lesen und interpretieren kann. Oder geht dies auch einfacher ohne WinHTTP? So hier der Code:
Delphi-Quellcode:
Hoffe jemand weiss eine Lösung.
function TForm2.ReadProxyFromURL : Boolean;
var ProxySettings : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG; AutoProxyOptions : WINHTTP_AUTOPROXY_OPTIONS; ProxyInfo : WINHTTP_PROXY_INFO; Error : Cardinal; ThisResult : Boolean; hSite : Pointer; WinHTTP_Flags : DWORD; WinHTTP_Proxy,WinHTTP_ProxyByPass : LPCWSTR; begin ThisResult := False; if hWinHTTP_DLL <> 0 then begin if (@WinHttpOpen <> nil) and (@WinHttpGetProxyForUrl <> nil) and (@WinHttpCloseHandle <> nil) then begin Memo1.Lines.BeginUpdate; Memo1.Lines.Clear; FillChar(ProxyInfo,SizeOf(ProxySettings),0); if WinHttpGetIEProxyConfigForCurrentUser(ProxySettings) then Memo1.Lines.Add('WinHttpGetIEProxyConfigForCurrentUser=TRUE') else Memo1.Lines.Add('WinHttpGetIEProxyConfigForCurrentUser=FALSE'); if ProxySettings.lpszProxy <> nil then begin WinHTTP_Flags := WINHTTP_ACCESS_TYPE_NAMED_PROXY; WinHTTP_Proxy := ProxySettings.lpszProxy; WinHTTP_ProxyByPass := ProxySettings.lpszProxyBypass; end else begin WinHTTP_Flags := WINHTTP_ACCESS_TYPE_DEFAULT_PROXY; WinHTTP_Proxy := nil; WinHTTP_ProxyByPass := nil; end; hSite := WinHttpOpen('Proxy Read Test',WinHTTP_Flags,WinHTTP_Proxy,WinHTTP_ProxyByPass,0); if hSite <> nil then begin Memo1.Lines.Add('WinHttpOpen; hSite<>nil'); FillChar(ProxyInfo,SizeOf(ProxyInfo),0); FillChar(AutoProxyOptions,SizeOf(AutoProxyOptions),0); AutoProxyOptions.dwFlags := WINHTTP_AUTOPROXY_AUTO_DETECT; if ProxySettings.lpszAutoconfigUrl <> nil then begin AutoProxyOptions.dwFlags := AutoProxyOptions.dwFlags or WINHTTP_AUTOPROXY_CONFIG_URL; AutoProxyOptions.lpszAutoConfigUrl := ProxySettings.lpszAutoconfigUrl; end; if ProxySettings.lpszProxy <> nil then begin AutoProxyOptions.dwFlags := AutoProxyOptions.dwFlags or WINHTTP_AUTOPROXY_CONFIG_URL; AutoProxyOptions.lpszAutoConfigUrl := ProxySettings.lpszAutoconfigUrl; end; AutoProxyOptions.fAutoLogonIfChallenged := TRUE; if WinHttpGetProxyForUrl(hSite,LPCWSTR(Edit1.Text),@AutoProxyOptions,ProxyInfo) then begin Memo1.Lines.Add('WinHttpGetProxyForUrl=TRUE'); with ProxyInfo do begin case dwAccessType of WINHTTP_ACCESS_TYPE_DEFAULT_PROXY : Memo1.Lines.Add('dwAccessType=WINHTTP_ACCESS_TYPE_DEFAULT_PROXY'); WINHTTP_ACCESS_TYPE_NO_PROXY : Memo1.Lines.Add('dwAccessType=WINHTTP_ACCESS_TYPE_NO_PROXY'); WINHTTP_ACCESS_TYPE_NAMED_PROXY : Memo1.Lines.Add('dwAccessType=WINHTTP_ACCESS_TYPE_NAMED_PROXY'); end; if lpszProxy <> nil then begin Memo1.Lines.Add('lpszProxy="'+lpszProxy+'"'); GlobalFree(Cardinal(lpszProxy)); end else Memo1.Lines.Add('lpszProxy="nil"'); if lpszProxyBypass <> nil then begin Memo1.Lines.Add('lpszProxyBypass="'+lpszProxyBypass+'"'); GlobalFree(Cardinal(lpszProxyBypass)); end else Memo1.Lines.Add('lpszProxyBypass="nil"'); end; end else begin Memo1.Lines.Add('WinHttpGetProxyForUrl=FALSE'); Error := GetLastError; case Error of ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR : Memo1.Lines.Add('Error=ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR'); ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT : Memo1.Lines.Add('Error=ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT'); ERROR_WINHTTP_INVALID_URL : Memo1.Lines.Add('Error=ERROR_WINHTTP_INVALID_URL'); ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT : Memo1.Lines.Add('Error=ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT'); ERROR_WINHTTP_UNRECOGNIZED_SCHEME : Memo1.Lines.Add('Error=ERROR_WINHTTP_UNRECOGNIZED_SCHEME'); else Memo1.Lines.Add('Error='+IntToStr(Error)); end end; if ProxySettings.lpszProxy<>nil then GlobalFree(Cardinal(ProxySettings.lpszProxy)); if ProxySettings.lpszProxyBypass<>nil then GlobalFree(Cardinal(ProxySettings.lpszProxyBypass)); WinHttpCloseHandle(hSite); ThisResult := True; end else Memo1.Lines.Add('hSite="nil"'); Memo1.Lines.EndUpdate; end; end; Result := ThisResult; end; Gruß NickelM |
AW: [WinHTTP API] Proxyeinstellungen lesen und interpretieren
Proxy Server Einstellungen des IE abfragen
Ich glaube in dem Code in noch ein Bug drin und deshalb habe ich mir das vor längerer Zeit umgeschrieben:
Delphi-Quellcode:
uses ...,WinInet;
function GetProxyInformation2(handle:HINTERNET):string; var ProxyInfo: PInternetProxyInfo; Len: LongWord; begin Result := ''; Len := 4096; GetMem(ProxyInfo, Len); try if InternetQueryOption(handle, INTERNET_OPTION_PROXY, ProxyInfo, Len) then if ProxyInfo^.dwAccessType = INTERNET_OPEN_TYPE_PROXY then begin Result := ProxyInfo^.lpszProxy end; finally FreeMem(ProxyInfo); end; end; function GetProxyInformation:string; begin Result := GetProxyInformation2(nil); end; {************************************************************************** * NAME: GetProxyServer * DESC: Proxy-Server Einstellungen abfragen * PARAMS: protocol => z.B. 'http' oder 'ftp' *************************************************************************} procedure GetProxyServer(protocol:string; var ProxyServer:string; var ProxyPort:Integer); var i : Integer; proxyinfo : string; begin ProxyServer := ''; ProxyPort := 0; proxyinfo := GetProxyInformation; if proxyinfo = '' then Exit; protocol := AnsiLowerCase(protocol)+'='; i := Pos(protocol, proxyinfo); if i > 0 then begin Delete(proxyinfo, 1, i+Length(protocol)-1); i := Pos(';', proxyinfo); if i=0 then i := Pos(' ', proxyinfo); 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; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:15 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz