Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

java - Display webview with basic authentication inside program

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...