从java打开URL [英] Opening an URL from java
问题描述
我们正在为bigquery编写一个开源的jdbc驱动程序,并遇到了以下问题:
we're writing an open-source jdbc driver for bigquery, and ran into the following problem:
我们想要在安装了Oauth 2的情况下授权我们的驱动程序应用。在Windows XP,Windows 7 x64,Windows 7 x64 + RDP上它工作正常。但是在Windows服务器2008 R2 + RDP的测试平台上,它失败了。
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.
基本上,我们打开一个Web浏览器,他登录,我们收到回复,并进行身份验证用户。
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);
}
我想到的是:BareBonesBrowserLaunch不会打开链接,也不会FileProtocolHandler。
What i figured out is: BareBonesBrowserLaunch doesn't open the link, nor does the FileProtocolHandler.
URL长度略低于250字符。
The URL lenght is a little bit under 250character.
任何帮助都将不胜感激!
Any assistance would be appreciated!
推荐答案
使用 java.net.HttpURLConnection
URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
这篇关于从java打开URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!