⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webrequest.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * Constructs a web request using a base URL, a relative URL string, and a target.     **/    protected WebRequest( URL urlBase, String urlString, String target ) {        this( urlBase, urlString, FrameSelector.TOP_FRAME, target );    }    /**     * Constructs a web request using a base URL, a relative URL string, and a target.     **/    protected WebRequest( URL urlBase, String urlString, FrameSelector frame, String target ) {        this( urlBase, urlString, frame, target, new UncheckedParameterHolder() );    }    /**     * Constructs a web request from a form.     **/    protected WebRequest( WebForm sourceForm, ParameterHolder parameterHolder, SubmitButton button, int x, int y ) {        this( sourceForm, parameterHolder );        if (button != null && button.isImageButton() && button.getName().length() > 0) {            _button = button;            _parameterHolder.selectImageButtonPosition( _button, x, y );        }    }    protected WebRequest( WebRequestSource requestSource, ParameterHolder parameterHolder ) {        this( requestSource.getBaseURL(), requestSource.getRelativePage(), requestSource.getFrame(), requestSource.getTarget(), parameterHolder );        _webRequestSource = requestSource;        setHeaderField( REFERER_HEADER_NAME, requestSource.getBaseURL().toExternalForm() );    }    static ParameterHolder newParameterHolder( WebRequestSource requestSource ) {        if (HttpUnitOptions.getParameterValuesValidated()) {            return requestSource;        } else {            return new UncheckedParameterHolder( requestSource );        }    }    /**     * Constructs a web request using a base URL, a relative URL string, and a target.     **/    private WebRequest( URL urlBase, String urlString, FrameSelector sourceFrame, String requestTarget, ParameterHolder parameterHolder ) {        _urlBase   = urlBase;        _sourceFrame = sourceFrame;        _requestTarget = requestTarget;        _urlString = urlString.toLowerCase().startsWith( "http" ) ? escape( urlString ) : urlString;        _parameterHolder = parameterHolder;    }    private static String escape( String urlString ) {        if (urlString.indexOf( ' ' ) < 0) return urlString;        StringBuffer sb = new StringBuffer();        int start = 0;        do {            int index = urlString.indexOf( ' ', start );            if (index < 0) {                sb.append( urlString.substring( start ) );                break;            } else {                sb.append( urlString.substring( start, index ) ).append( "%20" );                start = index+1;            }        } while (true);        return sb.toString();    }    /**     * Returns true if selectFile may be called with this parameter.     */    protected boolean maySelectFile( String parameterName )    {        return isFileParameter( parameterName );    }    /**     * Selects whether MIME-encoding will be used for this request. MIME-encoding changes the way the request is sent     * and is required for requests which include file parameters. This method may only be called for a POST request     * which was not created from a form.     **/    protected void setMimeEncoded( boolean mimeEncoded )    {        _parameterHolder.setSubmitAsMime( mimeEncoded );    }    /**     * Returns true if this request is to be MIME-encoded.     **/    protected boolean isMimeEncoded() {        return _parameterHolder.isSubmitAsMime();    }    /**     * Returns the content type of this request. If null, no content is specified.     */    protected String getContentType() {        return null;    }    /**     * Returns the character set required for this request.     **/    final    protected String getCharacterSet() {        return _parameterHolder.getCharacterSet();    }    /**     * Performs any additional processing necessary to complete the request.     **/    protected void completeRequest( URLConnection connection ) throws IOException {        if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection).setRequestMethod( getMethod() );    }    /**     * Writes the contents of the message body to the specified stream.     */    protected void writeMessageBody( OutputStream stream ) throws IOException {    }    final    protected URL getURLBase() {        return _urlBase;    }//------------------------------------- protected members ---------------------------------------------    protected String getURLString() {        final String queryString = getQueryString();        if (queryString.length() == 0) {            return _urlString;        } else {            return _urlString + "?" + queryString;        }    }    final    protected ParameterHolder getParameterHolder() {        return _parameterHolder;    }//---------------------------------- package members --------------------------------    /** The target indicating the topmost frame of a window. **/    static final String TOP_FRAME = "_top";    /** The target indicating the parent of a frame. **/    static final String PARENT_FRAME = "_parent";    /** The target indicating a new, empty window. **/    static final String NEW_WINDOW = "_blank";    /** The target indicating the same frame. **/    static final String SAME_FRAME = "_self";    WebRequestSource getWebRequestSource() {        return _webRequestSource;    }    Hashtable getHeaderDictionary() {        if (_headers == null) {            _headers = new Hashtable();            if (getContentType() != null) _headers.put( "Content-Type", getContentType() );        }        return _headers;    }    String getReferer() {        return _headers == null ? null : (String) _headers.get( REFERER_HEADER_NAME );    }}class URLEncodedString implements ParameterProcessor {    private StringBuffer _buffer = new StringBuffer( HttpUnitUtils.DEFAULT_BUFFER_SIZE );    private boolean _haveParameters = false;    public String getString() {        return _buffer.toString();    }    public void addParameter( String name, String value, String characterSet ) {        if (_haveParameters) _buffer.append( '&' );        _buffer.append( encode( name, characterSet ) );        if (value != null) _buffer.append( '=' ).append( encode( value, characterSet ) );        _haveParameters = true;    }    public void addFile( String parameterName, UploadFileSpec fileSpec ) {        throw new RuntimeException( "May not URL-encode a file upload request" );    }    /**     * Returns a URL-encoded version of the string, including all eight bits, unlike URLEncoder, which strips the high bit.     **/    private String encode( String source, String characterSet ) {        if (characterSet.equalsIgnoreCase( HttpUnitUtils.DEFAULT_CHARACTER_SET )) {            return URLEncoder.encode( source );        } else {            try {                byte[] rawBytes = source.getBytes( characterSet );                StringBuffer result = new StringBuffer( 3*rawBytes.length );                for (int i = 0; i < rawBytes.length; i++) {                    int candidate = rawBytes[i] & 0xff;                    if (candidate == ' ') {                        result.append( '+' );                    } else if ((candidate >= 'A' && candidate <= 'Z') ||                               (candidate >= 'a' && candidate <= 'z') ||                               (candidate == '.') || (candidate == '-' ) ||                               (candidate == '*') || (candidate == '_') ||                               (candidate >= '0' && candidate <= '9')) {                        result.append( (char) rawBytes[i] );                    } else if (candidate < 16) {                        result.append( "%0" ).append( Integer.toHexString( candidate ).toUpperCase() );                    } else {                        result.append( '%' ).append( Integer.toHexString( candidate ).toUpperCase() );                    }                }                return result.toString();            } catch (java.io.UnsupportedEncodingException e) {                return "???";    // XXX should pass the exception through as IOException ultimately            }        }    }}//======================================== class JavaScriptURLStreamHandler ============================================class JavascriptURLStreamHandler extends URLStreamHandler {    protected URLConnection openConnection( URL u ) throws IOException {        return null;    }}//======================================== class HttpsURLStreamHandler ============================================class HttpsURLStreamHandler extends URLStreamHandler {    protected URLConnection openConnection( URL u ) throws IOException {        throw new RuntimeException( "https support requires the Java Secure Sockets Extension. See http://java.sun.com/products/jsse" );    }}//============================= exception class IllegalNonFileParameterException ======================================/** * This exception is thrown on an attempt to set a non-file parameter to a file value. **/class IllegalNonFileParameterException extends IllegalRequestParameterException {    IllegalNonFileParameterException( String parameterName ) {        _parameterName = parameterName;    }    public String getMessage() {        return "Parameter '" + _parameterName + "' is not a file parameter and may not be set to a file value.";    }    private String _parameterName;}//============================= exception class MultipartFormRequiredException ======================================/** * This exception is thrown on an attempt to set a file parameter in a form that does not specify MIME encoding. **/class MultipartFormRequiredException extends IllegalRequestParameterException {    MultipartFormRequiredException() {    }    public String getMessage() {        return "The request does not use multipart/form-data encoding, and cannot be used to upload files ";    }}//============================= exception class IllegalButtonPositionException ======================================/** * This exception is thrown on an attempt to set a file parameter in a form that does not specify MIME encoding. **/class IllegalButtonPositionException extends IllegalRequestParameterException {    IllegalButtonPositionException() {    }    public String getMessage() {        return "The request was not created with an image button, and cannot accept an image button click position";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -