简体   繁体   English

使用Java设置的代理设置打开浏览器

[英]Open browser with proxy setting set from Java

I need to open a browser from Java code. 我需要从Java代码打开浏览器。 I understand this can be done as follows : 我了解可以通过以下方式完成此操作:

java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://google.com"));

But i need the browser to use certain proxy settings as well. 但是我也需要浏览器使用某些代理设置。 (ie when the browser opens, its proxy settings must be set to certain values.) I tried using the follwoing code but it doesnt work : (即,打开浏览器时,必须将其代理设置设置为某些值。)我尝试使用下面的代码,但是它不起作用:

public static void main(String asf[]){
    System.setProperty("java.net.useSystemProxies", "true");
    System.setProperty("http.proxyHost", "127.0.0.1");
    System.setProperty("http.proxyPort", "8080");
    try {
        java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://google.com"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("done");
}

Setting the proxy from command line using 使用以下命令从命令行设置代理

 java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080

is not an option for me. 对我来说不是一个选择。 How do i accomplish this? 我该如何完成?

Your code is largely correct which deals with setting the proxy, but in case it is not working there is another way to set the proxy via Java code and that is via the proxy class. 您的代码在很大程度上与设置代理有关,但是在不起作用的情况下,还有另一种通过Java代码(即通过代理类)设置代理的方法。

SocketAddress addr = new InetSocketAddress("socks.example.com", 1080);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
Socket socket = new Socket(proxy);
InetSocketAddress dest = new InetSocketAddress("server.example.org", 1234);
socket.connect(dest);

Here the socket will try to connect to its destination address (server.example.org:1234) through the specified SOCKS proxy. 在这里,套接字将尝试通过指定的SOCKS代理连接到其目标地址(server.example.org:1234)。

For more detail you can go through the Standard Java Documentation for Proxies 有关更多详细信息,您可以阅读《 标准Java代理文档》。

Your solution for opening a browser can be improved by adding a check 您可以通过添加检查来改善打开浏览器的解决方案

if(Desktop.isDesktopSupported())
{
  Desktop.getDesktop().browse(new URI("http://www.google.com"));
}

this is in addition to your solution .... maybe you can call it an alternate way 这是您解决方案的补充....也许您可以将其称为替代方式

try    { 
    Process p=Runtime.getRuntime().exec("cmd /c start http://www.google.com"); 
} 
catch(IOException e1) {
    System.out.println(e1);
} 

The Google Chromes proxy switches can be useful here. Google Chrome浏览器的代理开关在这里很有用。 We can just make a shortcut for the chrome browser whose target contains the switch --proxy-server=127.0.0.1:8080 . 我们可以为目标包含开关--chrome-server = 127.0.0.1:8080的chrome浏览器创建快捷方式。 Now this shortcut can be opened from java code using the Runtime class' exec method. 现在,可以使用Runtime类的exec方法从Java代码中打开此快捷方式。 The arguments to exec will be "cmd /c start /d \\"d:\\" chrome.lnk" where d: is the path of my shortcut. exec的参数为“ cmd / c start / d \\” d:\\“ chrome.lnk”,其中d:是我的快捷方式的路径。 A detailed description of this technique can be found here http://sleepingthreads.blogspot.in/2013/07/open-browser-with-proxy-settings-set.html 有关此技术的详细说明,请参见http://sleepingthreads.blogspot.in/2013/07/open-browser-with-proxy-settings-set.html

Note that Google states that the use of switches is not recommended. 请注意,Google声明不建议使用开关。 So use this as a temporary solution only. 因此,仅将其用作临时解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM