java 从java打开一个URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12348463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening an URL from java
提问by Balazs Gunics
we're writing an open-source jdbc driver for bigquery, and ran into the following problem:
我们正在为 bigquery 编写一个开源 jdbc 驱动程序,但遇到了以下问题:
We want to authorize our driver with Oauth 2 as an installed application. On windows xp, windows 7 x64, windows 7 x64 + RDP it works fine. But on the testbench which is a windows server 2008 R2 + RDP it fails.
我们希望使用 Oauth 2 作为已安装的应用程序来授权我们的驱动程序。在 windows xp、windows 7 x64、windows 7 x64 + RDP 上它工作正常。但是在 Windows Server 2008 R2 + RDP 的测试平台上它失败了。
Basically, we open a web browser, he logs in, we catch the reply, and authenticate the user.
基本上,我们打开一个网络浏览器,他登录,我们接收回复,并验证用户。
Here's the code for the url opening:
这是打开网址的代码:
private static void browse(String url) {
// first try the Java Desktop
logger.debug("First try the Java Desktop");
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Action.BROWSE))
try {
desktop.browse(URI.create(url));
return;
} catch (IOException e) {
// handled below
}
}
// Next try rundll32 (only works on Windows)
logger.debug("Try the rundll32");
try {
Runtime.getRuntime().exec(
"rundll32 url.dll,FileProtocolHandler " + url);
return;
} catch (IOException e) {
// handled below
}
// Next try browsers
logger.debug("Try with browsers");
BareBonesBrowserLaunch.openURL(url);
}
What i figured out is: BareBonesBrowserLaunch doesn't open the link, nor does the FileProtocolHandler.
我发现的是:BareBonesBrowserLaunch 不会打开链接,FileProtocolHandler 也不会打开。
The URL lenght is a little bit under 250character.
URL 长度略低于 250 个字符。
Any assistance would be appreciated!
任何援助将不胜感激!
回答by Andrea Turri
Using java.net.HttpURLConnection
使用 java.net.HttpURLConnection
URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
回答by Santosh
Here is a suggestion from a different perspective. (Not sure if this is an option to you)
这是从不同角度提出的建议。(不确定这是否适合您)
The problem that you are trying to solve is to use Oauth 2 and authentication mechanism. Instead of opening a browser capturing its response. There are already available libraries like Apache amberwhich do this purely in java.
您试图解决的问题是使用 Oauth 2 和身份验证机制。而不是打开浏览器捕获其响应。已经有可用的库,如Apache amber,它们纯粹是在 java 中完成的。
Here is an official Amber Examplethat you can refer to.
这是您可以参考的官方Amber 示例。
回答by xproph
Basically sounds good but here is another way if you want to open using specific browser (if more than one browser in the system)
基本上听起来不错,但如果您想使用特定浏览器打开,这是另一种方式(如果系统中有多个浏览器)
String url = "http:/devmain.blogspot.com/";
String[] browsers = { "firefox", "opera", "mozilla", "netscape" }; // common browser names
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
browser = browsers[count]; // have found a browser
Runtime.getRuntime().exec(new String[] { browser, url }) // open using a browser
More details here: http://devmain.blogspot.com/2013/10/java-how-to-open-url-in-browser.html
更多细节在这里:http: //devmain.blogspot.com/2013/10/java-how-to-open-url-in-browser.html