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

📄 httpurl.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/** * * @(#)HttpUrl.java	1.4 01/08/10 * * Copyright 2001 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. */package com.sun.midp.io;import java.io.IOException;/** * A parsed HTTP (or subclass of) URL. Based on RFC 2396. * <p> * Handles IPv6 hosts, check host[0] for a "[". * Can be used for relative URL's that do not have authorities. * Can be used for FTP URL's that do not have the username and passwords. * <p> * Any elements not specified are represented by null, except a * non-specified port, which is represented by a -1. */public class HttpUrl {    /** Scheme of the URL or null. */    public String scheme;    /** Authority (host [port]) of the URL. */    public String authority;    /** Path of the URL or null. */    public String path;    /** Query of the URL or null. */    public String query;    /** Fragment of the URL or null. */    public String fragment;    /** hHst of the authority or null. */    public String host;    /** Port of the authority or -1 for not specified. */    public int port = -1;    /** Machine of the host or null. */    public String machine;    /** Domain of the host or null. */    public String domain;    /**     * Construct a HttpUrl.     *     * @param url HTTP URL to parse     *     * @exception IOException if the port is not numeric     */    public HttpUrl(String url) throws IOException {        int endOfScheme = url.indexOf(':');        // ":" marks the scheme in a absolute URL which has a "//"         if (endOfScheme < url.indexOf('/')) {            scheme = url.substring(0, endOfScheme);            endOfScheme++;        } else if (endOfScheme != -1 && endOfScheme == url.length() - 1) {            // URL has only a scheme            scheme = url.substring(0, endOfScheme);            return;        } else {            endOfScheme = 0;        }        parseAfterScheme(url, endOfScheme);    }    /**     * Construct a HttpUrl from a scheme and partial HTTP URL.     *     * @param theScheme  the protocol component of an http URL     * @param partialUrl HTTP URL to parse     *     * @exception IOException if the port is not numeric     */    public HttpUrl(String theScheme, String partialUrl) throws IOException {        scheme = theScheme;        parseAfterScheme(partialUrl, 0);    }    /**     * Parse the part of the HTTP URL after the scheme.     *     * @param url the part of the HTTP URL after the ":" of the scheme     * @param afterScheme index of the first char after the scheme     *     * @exception IOException if the port is not numeric     */    private void parseAfterScheme(String url, int afterScheme)            throws IOException {        int start;        int startOfAuthority;        int endOfUrl;        int endOfAuthority;        int endOfPath;        int endOfQuery;        int endOfHost;        int startOfPort;        int endOfPort;        int startOfDomain;        endOfUrl = url.length();        endOfAuthority = endOfUrl;        endOfPath = endOfUrl;        endOfQuery = endOfUrl;        if (url.startsWith("//", afterScheme)) {            // do not include the "//"            startOfAuthority = afterScheme + 2;        } else {            // no authority, the path starts at 0 and may not begin with a "/"            startOfAuthority = afterScheme;        }        /*         * Since all of the elements after the authority are optional         * and they can contain the delimiter of the element before it.         * Work backwards since we know the end of the last item and will         * know the end of the next item when find the start of the current         * item.         */        start = url.indexOf('#', startOfAuthority);        if (start != -1) {            endOfAuthority = start;            endOfPath = start;            endOfQuery = start;            // do not parse an empty fragment            if (start + 2 < endOfUrl) {                // do not include the "#"                fragment = url.substring(start + 1, endOfUrl);            }        }        start = url.indexOf('?', startOfAuthority);        if (start != -1 && start < endOfQuery) {            endOfAuthority = start;            endOfPath = start;            // do not parse an empty query            if (start + 2 < endOfQuery) {                // do not include the "?"                query = url.substring(start + 1, endOfQuery);            }        }        if (startOfAuthority == afterScheme) {            // no authority, the path starts after scheme            start = afterScheme;        } else {            // this is not relative URL so the path must begin with "/"            start = url.indexOf('/', startOfAuthority);        }        if (start != -1 && start < endOfPath) {            endOfAuthority = start;            // do not parse an empty path            if (start + 2 < endOfPath) {                path = url.substring(start, endOfPath);            }        }        if (startOfAuthority >= endOfAuthority) {            return;        }        authority = url.substring(startOfAuthority, endOfAuthority);        endOfPort = authority.length();        // get the port first, to find the end of the host        startOfPort = authority.lastIndexOf(':');        // IPv6 address have brackets around them and can have ":"'s        if (startOfPort != -1 && startOfPort > authority.indexOf(']')) {            endOfHost = startOfPort;            // do not try parse an empty port            if (startOfPort + 2 < endOfPort) {                try {                    // do not include the ":"                    port = Integer.parseInt(authority.substring(                                            startOfPort + 1,                                            endOfPort));                } catch (NumberFormatException nfe) {                    throw new IOException("invalid port");                }            }        } else {            endOfHost = endOfPort;        }        // get the host        host = authority.substring(0, endOfHost);        // find the machine and domain, if not host is not an IP address        if (Character.isDigit(host.charAt(0)) || host.charAt(0) == '[') {            return;        }        startOfDomain = host.indexOf('.');        if (startOfDomain != -1) {            // do not include the "."            domain = host.substring(startOfDomain + 1, host.length());            machine = host.substring(0, startOfDomain);        } else {            machine = host;        }    }    /**     * Adds a base URL to this URL if this URL is a relative one.     * Afterwards this URL will be an absolute URL.     *     * @param baseUrl an absolute URL     *     * @exception IOException if the port of base URL is not numeric     */    public void addBaseUrl(String baseUrl) throws IOException {        addBaseUrl(new HttpUrl(baseUrl));    }    /**     * Adds a base URL to this URL if this URL is a relative one.     * Afterwards this URL will be an absolute URL.     *     * @param baseUrl a parsed absolute URL     */    public void addBaseUrl(HttpUrl baseUrl) {        String basePath;        if (authority != null) {            return;        }        scheme = baseUrl.scheme;        authority = baseUrl.authority;        if (path == null) {            path = baseUrl.path;            return;        }        if (path.charAt(0) == '/' || baseUrl.path == null ||               baseUrl.path.charAt(0) != '/') {            return;        }        // find the base path        basePath = baseUrl.path.substring(0, baseUrl.path.lastIndexOf('/'));        path = basePath + '/' + path;    }    /**     * Converts this URL into a string.     *     * @return string representation of this URL     */    public String toString() {        StringBuffer url = new StringBuffer();        if (scheme != null) {            url.append(scheme);            url.append(':');        }        if (authority != null) {            url.append('/');            url.append('/');            url.append(authority);        }        if (path != null) {            url.append(path);        }        if (query != null) {            url.append('?');            url.append(path);        }        if (fragment != null) {            url.append('#');            url.append(fragment);        }        return url.toString();    }}

⌨️ 快捷键说明

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