发怒的弓箭 · 517、518路公交下周开通_合肥市瑶海区人民政府· 2 周前 · |
无邪的圣诞树 · 凤于九天风弄完本 - 百度· 2 周前 · |
会开车的蘑菇 · “ssh_dispatch_run_fata ...· 2 月前 · |
逼格高的沙发 · 来凤藤茶、潜江小龙虾、恩施小土豆…“山货上头 ...· 4 月前 · |
英俊的大葱 · protobuf ...· 1 年前 · |
getPage(String)
method
(or
getPage(URL)
) to load a first
Page
and will continue with further processing on this page depending on its type.
Example:
HtmlPage
startPage = webClient.getPage("http://htmlunit.sf.net");
getTitleText
());
Note: a
WebClient
instance is
not thread safe
. It is intended to be used from a single thread.
WebClient
(
BrowserVersion
browserVersion,
boolean javaScriptEngineEnabled,
String
proxyHost,
int proxyPort)
BrowserVersion
and proxy server.
WebClient
(
BrowserVersion
browserVersion,
boolean javaScriptEngineEnabled,
String
proxyHost,
int proxyPort,
String
proxyScheme)
BrowserVersion
and proxy server.
WebClient
(
BrowserVersion
browserVersion,
String
proxyHost,
int proxyPort)
BrowserVersion
and proxy server.
WebClient
(
BrowserVersion
browserVersion,
String
proxyHost,
int proxyPort,
String
proxyScheme)
BrowserVersion
and proxy server.
boolean
containsWebWindow
(
WebWindow
webWindow)
deregisterWebWindow
(
WebWindow
webWindow)
download
(
WebWindow
requestingWindow,
String
target,
WebRequest
request,
boolean checkHash,
boolean forceLoad,
String
forceAttachmentWithFilename,
String
description)
static
URL
expandUrl
(
URL
baseUrl,
String
relativeUrl)
AjaxController
getAjaxController
()
AlertHandler
getAlertHandler
()
AttachmentHandler
getAttachmentHandler
()
BrowserVersion
getBrowserVersion
()
Cache
getCache
()
ClipboardHandler
getClipboardHandler
()
ConfirmHandler
getConfirmHandler
()
CookieManager
getCookieManager
()
Set
<
Cookie
>
getCookies
(
URL
url)
org.apache.http.client.CredentialsProvider
getCredentialsProvider
()
WebClient.PooledCSS3Parser
getCSS3Parser
()
org.htmlunit.cssparser.parser.CSSErrorHandler
getCssErrorHandler
()
WebWindow
getCurrentWindow
()
Executor
getExecutor
()
WebClient
.
FrameContentHandler
getFrameContentHandler
()
HTMLParserListener
getHTMLParserListener
()
IncorrectnessListener
getIncorrectnessListener
()
AbstractJavaScriptEngine
<?>
getJavaScriptEngine
()
JavaScriptErrorListener
getJavaScriptErrorListener
()
WebClient
.
getJavaScriptTimeout
()
OnbeforeunloadHandler
getOnbeforeunloadHandler
()
WebClient
.
WebClientOptions
getOptions
()
<P extends
Page
>
P
getPage
(
String
url)
getPage(WebWindow, WebRequest)
.
<P extends
Page
>
P
getPage
(
URL
url)
getPage(WebWindow, WebRequest)
.
<P extends
Page
>
P
getPage
(
WebRequest
request)
<P extends
Page
>
P
getPage
(
WebWindow
webWindow,
WebRequest
webRequest)
PageCreator
getPageCreator
()
PrintHandler
getPrintHandler
()
PrintHandler
.
PromptHandler
getPromptHandler
()
RefreshHandler
getRefreshHandler
()
ScriptPreProcessor
getScriptPreProcessor
()
WebClient
.
StatusHandler
getStatusHandler
()
WebClient
.
StorageHolder
getStorageHolder
()
List
<
TopLevelWindow
>
getTopLevelWindows
()
WebConnection
getWebConnection
()
WebConsole
getWebConsole
()
WebStartHandler
getWebStartHandler
()
WebWindow
getWebWindowByName
(
String
name)
WebWindow
that matches the specified name.
List
<
WebWindow
>
getWebWindows
()
String
guessContentType
(
File
file)
initialize
(
WebWindow
webWindow,
Page
page)
initializeEmptyWindow
(
WebWindow
webWindow,
Page
page)
boolean
isJavaScriptEnabled
()
boolean
isJavaScriptEngineEnabled
()
loadDownloadedResponses
()
HtmlPage
loadHtmlCodeIntoCurrentWindow
(
String
htmlCode)
WebResponse
loadWebResponse
(
WebRequest
webRequest)
WebResponse
from the server.
loadWebResponseInto
(
WebResponse
webResponse,
WebWindow
webWindow)
loadWebResponseInto
(
WebResponse
webResponse,
WebWindow
webWindow,
String
forceAttachmentWithFilename)
XHtmlPage
loadXHtmlCodeIntoCurrentWindow
(
String
xhtmlCode)
DialogWindow
openDialogWindow
(
URL
url,
WebWindow
opener,
Object
dialogArguments)
WebWindow
openTargetWindow
(
WebWindow
opener,
String
windowName,
String
defaultName)
WebWindow
openWindow
(
URL
url,
String
windowName)
WebWindow
openWindow
(
URL
url,
String
windowName,
WebWindow
opener)
printContentIfNecessary
(
WebResponse
webResponse)
registerWebWindow
(
WebWindow
webWindow)
removeRequestHeader
(
String
name)
removeWebWindowListener
(
WebWindowListener
listener)
WebWindowEvent
s.
reset
()
setAjaxController
(
AjaxController
newValue)
setAlertHandler
(
AlertHandler
alertHandler)
setAttachmentHandler
(
AttachmentHandler
handler)
setCache
(
Cache
cache)
setClipboardHandler
(
ClipboardHandler
handler)
setConfirmHandler
(
ConfirmHandler
handler)
setCookieManager
(
CookieManager
cookieManager)
setCredentialsProvider
(org.apache.http.client.CredentialsProvider credentialsProvider)
setCssErrorHandler
(
org.htmlunit.cssparser.parser.CSSErrorHandler
cssErrorHandler)
setCurrentWindow
(
WebWindow
window)
setExecutor
(
ExecutorService
executor)
WebClient
.
setFrameContentHandler
(
FrameContentHandler
handler)
setHTMLParserListener
(
HTMLParserListener
listener)
setIncorrectnessListener
(
IncorrectnessListener
listener)
setJavaScriptEngine
(
AbstractJavaScriptEngine
<?> engine)
setJavaScriptErrorListener
(
JavaScriptErrorListener
javaScriptErrorListener)
WebClient
.
setJavaScriptTimeout
(long timeout)
setOnbeforeunloadHandler
(
OnbeforeunloadHandler
onbeforeunloadHandler)
WebClient
.
setPageCreator
(
PageCreator
pageCreator)
setPrintHandler
(
PrintHandler
handler)
PrintHandler
to be used if Windoe.print() is called
(
Printing Spec
).
setPromptHandler
(
PromptHandler
handler)
setRefreshHandler
(
RefreshHandler
handler)
setScriptPreProcessor
(
ScriptPreProcessor
scriptPreProcessor)
WebClient
.
setStatusHandler
(
StatusHandler
statusHandler)
setWebConnection
(
WebConnection
webConnection)
setWebStartHandler
(
WebStartHandler
handler)
throwFailingHttpStatusCodeExceptionIfNecessary
(
WebResponse
webResponse)
waitForBackgroundJavaScript
(long timeoutMillis)
waitForBackgroundJavaScriptStartingBefore
(long delayMillis)
public WebClient()
BrowserVersion.getDefault()
.
public WebClient(BrowserVersion browserVersion)
BrowserVersion
.
browserVersion
- the browser version to simulate
BrowserVersion
and proxy server.
browserVersion
- the browser version to simulate
proxyHost
- the server that will act as proxy or null for no proxy
proxyPort
- the port to use on the proxy server
BrowserVersion
and proxy server.
browserVersion
- the browser version to simulate
proxyHost
- the server that will act as proxy or null for no proxy
proxyPort
- the port to use on the proxy server
proxyScheme
- the scheme http/https
public WebClient(BrowserVersion browserVersion, boolean javaScriptEngineEnabled, String proxyHost, int proxyPort)
BrowserVersion
and proxy server.
browserVersion
- the browser version to simulate
javaScriptEngineEnabled
- set to false if the simulated browser should not support javaScript
proxyHost
- the server that will act as proxy or null for no proxy
proxyPort
- the port to use on the proxy server
public WebClient(BrowserVersion browserVersion, boolean javaScriptEngineEnabled, String proxyHost, int proxyPort, String proxyScheme)
BrowserVersion
and proxy server.
browserVersion
- the browser version to simulate
javaScriptEngineEnabled
- set to false if the simulated browser should not support javaScript
proxyHost
- the server that will act as proxy or null for no proxy
proxyPort
- the port to use on the proxy server
proxyScheme
- the scheme http/https
public WebConnection getWebConnection()
public void setWebConnection(WebConnection webConnection)
webConnection
- the new web connection
public <P extends Page> P getPage(WebWindow webWindow, WebRequest webRequest) throws IOException, FailingHttpStatusCodeException
Page
will be created by the
PageCreator
configured by
setPageCreator(PageCreator)
, if any.
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.
P
- the page type
webWindow
- the WebWindow to load the result of the request into
webRequest
- the web request
IOException
- if an IO error occurs
FailingHttpStatusCodeException
- if the server returns a failing status code AND the property
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
is set to true
WebRequest
public <P extends Page> P getPage(String url) throws IOException, FailingHttpStatusCodeException, MalformedURLException
getPage(WebWindow, WebRequest)
.
P
- the page type
url
- the URL of the new content
FailingHttpStatusCodeException
- if the server returns a failing status code AND the property
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
is set to true.
IOException
- if an IO problem occurs
MalformedURLException
- if no URL can be created from the provided string
public <P extends Page> P getPage(URL url) throws IOException, FailingHttpStatusCodeException
getPage(WebWindow, WebRequest)
.
P
- the page type
url
- the URL of the new content
FailingHttpStatusCodeException
- if the server returns a failing status code AND the property
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
is set to true.
IOException
- if an IO problem occurs
public <P extends Page> P getPage(WebRequest request) throws IOException, FailingHttpStatusCodeException
P
- the page type
request
- the request parameters
FailingHttpStatusCodeException
- if the server returns a failing status code AND the property
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
is set to true.
IOException
- if an IO problem occurs
getPage(WebWindow,WebRequest)
public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow) throws IOException, FailingHttpStatusCodeException
Creates a page based on the specified response and inserts it into the specified window. All page initialization and event notification is handled here.
Note that if the page created is an attachment page, and an
AttachmentHandler
has been
registered with this client, the page is
not
loaded into the specified window; in this case,
the page is loaded into a new window, and attachment handling is delegated to the registered
AttachmentHandler
.
webResponse
- the response that will be used to create the new page
webWindow
- the window that the new page will be placed within
IOException
- if an IO error occurs
FailingHttpStatusCodeException
- if the server returns a failing status code AND the property
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
is set to true
setAttachmentHandler(AttachmentHandler)
public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow, String forceAttachmentWithFilename) throws IOException, FailingHttpStatusCodeException
Creates a page based on the specified response and inserts it into the specified window. All page initialization and event notification is handled here.
Note that if the page created is an attachment page, and an
AttachmentHandler
has been
registered with this client, the page is
not
loaded into the specified window; in this case,
the page is loaded into a new window, and attachment handling is delegated to the registered
AttachmentHandler
.
webResponse
- the response that will be used to create the new page
webWindow
- the window that the new page will be placed within
forceAttachmentWithFilename
- if not
null
, handle this as an attachment with the specified name
or if an empty string ("") use the filename provided in the response
IOException
- if an IO error occurs
FailingHttpStatusCodeException
- if the server returns a failing status code AND the property
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
is set to true
setAttachmentHandler(AttachmentHandler)
public void printContentIfNecessary(WebResponse webResponse)
Logs the response's content if its status code indicates a request failure and
WebClientOptions.isPrintContentOnFailingStatusCode()
returns
true
.
webResponse
- the response whose content may be logged
public void throwFailingHttpStatusCodeExceptionIfNecessary(WebResponse webResponse)
Throws a
FailingHttpStatusCodeException
if the request's status code indicates a request
failure and
WebClientOptions.isThrowExceptionOnFailingStatusCode()
returns
true
.
webResponse
- the response which may trigger a
FailingHttpStatusCodeException
public void addRequestHeader(String name, String value)
name
- the name of the header to add
value
- the value of the header to add
removeRequestHeader(String)
public void removeRequestHeader(String name)
addRequestHeader(String, String)
before.
name
- the name of the header to remove
addRequestHeader(java.lang.String, java.lang.String)
public void setCredentialsProvider(org.apache.http.client.CredentialsProvider credentialsProvider)
credentialsProvider
- the new credentials provider to use to authenticate
public org.apache.http.client.CredentialsProvider getCredentialsProvider()
DefaultCredentialsProvider
.
public AbstractJavaScriptEngine<?> getJavaScriptEngine()
null
)
public void setJavaScriptEngine(AbstractJavaScriptEngine<?> engine)
engine
- the new script engine to use
public CookieManager getCookieManager()
public void setCookieManager(CookieManager cookieManager)
cookieManager
- the cookie manager used by this web client
public void setAlertHandler(AlertHandler alertHandler)
alertHandler
- the new alerthandler or null if none is specified
public AlertHandler getAlertHandler()
public void setConfirmHandler(ConfirmHandler handler)
handler
- the new handler or null if no handler is to be used
public void setPromptHandler(PromptHandler handler)
handler
- the new handler or null if no handler is to be used
public void setStatusHandler(StatusHandler statusHandler)
statusHandler
- the new status handler or null if none is specified
public StatusHandler getStatusHandler()
WebClient
.
public void setExecutor(ExecutorService executor)
WebClient
.
You have to call this before the first use of the executor, otherwise
an IllegalStateExceptions is thrown.
executor
- the new Executor.
public void setJavaScriptErrorListener(JavaScriptErrorListener javaScriptErrorListener)
WebClient
.
When setting to null, the
DefaultJavaScriptErrorListener
is used.
javaScriptErrorListener
- the new JavaScriptErrorListener or null if none is specified
public JavaScriptErrorListener getJavaScriptErrorListener()
WebClient
.
public BrowserVersion getBrowserVersion()
public WebWindow getCurrentWindow()
getPage(...)
is called without specifying a window.
public void setCurrentWindow(WebWindow window)
getPage(...)
is called without specifying a window.
window
- the new "current" window for this client
public void addWebWindowListener(WebWindowListener listener)
WebWindowEvent
s. All events from all windows associated with this
client will be sent to the specified listener.
listener
- a listener
public void removeWebWindowListener(WebWindowListener listener)
WebWindowEvent
s.
listener
- a listener
public WebWindow openWindow(URL url, String windowName)
url
- the URL to load content from or null if no content is to be loaded
windowName
- the name of the new window
url
- the URL to load content from or null if no content is to be loaded
windowName
- the name of the new window
opener
- the web window that is calling openWindow
public WebWindow openTargetWindow(WebWindow opener, String windowName, String defaultName)
opener
- the web window that is calling openWindow
windowName
- the name of the new window
defaultName
- the default target if no name is given
public DialogWindow openDialogWindow(URL url, WebWindow opener, Object dialogArguments) throws IOException
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Opens a new dialog window.
url
- the URL of the document to load and display
opener
- the web window that is opening the dialog
dialogArguments
- the object to make available inside the dialog via
window.dialogArguments
IOException
- if there is an IO error
public void setPageCreator(PageCreator pageCreator)
pageCreator
- the new page creator
public WebWindow getWebWindowByName(String name) throws WebWindowNotFoundException
WebWindow
that matches the specified name.
name
- the name to search for
WebWindow
with the specified name
WebWindowNotFoundException
- if the
WebWindow
can't be found
getWebWindows()
,
getTopLevelWindows()
public void initialize(WebWindow webWindow, Page page)
webWindow
- the new WebWindow
page
- the page that will become the enclosing page
public void initializeEmptyWindow(WebWindow webWindow, Page page)
webWindow
- the new WebWindow
page
- the page that will become the enclosing page
public void registerWebWindow(WebWindow webWindow)
webWindow
- the new WebWindow
public void deregisterWebWindow(WebWindow webWindow)
webWindow
- the window to remove
public static URL expandUrl(URL baseUrl, String relativeUrl) throws MalformedURLException
new URL(baseUrl, relativeUrl)
but
there are some cases that URL doesn't handle correctly. See
RFC1808
regarding Relative Uniform Resource Locators for more information.
baseUrl
- the base URL
relativeUrl
- the relative URL
MalformedURLException
- if an error occurred when creating a URL object
public String guessContentType(File file)
file
- the file
public WebResponse loadWebResponse(WebRequest webRequest) throws IOException
WebResponse
from the server.
webRequest
- the request
IOException
- if an IO problem occurs
public List<WebWindow> getWebWindows()
getWebWindowByName(String)
,
getTopLevelWindows()
public boolean containsWebWindow(WebWindow webWindow)
webWindow
- the window to check
public List<TopLevelWindow> getTopLevelWindows()
getWebWindowByName(String)
,
getWebWindows()
public void setRefreshHandler(RefreshHandler handler)
RefreshHandler
for more details.
handler
- the new handler
public RefreshHandler getRefreshHandler()
NiceRefreshHandler(2)
.
public void setScriptPreProcessor(ScriptPreProcessor scriptPreProcessor)
WebClient
.
scriptPreProcessor
- the new preprocessor or null if none is specified
public ScriptPreProcessor getScriptPreProcessor()
WebClient
.
public void setHTMLParserListener(HTMLParserListener listener)
listener
- the new listener,
null
if messages should be totally ignored
public HTMLParserListener getHTMLParserListener()
null
if no listener is defined (default value)
public org.htmlunit.cssparser.parser.CSSErrorHandler getCssErrorHandler()
DefaultCssErrorHandler
,
SilentCssErrorHandler
public void setCssErrorHandler(org.htmlunit.cssparser.parser.CSSErrorHandler cssErrorHandler)
cssErrorHandler
- the CSS error handler used by this web client when CSS problems are encountered
DefaultCssErrorHandler
,
SilentCssErrorHandler
public void setJavaScriptTimeout(long timeout)
timeout
- the timeout value, in milliseconds
public long getJavaScriptTimeout()
public IncorrectnessListener getIncorrectnessListener()
IncorrectnessListenerImpl
.
null
)
public void setIncorrectnessListener(IncorrectnessListener listener)
listener
- the new value (not
null
)
public AjaxController getAjaxController()
public void setAjaxController(AjaxController newValue)
newValue
- the controller
public void setAttachmentHandler(AttachmentHandler handler)
handler
- the new attachment handler
public AttachmentHandler getAttachmentHandler()
public void setWebStartHandler(WebStartHandler handler)
handler
- the new WebStart handler
public WebStartHandler getWebStartHandler()
public ClipboardHandler getClipboardHandler()
public void setClipboardHandler(ClipboardHandler handler)
handler
- the new clipboard handler
public void setPrintHandler(PrintHandler handler)
PrintHandler
to be used if Windoe.print() is called
(
Printing Spec
).
handler
- the new
PrintHandler
or null if you like to
ignore print requests (default is null)
public FrameContentHandler getFrameContentHandler()
public void setFrameContentHandler(FrameContentHandler handler)
handler
- the new FrameContent handler
public void setOnbeforeunloadHandler(OnbeforeunloadHandler onbeforeunloadHandler)
WebClient
.
onbeforeunloadHandler
- the new onbeforeunloadHandler or null if none is specified
public OnbeforeunloadHandler getOnbeforeunloadHandler()
WebClient
.
public void close()
close
in interface
AutoCloseable
public void reset()
Experimental API: May be changed in next release and may not yet work perfectly!
This shuts down the whole client and restarts with a new empty window. Cookies and other states are preserved.
public int waitForBackgroundJavaScript(long timeoutMillis)
Experimental API: May be changed in next release and may not yet work perfectly!
This method blocks until all background JavaScript tasks have finished executing. Background
JavaScript tasks are JavaScript tasks scheduled for execution via
window.setTimeout
,
window.setInterval
or asynchronous
XMLHttpRequest
.
If a job is scheduled to begin executing after
(now + timeoutMillis)
, this method will
wait for
timeoutMillis
milliseconds and then return a value greater than
0
. This
method will never block longer than
timeoutMillis
milliseconds.
Use this method instead of
waitForBackgroundJavaScriptStartingBefore(long)
if you
don't know when your background JavaScript is supposed to start executing, but you're fairly sure
that you know how long it should take to finish executing.
timeoutMillis
- the maximum amount of time to wait (in milliseconds)
0
if there are no jobs left to execute
public int waitForBackgroundJavaScriptStartingBefore(long delayMillis)
Experimental API: May be changed in next release and may not yet work perfectly!
This method blocks until all background JavaScript tasks scheduled to start executing before
(now + delayMillis)
have finished executing. Background JavaScript tasks are JavaScript
tasks scheduled for execution via
window.setTimeout
,
window.setInterval
or
asynchronous
XMLHttpRequest
.
If there is no background JavaScript task currently executing, and there is no background JavaScript
task scheduled to start executing within the specified time, this method returns immediately -- even
if there are tasks scheduled to be executed after
(now + delayMillis)
.
Note that the total time spent executing a background JavaScript task is never known ahead of time, so this method makes no guarantees as to how long it will block.
Use this method instead of
waitForBackgroundJavaScript(long)
if you know roughly when
your background JavaScript is supposed to start executing, but you're not necessarily sure how long
it will take to execute.
delayMillis
- the delay which determines the background tasks to wait for (in milliseconds)
0
if there are no jobs left to execute
requestingWindow
- the window from which the request comes
target
- the name of the target window
request
- the request to perform
checkHash
- if true check for hashChenage
forceLoad
- if true always load the request even if there is already the same in the queue
forceAttachmentWithFilename
- if not
null
the AttachmentHandler isAttachment() method is not called,
the response has to be handled as attachment in any case
description
- information about the origin of the request. Useful for debugging.
public void loadDownloadedResponses() throws FailingHttpStatusCodeException, IOException
IOException
- in case of exception
FailingHttpStatusCodeException
- in case of exception
public WebClientOptions getOptions()
public StorageHolder getStorageHolder()
Experimental API: May be changed in next release!
public Set<Cookie> getCookies(URL url)
url
- the URL on which to filter the returned cookies
cookieString
- the string to parse
pageUrl
- the url of the page that likes to set the cookie
origin
- the requester
public boolean isJavaScriptEnabled()
WebClientOptions.setJavaScriptEnabled(boolean)
setter.
isJavaScriptEngineEnabled()
,
WebClientOptions.isJavaScriptEnabled()
public boolean isJavaScriptEngineEnabled()
WebClient(BrowserVersion, boolean, String, int)
constructor.
public HtmlPage loadHtmlCodeIntoCurrentWindow(String htmlCode) throws IOException
htmlCode
- the html code as string
IOException
- in case of error
public XHtmlPage loadXHtmlCodeIntoCurrentWindow(String xhtmlCode) throws IOException
xhtmlCode
- the xhtml code as string
IOException
- in case of error
public WebClient.PooledCSS3Parser getCSS3Parser()
发怒的弓箭 · 517、518路公交下周开通_合肥市瑶海区人民政府 2 周前 |
无邪的圣诞树 · 凤于九天风弄完本 - 百度 2 周前 |