📄 webclient.java
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: WebClient.java,v 1.62 2004/09/29 17:15:24 russgold Exp $** Copyright (c) 2000-2004, Russell Gold** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including without limitation* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and* to permit persons to whom the Software is furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or substantial portions* of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER* DEALINGS IN THE SOFTWARE.********************************************************************************************************************/import java.io.IOException;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.*;import org.xml.sax.SAXException;import com.meterware.httpunit.cookies.CookieJar;/** * The context for a series of web requests. This class manages cookies used to maintain * session context, computes relative URLs, and generally emulates the browser behavior * needed to build an automated test of a web site. * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> * @author Jan Ohrstrom * @author Seth Ladd * @author Oliver Imbusch **/abstractpublic class WebClient { private ArrayList _openWindows = new ArrayList(); /** The current main window. **/ private WebWindow _mainWindow = new WebWindow( this ); private String _authorizationString; private String _proxyAuthorizationString; public WebWindow getMainWindow() { return _mainWindow; } public void setMainWindow( WebWindow mainWindow ) { if (!_openWindows.contains( mainWindow )) throw new IllegalArgumentException( "May only select an open window owned by this client" ); _mainWindow = mainWindow; } public WebWindow[] getOpenWindows() { return (WebWindow[]) _openWindows.toArray( new WebWindow[ _openWindows.size() ] ); } public WebWindow getOpenWindow( String name ) { if (name == null || name.length() == 0) return null; for (Iterator i = _openWindows.iterator(); i.hasNext();) { WebWindow window = (WebWindow) i.next(); if (name.equals( window.getName() )) return window; } return null; } /** * Submits a GET method request and returns a response. * @exception SAXException thrown if there is an error parsing the retrieved page **/ public WebResponse getResponse( String urlString ) throws MalformedURLException, IOException, SAXException { return _mainWindow.getResponse( urlString ); } /** * Submits a web request and returns a response. This is an alternate name for the getResponse method. */ public WebResponse sendRequest( WebRequest request ) throws MalformedURLException, IOException, SAXException { return _mainWindow.sendRequest( request ); } /** * Returns the response representing the current top page in the main window. */ public WebResponse getCurrentPage() { return _mainWindow.getCurrentPage(); } /** * Submits a web request and returns a response, using all state developed so far as stored in * cookies as requested by the server. * @exception SAXException thrown if there is an error parsing the retrieved page **/ public WebResponse getResponse( WebRequest request ) throws MalformedURLException, IOException, SAXException { return _mainWindow.getResponse( request ); } /** * Returns the name of the currently active frames in the main window. **/ public String[] getFrameNames() { return _mainWindow.getFrameNames(); } /** * Returns the response associated with the specified frame name in the main window. * Throws a runtime exception if no matching frame is defined. **/ public WebResponse getFrameContents( String frameName ) { return _mainWindow.getFrameContents( frameName ); } /** * Returns the response associated with the specified frame name in the main window. * Throws a runtime exception if no matching frame is defined. * * @since 1.6 **/ public WebResponse getFrameContents( FrameSelector targetFrame ) { return _mainWindow.getFrameContents( targetFrame ); } /** * Returns the resource specified by the request. Does not update the client or load included framesets or scripts. * May return null if the resource is a JavaScript URL which would normally leave the client unchanged. */ public WebResponse getResource( WebRequest request ) throws IOException { return _mainWindow.getResource( request ); } /** * Resets the state of this client, removing all cookies, frames, and per-client headers. This does not affect * any listeners or preferences which may have been set. **/ public void clearContents() { _mainWindow = new WebWindow( this ); _cookieJar.clear(); _headers = new HeaderDictionary(); } /** * Defines a cookie to be sent to the server on every request. * @deprecated as of 1.6, use #putCookie instead. **/ public void addCookie( String name, String value ) { _cookieJar.addCookie( name, value ); } /** * Defines a cookie to be sent to the server on every request. This overrides any previous setting for this cookie name. **/ public void putCookie( String name, String value ) { _cookieJar.putCookie( name, value ); } /** * Returns the name of all the active cookies which will be sent to the server. **/ public String[] getCookieNames() { return _cookieJar.getCookieNames(); } /** * Returns the value of the specified cookie. **/ public String getCookieValue( String name ) { return _cookieJar.getCookieValue( name ); } /** * Returns the properties associated with this client. */ public ClientProperties getClientProperties() { return _clientProperties; } /** * Specifies the user agent identification. Used to trigger browser-specific server behavior. * @deprecated as of 1.4.6. Use ClientProperties#setUserAgent instead. **/ public void setUserAgent( String userAgent ) { getClientProperties().setUserAgent( userAgent ); } /** * Returns the current user agent setting. * @deprecated as of 1.4.6. Use ClientProperties#getUserAgent instead. **/ public String getUserAgent() { return getClientProperties().getUserAgent(); } /** * Sets a username and password for a basic authentication scheme. **/ public void setAuthorization( String userName, String password ) { _authorizationString = "Basic " + Base64.encode( userName + ':' + password ); } /** * Specifies a proxy server to use for requests from this client. */ public void setProxyServer( String proxyHost, int proxyPort ) { } /** * Specifies a proxy server to use, along with a user and password for authentication. * * @since 1.6 */ public void setProxyServer( String proxyHost, int proxyPort, String userName, String password ) { setProxyServer( proxyHost, proxyPort ); _proxyAuthorizationString = "Basic " + Base64.encode( userName + ':' + password ); } /** * Clears the proxy server settings. */ public void clearProxyServer() { } /** * Returns the name of the active proxy server. */ public String getProxyHost() { return System.getProperty( "proxyHost" ); } /** * Returns the number of the active proxy port, or 0 is none is specified. */ public int getProxyPort() { try { return Integer.parseInt( System.getProperty( "proxyPort" ) ); } catch (NumberFormatException e) { return 0; } } /** * Sets the value for a header field to be sent with all requests. If the value set is null, * removes the header from those to be sent. **/ public void setHeaderField( String fieldName, String fieldValue ) { _headers.put( fieldName, fieldValue ); } /** * Returns the value for the header field with the specified name. This method will ignore the case of the field name. */ public String getHeaderField( String fieldName ) { return (String) _headers.get( fieldName ); } /** * Specifies whether an exception will be thrown when an error status (4xx or 5xx) is detected on a response. * Defaults to the value returned by HttpUnitOptions.getExceptionsThrownOnErrorStatus. **/ public void setExceptionsThrownOnErrorStatus( boolean throwExceptions ) { _exceptionsThrownOnErrorStatus = throwExceptions; } /** * Returns true if an exception will be thrown when an error status (4xx or 5xx) is detected on a response. **/ public boolean getExceptionsThrownOnErrorStatus() { return _exceptionsThrownOnErrorStatus; } /** * Adds a listener to watch for requests and responses. */ public void addClientListener( WebClientListener listener ) { synchronized (_clientListeners) { if (listener != null && !_clientListeners.contains( listener )) _clientListeners.add( listener ); } } /** * Removes a listener to watch for requests and responses. */ public void removeClientListener( WebClientListener listener ) { synchronized (_clientListeners) { _clientListeners.remove( listener ); } } /** * Adds a listener to watch for window openings and closings. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -