Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过身份验证将联机pdf内容缓存到BufferedInputStream_Java_Selenium_Pdf_Selenium Chromedriver - Fatal编程技术网

Java 通过身份验证将联机pdf内容缓存到BufferedInputStream

Java 通过身份验证将联机pdf内容缓存到BufferedInputStream,java,selenium,pdf,selenium-chromedriver,Java,Selenium,Pdf,Selenium Chromedriver,我一直试图阅读在浏览器中打开的pdf的内容,但没有成功。我在网上找到的所有示例都需要此步骤: URL url = new URL(strURL); BufferedInputStream file = new BufferedInputStream(url.openStream()); PDFParser parser = new PDFParser(file); 问题是,我必须首先在站点中进行身份验证,然后导航到pdf位置,在新选项卡上打开,然后切换到该选项卡并获取u

我一直试图阅读在浏览器中打开的pdf的内容,但没有成功。我在网上找到的所有示例都需要此步骤:

    URL url = new URL(strURL);
    BufferedInputStream file = new BufferedInputStream(url.openStream());
    PDFParser parser = new PDFParser(file);
问题是,我必须首先在站点中进行身份验证,然后导航到pdf位置,在新选项卡上打开,然后切换到该选项卡并获取url

但是,当我将url传递给上面的代码时,它正在创建一个新的请求,因此它失去了身份验证,站点返回代码
401 Unauthorized

一个重要的细节-这是一个嵌入式pdf

是否有一种解决方法可以使用,比如抓取已经加载的pdf文件,而不是打一个新的电话?我没有主意了。谢谢大家!

编辑

我想到的一个可能的解决方法是在Chrome上打开此设置

Download PDF files instead of automatically opening them in Chrome    On/Off
是否可以通过编程方式打开此设置?请注意,我打开了设置-在我的Chrome配置文件上,
Selenium
也使用该配置文件-但当Selenium浏览器打开时,此设置被关闭

这可以通过添加选项来解决:

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=<path_to_profile>");
driver = new ChromeDriver(options);

最后用pdf做些什么,比如:获取文本

为避免
401未经授权
,您需要传递您的用户凭据

    URL url = new URL("YOUR_URL");

    HttpURLConnection myURLConnection = (HttpURLConnection)url.openConnection();
    String userCredentials = "username:password";
    String basicAuth = "Basic " + java.util.Base64.getEncoder().encodeToString(userCredentials.getBytes());
    myURLConnection.setRequestProperty ("Authorization", basicAuth);
    myURLConnection.setRequestMethod("GET");

    BufferedInputStream file = new BufferedInputStream(myURLConnection.getInputStream());
    PDFParser parser = new PDFParser(file);
启用下载

    ChromeOptions options = new ChromeOptions();
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("download.prompt_for_download", false);
    prefs.put("download.directory_upgrade", true);
    prefs.put("download.default_directory", "path-to-download-directory");
    options.setExperimentalOption("prefs", prefs);
    WebDriver driver = new ChromeDriver(options);
ChromeOptions选项=新的ChromeOptions();
Map prefs=新的HashMap();
prefs.put(“下载.提示下载”,false);
prefs.put(“download.directory\u upgrade”,true);
prefs.put(“download.default_目录”,“下载目录路径”);
选项。设置实验选项(“prefs”,prefs);
WebDriver=新的ChromeDriver(选项);
    URL url = new URL("YOUR_URL");

    HttpURLConnection myURLConnection = (HttpURLConnection)url.openConnection();
    String userCredentials = "username:password";
    String basicAuth = "Basic " + java.util.Base64.getEncoder().encodeToString(userCredentials.getBytes());
    myURLConnection.setRequestProperty ("Authorization", basicAuth);
    myURLConnection.setRequestMethod("GET");

    BufferedInputStream file = new BufferedInputStream(myURLConnection.getInputStream());
    PDFParser parser = new PDFParser(file);
    ChromeOptions options = new ChromeOptions();
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("download.prompt_for_download", false);
    prefs.put("download.directory_upgrade", true);
    prefs.put("download.default_directory", "path-to-download-directory");
    options.setExperimentalOption("prefs", prefs);
    WebDriver driver = new ChromeDriver(options);