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

📄 httpsender.java

📁 Java有关XML编程需要用到axis 的源代码 把里面bin下的包导入相应的Java工程 进行使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        for (ByteArrayOutputStream buf = new ByteArrayOutputStream(4097); ;) {            if (!readTooMuch) {                b = (byte) inp.read();            }            if (b == -1) {                break;            }            readTooMuch = false;            if ((b != '\r') && (b != '\n')) {                if ((b == ':') && (colonIndex == -1)) {                    colonIndex = len;                }                len++;                buf.write(b);            } else if (b == '\r') {                continue;            } else {    // b== '\n'                if (len == 0) {                    break;                }                b = (byte) inp.read();                readTooMuch = true;                // A space or tab at the begining of a line means the header continues.                if ((b == ' ') || (b == '\t')) {                    continue;                }                buf.close();                byte[] hdata = buf.toByteArray();                buf.reset();                if (colonIndex != -1) {                    name =                            new String(hdata, 0, colonIndex,                                    HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);                    value =                            new String(hdata, colonIndex + 1, len - 1 - colonIndex,                                    HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);                    colonIndex = -1;                } else {                    name =                            new String(hdata, 0, len,                                    HTTPConstants.HEADER_DEFAULT_CHAR_ENCODING);                    value = "";                }                if (log.isDebugEnabled()) {                    log.debug(name + value);                }                if (msgContext.getProperty(HTTPConstants.MC_HTTP_STATUS_CODE)                        == null) {                    // Reader status code                    int start = name.indexOf(' ') + 1;                    String tmp = name.substring(start).trim();                    int end = tmp.indexOf(' ');                    if (end != -1) {                        tmp = tmp.substring(0, end);                    }                    returnCode = Integer.parseInt(tmp);                    msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE,                            new Integer(returnCode));                    msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_MESSAGE,                            name.substring(start + end + 1));                } else {                    // if we are maintaining session state,                    // handle cookies (if any)                    if (msgContext.getMaintainSession()) {                        final String nameLowerCase = name.toLowerCase();                        if (nameLowerCase.equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE)) {                            handleCookie(HTTPConstants.HEADER_COOKIE, null, value, msgContext);                        } else if (nameLowerCase.equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE2)) {                            handleCookie(HTTPConstants.HEADER_COOKIE2, null, value, msgContext);                        } else {                            headers.put(name.toLowerCase(), value);                        }                    } else {                        headers.put(name.toLowerCase(), value);                    }                }                len = 0;            }        }        return inp;    }    /**     * Reads the SOAP response back from the server     *     * @param msgContext message context     *     * @throws IOException     */    private InputStream readFromSocket(SocketHolder socketHolder,                                       MessageContext msgContext,                                       InputStream inp,                                       Hashtable headers)            throws IOException {        Message outMsg = null;        byte b;        Integer rc = (Integer)msgContext.getProperty(                                            HTTPConstants.MC_HTTP_STATUS_CODE);        int returnCode = 0;        if (rc != null) {            returnCode = rc.intValue();        } else {            // No return code?? Should have one by now.        }        /* All HTTP headers have been read. */        String contentType = (String) headers.get(HEADER_CONTENT_TYPE_LC);                                contentType = (null == contentType)                ? null                : contentType.trim();                        String location = (String) headers.get(HEADER_LOCATION_LC);        location = (null == location)                ? null                : location.trim();                        if ((returnCode > 199) && (returnCode < 300)) {            if (returnCode == 202) {                return inp;            }            // SOAP return is OK - so fall through        } else if (msgContext.getSOAPConstants() ==                SOAPConstants.SOAP12_CONSTANTS) {            // For now, if we're SOAP 1.2, fall through, since the range of            // valid result codes is much greater        } else if ((contentType != null) && !contentType.startsWith("text/html")                && ((returnCode > 499) && (returnCode < 600))) {            // SOAP Fault should be in here - so fall through        } else if ((location != null) &&                ((returnCode == 302) || (returnCode == 307))) {            // Temporary Redirect (HTTP: 302/307)                        // close old connection                inp.close();                socketHolder.getSocket().close();                // remove former result and set new target url                msgContext.removeProperty(HTTPConstants.MC_HTTP_STATUS_CODE);                msgContext.setProperty(MessageContext.TRANS_URL, location);            // next try                invoke(msgContext);                            return inp;        } else if (returnCode == 100) {            msgContext.removeProperty(HTTPConstants.MC_HTTP_STATUS_CODE);            msgContext.removeProperty(HTTPConstants.MC_HTTP_STATUS_MESSAGE);            readHeadersFromSocket(socketHolder, msgContext, inp, headers);            return readFromSocket(socketHolder, msgContext, inp, headers);        } else {            // Unknown return code - so wrap up the content into a            // SOAP Fault.            ByteArrayOutputStream buf = new ByteArrayOutputStream(4097);            while (-1 != (b = (byte) inp.read())) {                buf.write(b);            }            String statusMessage = msgContext.getStrProp(                                        HTTPConstants.MC_HTTP_STATUS_MESSAGE);            AxisFault fault = new AxisFault("HTTP", "(" + returnCode + ")" +                                                    statusMessage, null, null);            fault.setFaultDetailString(Messages.getMessage("return01",                    "" + returnCode, buf.toString()));            fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_HTTPERRORCODE,                    Integer.toString(returnCode));            throw fault;        }        String contentLocation =             (String) headers.get(HEADER_CONTENT_LOCATION_LC);        contentLocation = (null == contentLocation)                ? null                : contentLocation.trim();        String contentLength =             (String) headers.get(HEADER_CONTENT_LENGTH_LC);        contentLength = (null == contentLength)                ? null                : contentLength.trim();        String transferEncoding =            (String) headers.get(HEADER_TRANSFER_ENCODING_LC);        if (null != transferEncoding) {            transferEncoding = transferEncoding.trim().toLowerCase();            if (transferEncoding.equals(                   HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED)) {                inp = new ChunkedInputStream(inp);            }        }        outMsg = new Message( new SocketInputStream(inp, socketHolder.getSocket()), false,                              contentType, contentLocation);        // Transfer HTTP headers of HTTP message to MIME headers of SOAP message        MimeHeaders mimeHeaders = outMsg.getMimeHeaders();        for (Enumeration e = headers.keys(); e.hasMoreElements(); ) {            String key = (String) e.nextElement();            mimeHeaders.addHeader(key, ((String) headers.get(key)).trim());        }                outMsg.setMessageType(Message.RESPONSE);        msgContext.setResponseMessage(outMsg);        if (log.isDebugEnabled()) {            if (null == contentLength) {                log.debug("\n"                          + Messages.getMessage("no00", "Content-Length"));            }            log.debug("\n" + Messages.getMessage("xmlRecd00"));            log.debug("-----------------------------------------------");            log.debug(outMsg.getSOAPEnvelope().toString());        }        return inp;    }    /**     * little helper function for cookies. fills up the message context with     * a string or an array of strings (if there are more than one Set-Cookie)     *     * @param cookieName     * @param setCookieName     * @param cookie     * @param msgContext     */    public void handleCookie(String cookieName, String setCookieName,                             String cookie, MessageContext msgContext) {        cookie = cleanupCookie(cookie);        int keyIndex = cookie.indexOf("=");        String key = (keyIndex != -1) ? cookie.substring(0, keyIndex) : cookie;                ArrayList cookies = new ArrayList();        Object oldCookies = msgContext.getProperty(cookieName);        boolean alreadyExist = false;        if(oldCookies != null) {            if(oldCookies instanceof String[]) {                String[] oldCookiesArray = (String[])oldCookies;                for(int i = 0; i < oldCookiesArray.length; i++) {                    String anOldCookie = oldCookiesArray[i];                    if (key != null && anOldCookie.indexOf(key) == 0) { // same cookie key                        anOldCookie = cookie;             // update to new one                        alreadyExist = true;                    }                    cookies.add(anOldCookie);                }            } else {				String oldCookie = (String)oldCookies;                if (key != null && oldCookie.indexOf(key) == 0) { // same cookie key					oldCookie = cookie;             // update to new one                    alreadyExist = true;                }                cookies.add(oldCookie);            }        }                if (!alreadyExist) {            cookies.add(cookie);        }                if(cookies.size()==1) {            msgContext.setProperty(cookieName, cookies.get(0));        } else if (cookies.size() > 1) {            msgContext.setProperty(cookieName, cookies.toArray(new String[cookies.size()]));        }    }        /**     * cleanup the cookie value.     *     * @param cookie initial cookie value     *     * @return a cleaned up cookie value.     */    private String cleanupCookie(String cookie) {        cookie = cookie.trim();        // chop after first ; a la Apache SOAP (see HTTPUtils.java there)        int index = cookie.indexOf(';');        if (index != -1) {            cookie = cookie.substring(0, index);        }        return cookie;    }}

⌨️ 快捷键说明

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