Display webview with basic authentication inside program

旧巷老猫 提交于 2020-01-25 23:53:26

问题


I have to display a webpage inside my Java software.( you can think it is one the page of a Wireless Device configuration) But when Im gonna go to that page in browser,browser displays a user and password popup and I enter the user and password and go to that page.( notice that username is always root, but password can be different) Now I am gonna display the page from Java software, I could link the page and open the page via Java, But the host inside the wireless device displays : 401 Unauthorized, The URL of the page I want to display is http://192.168.1.2/Wireless I used Fiddler to monitor its behavior, that URL is continuously reloading. And here is the Header of the Get method of that :

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

How can I do that? With HttpClient ? Does anybody knows how to do that? And give me a solution? Or a sample or anything can help me?| Thanks.

EDIT : Here is my code to display the 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);
});

And even it is a GET method, the user and password does not post via URL.


回答1:


I did it in this way, I created a class with a constructor, the all is in constructor, notice that my constructor what I wanted, you can change it, but the algorithm is this :

    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());            
        }       
    }
}


来源:https://stackoverflow.com/questions/44387512/display-webview-with-basic-authentication-inside-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!