Java 在程序内部显示具有基本身份验证的webview
我必须在我的Java 在程序内部显示具有基本身份验证的webview,java,javafx,webview,get,Java,Javafx,Webview,Get,我必须在我的Java软件中显示webpage。(你可以认为这是无线设备配置的一个页面) 但当我要在浏览器中进入该页面时,浏览器会显示一个user和password弹出窗口,我输入user和password并进入该页面。(注意用户名始终是root,但密码可以不同) 现在我要显示Java软件中的页面,我可以通过Java链接页面并打开页面,但无线设备内的主机显示: 401未经授权,我要显示的页面的URL是http://192.168.1.2/Wireless 我使用了Fiddler来监视它的行为,该U
Java
软件中显示webpage
。(你可以认为这是无线设备
配置的一个页面)
但当我要在浏览器中进入该页面时,浏览器会显示一个user
和password
弹出窗口,我输入user
和password
并进入该页面。(注意用户名始终是root,但密码可以不同)
现在我要显示Java软件中的页面,我可以通过Java
链接页面并打开页面,但无线设备内的主机显示:
401未经授权
,我要显示的页面的URL是http://192.168.1.2/Wireless
我使用了Fiddler
来监视它的行为,该URL正在不断地重新加载。
下面是它的Get方法的头:
GET /Wireless HTTP/1.1
Host: 192.168.1.2
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.1.2/Wireless
Authorization: Basic xxxxxxxxx
Connection: keep-alive
我该怎么做?使用HttpClient
?
有人知道怎么做吗?给我一个解决方案?
或者一个样品或者任何能帮我的东西|
谢谢
编辑:
以下是显示webview的代码:
myFrame.setSize(mainJTabbed.getSize());
myFrame.setLocationRelativeTo(mainJTabbed);
myFrame.setVisible(true);
myFrame.add(myFXPanel);
Platform.runLater(() -> {
BorderPane borderPane = new BorderPane();
WebView webComponent = new WebView();
webComponent.getEngine().load("myURL");
borderPane.setCenter(webComponent);
Scene scene = new Scene(borderPane,450,450);
myFXPanel.setScene(scene);
});
即使它是一个GET方法,用户和密码也不会通过
URL
发布。我是这样做的,我用构造函数创建了一个类,所有都在构造函数中,请注意,我的构造函数是我想要的,您可以更改它,但算法是这样的:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javax.swing.JFrame;
public class WebViewSample {
public WebViewSample(String urlString, JFrame myFrame, JFXPanel myFXPanel) {
try {
// Sets the authenticator that will be used by the networking code
// when a proxy or an HTTP server asks for authentication.
Authenticator.setDefault(new CustomAuthenticator());
URL url = new URL("http://" +urlString + "/wireless");
Platform.runLater(() -> {
BorderPane borderPane = new BorderPane();
WebView webComponent = new WebView();
webComponent.getEngine().load(url.toString());
borderPane.setCenter(webComponent);
Scene scene = new Scene(borderPane,450,450);
myFXPanel.setScene(scene);
});
}
catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
public static class CustomAuthenticator extends Authenticator {
// Called when password authorization is needed
protected PasswordAuthentication getPasswordAuthentication() {
// Get information about the request
String prompt = getRequestingPrompt();
String hostname = getRequestingHost();
InetAddress ipaddr = getRequestingSite();
int port = getRequestingPort();
String username = "myUserName";
String password = "myPassword";
// Return the information (a data holder that is used by Authenticator)
return new PasswordAuthentication(username, password.toCharArray());
}
}
}
你看过这个吗?如果这不是一个重复的问题,请编辑你的问题,包括一个显示你修改过的方法。谢谢你的回答,先生,但正如你所看到的,这是一个GET方法,但它在url中不显示任何内容,只显示url。web显示验证弹出窗口,然后转到192.168.1.2/wireless