📄 webresponse.java
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: WebResponse.java,v 1.140 2004/12/02 03:43:20 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 com.meterware.httpunit.scripting.ScriptableDelegate;import com.meterware.httpunit.scripting.NamedDelegate;import com.meterware.httpunit.cookies.CookieJar;import com.meterware.httpunit.cookies.CookieSource;import java.io.*;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.net.MalformedURLException;import java.util.Hashtable;import java.util.Vector;import java.util.zip.GZIPInputStream;import org.w3c.dom.Document;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * A response to a web request from a web server. * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> * @author <a href="mailto:DREW.VARNER@oracle.com">Drew Varner</a> * @author <a href="mailto:dglo@ssec.wisc.edu">Dave Glowacki</a> * @author <a href="mailto:bx@bigfoot.com">Benoit Xhenseval</a> **/abstractpublic class WebResponse implements HTMLSegment, CookieSource { private static final String HTML_CONTENT = "text/html"; private static final String XHTML_CONTENT = "application/xhtml+xml"; private static final String FAUX_XHTML_CONTENT = "text/xhtml"; private static final int UNINITIALIZED_INT = -2; private static final int UNKNOWN_LENGTH_TIMEOUT = 500; private static final int UNKNOWN_LENGTH_RETRY_INTERVAL = 10; private FrameSelector _frame; private String _baseTarget; private String _refreshHeader; private boolean _hasSubframes; private URL _baseURL; private boolean _parsingPage; /** * Returns a web response built from a URL connection. Provided to allow * access to WebResponse parsing without using a WebClient. **/ public static WebResponse newResponse( URLConnection connection ) throws IOException { return new HttpWebResponse( null, FrameSelector.TOP_FRAME, connection.getURL(), connection, HttpUnitOptions.getExceptionsThrownOnErrorStatus() ); } /** * Returns true if the response is HTML. **/ public boolean isHTML() { return getContentType().equalsIgnoreCase( HTML_CONTENT ) || getContentType().equalsIgnoreCase( FAUX_XHTML_CONTENT ) || getContentType().equalsIgnoreCase( XHTML_CONTENT ); } /** * Returns the URL which invoked this response. **/ public URL getURL() { return _pageURL; } /** * Returns the title of the page. * @exception SAXException thrown if there is an error parsing this response **/ public String getTitle() throws SAXException { return getReceivedPage().getTitle(); } /** * Returns the stylesheet linked in the head of the page. * <code> * <link type="text/css" rel="stylesheet" href="/mystyle.css" /> * </code> * will return "/mystyle.css". * @exception SAXException thrown if there is an error parsing this response **/ public String getExternalStyleSheet() throws SAXException { return getReceivedPage().getExternalStyleSheet(); } /** * Retrieves the "content" of the meta tags for a key pair attribute-attributeValue. * <code> * <meta name="robots" content="index" /> * <meta name="robots" content="follow" /> * <meta http-equiv="Expires" content="now" /> * </code> * this can be used like this * <code> * getMetaTagContent("name","robots") will return { "index","follow" } * getMetaTagContent("http-equiv","Expires") will return { "now" } * </code> * @exception SAXException thrown if there is an error parsing this response **/ public String[] getMetaTagContent(String attribute, String attributeValue) throws SAXException { return getReceivedPage().getMetaTagContent(attribute, attributeValue); } /** * Returns the name of the frame containing this page. **/ public String getFrameName() { return _frame.getName(); } void setFrame( FrameSelector frame ) { if (!_frame.getName().equals( frame.getName())) throw new IllegalArgumentException( "May not modify the frame name" ); _frame = frame; } /** * Returns the frame containing this page. */ FrameSelector getFrame() { return _frame; } /** * Returns a request to refresh this page, if any. This request will be defined * by a <meta> tag in the header. If no tag exists, will return null. **/ public WebRequest getRefreshRequest() { readRefreshRequest(); return _refreshRequest; } /** * Returns the delay before normally following the request to refresh this page, if any. * This request will be defined by a <meta> tag in the header. If no tag exists, * will return zero. **/ public int getRefreshDelay() { readRefreshRequest(); return _refreshDelay; } /** * Returns the response code associated with this response. **/ abstract public int getResponseCode(); /** * Returns the response message associated with this response. **/ abstract public String getResponseMessage(); /** * Returns the content length of this response. * @return the content length, if known, or -1. */ public int getContentLength() { if (_contentLength == UNINITIALIZED_INT) { String length = getHeaderField( "Content-Length" ); _contentLength = (length == null) ? -1 : Integer.parseInt( length ); } return _contentLength; } /** * Returns the content type of this response. **/ public String getContentType() { if (_contentType == null) readContentTypeHeader(); return _contentType; } /** * Returns the character set used in this response. **/ public String getCharacterSet() { if (_characterSet == null) { readContentTypeHeader(); if (_characterSet == null) setCharacterSet( getHeaderField( "Charset" ) ); if (_characterSet == null) setCharacterSet( HttpUnitOptions.getDefaultCharacterSet() ); } return _characterSet; } /** * Returns a list of new cookie names defined as part of this response. **/ public String[] getNewCookieNames() { return getCookieJar().getCookieNames(); } /** * Returns the new cookie value defined as part of this response. **/ public String getNewCookieValue( String name ) { return getCookieJar().getCookieValue( name ); } /** * Returns the names of the header fields found in the response. **/ abstract public String[] getHeaderFieldNames(); /** * Returns the value for the specified header field. If no such field is defined, will return null. * If more than one header is defined for the specified name, returns only the first found. **/ abstract public String getHeaderField( String fieldName ); /** * Returns the text of the response (excluding headers) as a string. Use this method in preference to 'toString' * which may be used to represent internal state of this object. **/ public String getText() throws IOException { if (_responseText == null) loadResponseText(); return _responseText; } /** * Returns a buffered input stream for reading the contents of this reply. **/ public InputStream getInputStream() throws IOException { if (_inputStream == null) _inputStream = new ByteArrayInputStream( getText().getBytes() ); return _inputStream; } /** * Returns the names of the frames found in the page in the order in which they appear. * @exception SAXException thrown if there is an error parsing this response **/ public String[] getFrameNames() throws SAXException { WebFrame[] frames = getFrames(); String[] result = new String[ frames.length ]; for (int i = 0; i < result.length; i++) { result[i] = frames[i].getFrameName(); } return result; } /** * Returns the frames found in the page in the order in which they appear. * @exception SAXException thrown if there is an error parsing this response **/ FrameSelector[] getFrameSelectors() throws SAXException { WebFrame[] frames = getFrames(); FrameSelector[] result = new FrameSelector[ frames.length ]; for (int i = 0; i < result.length; i++) { result[i] = frames[i].getSelector(); } return result; } /** * Returns the contents of the specified subframe of this frameset response. * * @param subFrameName the name of the desired frame as defined in the frameset. **/ public WebResponse getSubframeContents( String subFrameName ) { if (_window == null) throw new NoSuchFrameException( subFrameName ); return _window.getSubframeContents( _frame, subFrameName ); }//---------------------- HTMLSegment methods ----------------------------- /** * Returns the HTMLElement with the specified ID. * @throws SAXException thrown if there is an error parsing the response. */ public HTMLElement getElementWithID( String id ) throws SAXException { return getReceivedPage().getElementWithID( id ); } /** * Returns a list of HTML element names contained in this HTML section. */ public String[] getElementNames() throws SAXException { return getReceivedPage().getElementNames(); } /** * Returns the HTMLElements found in this segment with the specified name. */ public HTMLElement[] getElementsWithName( String name ) throws SAXException { return getReceivedPage().getElementsWithName( name ); } /** * Returns the HTMLElements found with the specified attribute value. * @since 1.6 */ public HTMLElement[] getElementsWithAttribute( String name, String value ) throws SAXException { return getReceivedPage().getElementsWithAttribute( name, value ); } /** * Returns the forms found in the page in the order in which they appear. * @exception SAXException thrown if there is an error parsing the response. **/ public WebForm[] getForms() throws SAXException { return getReceivedPage().getForms(); } /** * Returns the form found in the page with the specified name. * @exception SAXException thrown if there is an error parsing the response. **/ public WebForm getFormWithName( String name ) throws SAXException { return getReceivedPage().getFormWithName( name ); } /** * Returns the form found in the page with the specified ID. * @exception SAXException thrown if there is an error parsing the response. **/ public WebForm getFormWithID( String ID ) throws SAXException { return getReceivedPage().getFormWithID( ID ); } /** * Returns the first form found in the page matching the specified criteria. * @exception SAXException thrown if there is an error parsing the response. **/ public WebForm getFirstMatchingForm( HTMLElementPredicate predicate, Object criteria ) throws SAXException { return getReceivedPage().getFirstMatchingForm( predicate, criteria ); } /** * Returns all forms found in the page matching the specified criteria. * @exception SAXException thrown if there is an error parsing the response. **/ public WebForm[] getMatchingForms( HTMLElementPredicate predicate, Object criteria ) throws SAXException { return getReceivedPage().getMatchingForms( predicate, criteria ); } /** * Returns the links found in the page in the order in which they appear. * @exception SAXException thrown if there is an error parsing the response. **/ public WebLink[] getLinks() throws SAXException { return getReceivedPage().getLinks(); } /** * Returns the first link which contains the specified text. * @exception SAXException thrown if there is an error parsing the response. **/ public WebLink getLinkWith( String text ) throws SAXException { return getReceivedPage().getLinkWith( text ); } /** * Returns the first link which contains an image with the specified text as its 'alt' attribute. * @exception SAXException thrown if there is an error parsing the response. **/ public WebLink getLinkWithImageText( String text ) throws SAXException { return getReceivedPage().getLinkWithImageText( text ); } /** * Returns the link found in the page with the specified name. * @exception SAXException thrown if there is an error parsing the response. **/ public WebLink getLinkWithName( String name ) throws SAXException { return getReceivedPage().getLinkWithName( name ); } /** * Returns the link found in the page with the specified ID. * @exception SAXException thrown if there is an error parsing the response. **/ public WebLink getLinkWithID( String ID ) throws SAXException { return getReceivedPage().getLinkWithID( ID ); } /** * Returns the first link found in the page matching the specified criteria. * @exception SAXException thrown if there is an error parsing the response. **/ public WebLink getFirstMatchingLink( HTMLElementPredicate predicate, Object criteria ) throws SAXException { return getReceivedPage().getFirstMatchingLink( predicate, criteria );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -