HtmlUnit 页面响应(下载文件)

HtmlUnit Page response (Downloading File)

所以我尝试使用 HtmlUnit 转到 URL,但是一旦您访问那个 url,它就会下载一个关于您想要的数据的 json 文件。不知道如何表达,但基本上在 HtmlUnit 中我如何从下载的文件中获取结果。

我懒得在这里解释了看

正在尝试通过此检查用户可用性

private static final String URL = "https://twitter.com/users/username_available?username=";

...

HtmlPage page = webClient.getPage(URL + users[finalUsersIndex]);

所以基本上为每个用户名创建一个新页面的事情是 URL + 用户名 returns 用户可用性的 json 文件。我知道如何读取 json 文件,但问题是这个

java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage
 cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage

我在这条线上得到了

HtmlPage page = webClient.getPage(URL + users[finalUsersIndex]);

我想我需要为响应创建一个新页面,但我该怎么做,因为它会自动下载文件而不是本身,单击一个下载文件的按钮。 (如果我错了请纠正我)

抱歉凌晨 4 点

顾名思义,HtmlPage 是包含 HTML 的页面。 JSON 不是 HTML。

the documentation所示;

The DefaultPageCreator will create a Page depending on the content type of the HTTP response, basically HtmlPage for HTML content, XmlPage for XML content, TextPage for other text content and UnexpectedPage for anything else.

(强调我的)。

因此,正如您得到的异常所示,您观察到的行为是记录在案的行为:您得到的页面既不是 HTML,也不是 XML,也不是文本,所以你得到一个 UnexpectedPage.

因此您的代码应该是:

UnexpectedPage page = webClient.getPage(URL + users[finalUsersIndex]);