如何在小程序中设置Http代理
对于 Java 桌面应用程序,在我们设置这些属性后,
System.setProperty("java.net.useSystemProxies","true");
System.setProperty("http.proxyHost", "1.1.1.1");
System.setProperty("http.proxyPort", "8080");
每个 http 连接都将通过定义的代理完成。
但对于小程序来说,这些不起作用。(在小程序查看器中可以,但在浏览器中则不行。) Applet 始终使用在控制面板\java\网络设置\代理设置中定义的这些设置。
如何在小程序中设置代理? (在每个打开的连接中使用代理类对我来说不是一个解决方案。)
Applet 使用 java 1.6 进行签名和编译
For a java desktop application after we set these properties
System.setProperty("java.net.useSystemProxies","true");
System.setProperty("http.proxyHost", "1.1.1.1");
System.setProperty("http.proxyPort", "8080");
every http connection will be done through the defined proxy.
But for an applet these does not work.(In an applet viewer it does but in a browser it doesnt.)
Applet always uses these settings which are defined in control panel\java\network settings\proxy settings.
How can i set the proxy in an applet?
(Using proxy class in every opening connection is not a solution for me.)
Applet is signed and compiled with java 1.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为系统属性方法不起作用的真正原因是,当小程序启动时,Java 运行时系统已经读取了属性并设置了默认代理选择器。
您尝试过使用 ProxySelector 吗?请参阅本文档的第 4) 节。
当然,只有当您的小程序是签名小程序时,这才可能起作用。
I imagine that the real reason that the System properties approach doesn't work is that by the time the applet starts, the Java runtime system has already read the properties and set up the default proxy selector.
Have you tried using
ProxySelector
? Refer to section 4) of this document.Of course, this is only likely to work when your applet is a signed applet.
您可以使用 API 来执行此操作,但不能针对每个连接执行此操作。
查看 URL.openConnection()。它将调用委托给处理程序。处理程序由处理程序工厂创建(如果已注册)。因此,您必须注册自己的工厂,创建通过代理执行 URL 连接的 URL 处理程序(调用 URL.openConnection(proxy))。
Factory 必须实现 URLStreamHandlerFactory 接口,并且可以通过调用静态方法 URL.setURLStreamHandlerFactory() 来注册。
You can do it using API but not for each connection.
Look at URL.openConnection(). It delegates the call to handler. Handler is created by handler factory (if registered). So, you have to register your own factory, create your URL handler that performs URL connection via proxy (calls URL.openConnection(proxy)).
Factory must implement interface URLStreamHandlerFactory and can be registered by calling static method URL.setURLStreamHandlerFactory().