想了解使用Java检测Windows/IE代理设置的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java代码检测工具的相关问题,此外,我们还将为您介绍关于c#–检测Windows重新启动是
想了解使用Java检测Windows / IE代理设置的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java代码检测工具的相关问题,此外,我们还将为您介绍关于c# – 检测Windows重新启动是否是由Windows更新引起的、c# – 检测Windows错误/弹出窗口、delphi – 检测IE代理设置并使用TIdHTTP、Delphi应用程序如何检测Windows PC的网络代理设置?的新知识。
本文目录一览:- 使用Java检测Windows / IE代理设置(java代码检测工具)
- c# – 检测Windows重新启动是否是由Windows更新引起的
- c# – 检测Windows错误/弹出窗口
- delphi – 检测IE代理设置并使用TIdHTTP
- Delphi应用程序如何检测Windows PC的网络代理设置?
使用Java检测Windows / IE代理设置(java代码检测工具)
我需要自动检测用户是否需要代理才能访问互联网。Java应用程序是否可以读取系统代理设置?
谢谢,吉米
答案1
小编典典Java SE
1.5提供了ProxySelector类来检测代理设置。如果存在与Internet的直接连接,则代理类型将为DIRECT,否则它将返回主机和端口。
下面的示例说明了此功能:
import java.net.InetSocketAddress;import java.net.Proxy;import java.net.ProxySelector;import java.net.URI;import java.util.Iterator;import java.util.List;public class TestProxy { public static void main(String[] args) { try { System.setProperty("java.net.useSystemProxies","true"); List<Proxy> l = ProxySelector.getDefault().select( new URI("http://www.yahoo.com/")); for (Iterator<Proxy> iter = l.iterator(); iter.hasNext(); ) { Proxy proxy = iter.next(); System.out.println("proxy hostname : " + proxy.type()); InetSocketAddress addr = (InetSocketAddress)proxy.address(); if(addr == null) { System.out.println("No Proxy"); } else { System.out.println("proxy hostname : " + addr.getHostName()); System.out.println("proxy port : " + addr.getPort()); } } } catch (Exception e) { e.printStackTrace(); } }}
c# – 检测Windows重新启动是否是由Windows更新引起的
这非常有用,因为这些应用程序会重新加载更改,甚至是未保存的更改或还原选项卡(如果是浏览器).执行此操作的应用程序示例:
>谷歌浏览器
> Microsoft Visual Studio
> Microsoft Paint
在所有情况下,应用程序都会保存重启前的状态.
(我的PC实际上是从待机状态自动唤醒,并在安装更新时重新启动.太糟糕了,这样做后它没有回到待机状态.)
我的问题是:我如何以编程方式检测这种重启?在重启之前似乎可以可靠地检测到.
问题How can I get the Windows last reboot reason似乎是重复的,但没有回答任何关于更新的说法.
我用C#编写代码,所以C#中的答案会有所帮助,但我也可以阅读C和C.
解决方法
An application can use Application Recovery and Restart (ARR) to save data and state information before the application exits due to an unhandled exception or when the application stops responding
和:
or if the computer needs to restart as the result of an update.
事实之后,应用程序没有检测到重启原因 – 它们已被架构为使用此API.没有(据我所知)这是一个托管API.
点击几下就是Saving data and application state when application is being closed due to a software update.再次,没有托管API.
c# – 检测Windows错误/弹出窗口
我想捕获这些异常,并且不希望向用户显示这些错误窗口.
解决方法
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
delphi – 检测IE代理设置并使用TIdHTTP
它应该与XP / Vista / Win7兼容可靠.
解决方法
InternetQueryOption
功能.
更新:
这是使用WinHTTP
的代码,应尝试从IE接收设置.如果它们可用并且自动检测代理设置或自动配置脚本URL选项被设置,则将执行代理检测.当IE设置不可用时,也会执行自动检测.
免责声明:
以下代码仅在最简单的情况下进行测试,当IE设置可用并且代理设置未被配置为自动检测(没有环境)时.还请注意,本单元中的一些功能,结构和常数是额外的.
unit Unit1; interface uses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; type HINTERNET = Pointer; {$EXTERNALSYM HINTERNET} INTERNET_PORT = Word; {$EXTERNALSYM INTERNET_PORT} PWinHTTPProxyInfo = ^TWinHTTPProxyInfo; WINHTTP_PROXY_INFO = record dwAccesstype: DWORD; lpszProxy: LPWSTR; lpszProxyBypass: LPWSTR; end; {$EXTERNALSYM WINHTTP_PROXY_INFO} TWinHTTPProxyInfo = WINHTTP_PROXY_INFO; LPWINHTTP_PROXY_INFO = PWinHTTPProxyInfo; {$EXTERNALSYM LPWINHTTP_PROXY_INFO} PWinHTTPAutoproxyOptions = ^TWinHTTPAutoproxyOptions; WINHTTP_AutopROXY_OPTIONS = record dwFlags: DWORD; dwAutoDetectFlags: DWORD; lpszAutoConfigUrl: LPCWSTR; lpvReserved: Pointer; dwReserved: DWORD; fAutologonIfChallenged: BOOL; end; {$EXTERNALSYM WINHTTP_AutopROXY_OPTIONS} TWinHTTPAutoproxyOptions = WINHTTP_AutopROXY_OPTIONS; LPWINHTTP_AutopROXY_OPTIONS = PWinHTTPAutoproxyOptions; {$EXTERNALSYM LPWINHTTP_AutopROXY_OPTIONS} PWinHTTPCurrentUserIEProxyConfig = ^TWinHTTPCurrentUserIEProxyConfig; WINHTTP_CURRENT_USER_IE_PROXY_CONfig = record fAutoDetect: BOOL; lpszAutoConfigUrl: LPWSTR; lpszProxy: LPWSTR; lpszProxyBypass: LPWSTR; end; {$EXTERNALSYM WINHTTP_CURRENT_USER_IE_PROXY_CONfig} TWinHTTPCurrentUserIEProxyConfig = WINHTTP_CURRENT_USER_IE_PROXY_CONfig; LPWINHTTP_CURRENT_USER_IE_PROXY_CONfig = PWinHTTPCurrentUserIEProxyConfig; {$EXTERNALSYM LPWINHTTP_CURRENT_USER_IE_PROXY_CONfig} function WinHttpOpen(pwszUserAgent: LPCWSTR; dwAccesstype: DWORD; pwszProxyName,pwszProxyBypass: LPCWSTR; dwFlags: DWORD): HINTERNET; stdcall; external 'winhttp.dll' name 'WinHttpOpen'; {$EXTERNALSYM WinHttpOpen} function WinHttpConnect(hSession: HINTERNET; pswzServerName: LPCWSTR; nServerPort: INTERNET_PORT; dwReserved: DWORD): HINTERNET; stdcall; external 'winhttp.dll' name 'WinHttpConnect'; {$EXTERNALSYM WinHttpConnect} function WinHttpOpenRequest(hConnect: HINTERNET; pwszVerb: LPCWSTR; pwszObjectName: LPCWSTR; pwszVersion: LPCWSTR; pwszReferer: LPCWSTR; ppwszAcceptTypes: PLPWSTR; dwFlags: DWORD): HINTERNET; stdcall; external 'winhttp.dll' name 'WinHttpOpenRequest'; {$EXTERNALSYM WinHttpOpenRequest} function WinHttpQueryOption(hInet: HINTERNET; dwOption: DWORD; lpBuffer: Pointer; var lpdwBufferLength: DWORD): BOOL; stdcall; external 'winhttp.dll' name 'WinHttpQueryOption'; {$EXTERNALSYM WinHttpQueryOption} function WinHttpGetProxyForUrl(hSession: HINTERNET; lpcwszUrl: LPCWSTR; pAutoproxyOptions: LPWINHTTP_AutopROXY_OPTIONS; var pProxyInfo: WINHTTP_PROXY_INFO): BOOL; stdcall; external 'winhttp.dll' name 'WinHttpGetProxyForUrl'; {$EXTERNALSYM WinHttpGetProxyForUrl} function WinHttpGetIEProxyConfigForCurrentUser( var pProxyInfo: WINHTTP_CURRENT_USER_IE_PROXY_CONfig): BOOL; stdcall; external 'winhttp.dll' name 'WinHttpGetIEProxyConfigForCurrentUser'; {$EXTERNALSYM WinHttpGetIEProxyConfigForCurrentUser} function WinHttpCloseHandle(hInternet: HINTERNET): BOOL; stdcall; external 'winhttp.dll' name 'WinHttpCloseHandle'; {$EXTERNALSYM WinHttpCloseHandle} const WINHTTP_NO_REFERER = nil; {$EXTERNALSYM WINHTTP_NO_REFERER} WINHTTP_NO_PROXY_NAME = nil; {$EXTERNALSYM WINHTTP_NO_PROXY_NAME} WINHTTP_NO_PROXY_BYPASS = nil; {$EXTERNALSYM WINHTTP_NO_PROXY_BYPASS} WINHTTP_DEFAULT_ACCEPT_TYPES = nil; {$EXTERNALSYM WINHTTP_DEFAULT_ACCEPT_TYPES} WINHTTP_ACCESS_TYPE_DEFAULT_PROXY = 0; {$EXTERNALSYM WINHTTP_ACCESS_TYPE_DEFAULT_PROXY} WINHTTP_ACCESS_TYPE_NO_PROXY = 1; {$EXTERNALSYM WINHTTP_ACCESS_TYPE_NO_PROXY} WINHTTP_OPTION_PROXY = 38; {$EXTERNALSYM WINHTTP_OPTION_PROXY} WINHTTP_OPTION_PROXY_USERNAME = $1002; {$EXTERNALSYM WINHTTP_OPTION_PROXY_USERNAME} WINHTTP_OPTION_PROXY_PASSWORD = $1003; {$EXTERNALSYM WINHTTP_OPTION_PROXY_PASSWORD} WINHTTP_AutopROXY_AUTO_DETECT = $00000001; {$EXTERNALSYM WINHTTP_AutopROXY_AUTO_DETECT} WINHTTP_AutopROXY_CONfig_URL = $00000002; {$EXTERNALSYM WINHTTP_AutopROXY_CONfig_URL} WINHTTP_AUTO_DETECT_TYPE_DHCP = $00000001; {$EXTERNALSYM WINHTTP_AUTO_DETECT_TYPE_DHCP} WINHTTP_AUTO_DETECT_TYPE_DNS_A = $00000002; {$EXTERNALSYM WINHTTP_AUTO_DETECT_TYPE_DNS_A} WINHTTP_FLAG_BYPASS_PROXY_CACHE = $00000100; {$EXTERNALSYM WINHTTP_FLAG_BYPASS_PROXY_CACHE} WINHTTP_FLAG_REFRESH = WINHTTP_FLAG_BYPASS_PROXY_CACHE; {$EXTERNALSYM WINHTTP_FLAG_REFRESH} var Form1: TForm1; implementation {$R *.dfm} type TProxyInfo = record ProxyURL: WideString; ProxyBypass: WideString; ProxyAutoDetected: Boolean; end; function GetProxyInfo(const AURL: WideString; var AProxyInfo: TProxyInfo): DWORD; var Session: HINTERNET; AutoDetectProxy: Boolean; WinHttpProxyInfo: TWinHTTPProxyInfo; AutoproxyOptions: TWinHTTPAutoproxyOptions; IEProxyConfig: TWinHTTPCurrentUserIEProxyConfig; begin // initialize the result Result := 0; // initialize auto-detection to off AutoDetectProxy := False; // initialize the result structure AProxyInfo.ProxyURL := ''; AProxyInfo.ProxyBypass := ''; AProxyInfo.ProxyAutoDetected := False; // initialize the auto-proxy options FillChar(AutoproxyOptions,SizeOf(AutoproxyOptions),0); // check if the Internet Explorer's proxy configuration is // available and if so,check its settings for auto-detect // proxy settings and auto-config script URL options if WinHttpGetIEProxyConfigForCurrentUser(IEProxyConfig) then begin // if the Internet Explorer is configured to auto-detect // proxy settings then we try to detect them later on if IEProxyConfig.fAutoDetect then begin AutoproxyOptions.dwFlags := WINHTTP_AutopROXY_AUTO_DETECT; AutoproxyOptions.dwAutoDetectFlags := WINHTTP_AUTO_DETECT_TYPE_DHCP or WINHTTP_AUTO_DETECT_TYPE_DNS_A; AutoDetectProxy := True; end; // if the Internet Explorer is configured to use the proxy // auto-config script then we try to use it if IEProxyConfig.lpszAutoConfigURL <> '' then begin AutoproxyOptions.dwFlags := AutoproxyOptions.dwFlags or WINHTTP_AutopROXY_CONfig_URL; AutoproxyOptions.lpszAutoConfigUrl := IEProxyConfig.lpszAutoConfigUrl; AutoDetectProxy := True; end; // if IE don't have auto-detect or auto-config set,we are // done here and we can fill the AProxyInfo with the IE settings if not AutoDetectProxy then begin AProxyInfo.ProxyURL := IEProxyConfig.lpszProxy; AProxyInfo.ProxyBypass := IEProxyConfig.lpszProxyBypass; AProxyInfo.ProxyAutoDetected := False; end; end else begin // if the Internet Explorer's proxy configuration is not // available,then try to auto-detect it AutoproxyOptions.dwFlags := WINHTTP_AutopROXY_AUTO_DETECT; AutoproxyOptions.dwAutoDetectFlags := WINHTTP_AUTO_DETECT_TYPE_DHCP or WINHTTP_AUTO_DETECT_TYPE_DNS_A; AutoDetectProxy := True; end; // if the IE proxy settings are not available or IE has // configured auto-config script or auto-detect proxy settings if AutoDetectProxy then begin // create a temporary WinHttp session to allow the WinHTTP // auto-detect proxy settings if possible Session := WinHttpOpen(nil,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0); // if the WinHttp session has been created then try to // get the proxy data for the specified URL else we assign // the last error code to the function result if Assigned(Session) then try // get the proxy data for the specified URL with the // auto-proxy options specified,if succeed then we can // fill the AProxyInfo with the retrieved settings else // we assign the last error code to the function result if WinHttpGetProxyForUrl(Session,LPCWSTR(AURL),@AutoproxyOptions,WinHttpProxyInfo) then begin AProxyInfo.ProxyURL := WinHttpProxyInfo.lpszProxy; AProxyInfo.ProxyBypass := WinHttpProxyInfo.lpszProxyBypass; AProxyInfo.ProxyAutoDetected := True; end else Result := GetLastError; finally WinHttpCloseHandle(Session); end else Result := GetLastError; end; end; procedure TForm1.Button1Click(Sender: TObject); var Result: DWORD; ProxyInfo: TProxyInfo; begin Result := GetProxyInfo('http://www.example.com',ProxyInfo); case Result of 0: ShowMessage( 'Proxy URL: ' + ProxyInfo.ProxyURL + sLineBreak + 'Proxy bypass: ' + ProxyInfo.ProxyBypass + sLineBreak + 'Proxy autodetected: ' + BoolToStr(ProxyInfo.ProxyAutoDetected,True)); 12166: ShowMessage('Error in proxy auto-config script code'); 12167: ShowMessage('Unable to download proxy auto-config script'); 12180: ShowMessage('WPAD detection Failed'); else ShowMessage('Last error: ' + IntToStr(Result)); end; end; procedure TForm1.FormCreate(Sender: TObject); begin ReportMemoryLeaksOnShutdown := True; end; end.
对于替代Delphi代码,您可以检查this tip
.
以下是如何使用获取的代理设置设置TIdHTTP的示例(实际上您只需解析获取的代理URL并将其传递到ProxyServer
和ProxyPort
属性):
uses IdGlobal; procedure TForm1.Button1Click(Sender: TObject); var S: string; Result: DWORD; ProxyInfo: TProxyInfo; begin Result := GetProxyInfo('http://www.example.com',ProxyInfo); if Result <> 0 then IdHTTP1.ProxyParams.Clear else begin S := ProxyInfo.ProxyURL; IdHTTP1.ProxyParams.ProxyServer := Fetch(S,':'); IdHTTP1.ProxyParams.ProxyPort := StrToInt(S); end; end;
Delphi应用程序如何检测Windows PC的网络代理设置?
alt text http://toybase.files.wordpress.com/2008/11/ie-proxy-settings.png
坦白说,大多数人不会知道或关心这种情况.
可以通过Delphi-7应用程序的一些系统调用获取这些信息吗?
非常感谢!
解决方法
OP编辑后:您可以从注册表读取,AFAIR将位于此处:
[ HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Internet Settings ]
我们今天的关于使用Java检测Windows / IE代理设置和java代码检测工具的分享已经告一段落,感谢您的关注,如果您想了解更多关于c# – 检测Windows重新启动是否是由Windows更新引起的、c# – 检测Windows错误/弹出窗口、delphi – 检测IE代理设置并使用TIdHTTP、Delphi应用程序如何检测Windows PC的网络代理设置?的相关信息,请在本站查询。
本文标签: