类org.apache.commons.httpclient.util.EncodingUtil源码实例Demo

下面列出了怎么用org.apache.commons.httpclient.util.EncodingUtil的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: davmail   文件: SpNegoScheme.java
/**
 * Processes the Negotiate challenge.
 *
 * @param challenge the challenge string
 * @throws MalformedChallengeException is thrown if the authentication challenge is malformed
 */
public void processChallenge(final String challenge) throws MalformedChallengeException {
    String authScheme = AuthChallengeParser.extractScheme(challenge);
    if (!authScheme.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid Negotiate challenge: " + challenge);
    }
    int spaceIndex = challenge.indexOf(' ');
    if (spaceIndex != -1) {
        // step 2: received server challenge
        serverToken = Base64.decodeBase64(EncodingUtil.getBytes(
                challenge.substring(spaceIndex).trim(), "ASCII"));
        this.state = TYPE2_MSG_RECEIVED;
    } else {
        this.serverToken = null;
        if (this.state == UNINITIATED) {
            this.state = INITIATED;
        } else {
            this.state = FAILED;
        }
    }
}
 
源代码2 项目: davmail   文件: NTLMv2Scheme.java
/**
 * Processes the NTLM challenge.
 *
 * @param challenge the challenge string
 * @throws MalformedChallengeException is thrown if the authentication challenge
 *                                     is malformed
 */
public void processChallenge(final String challenge) throws MalformedChallengeException {
    String authScheme = AuthChallengeParser.extractScheme(challenge);
    if (!authScheme.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge);
    }
    int spaceIndex = challenge.indexOf(' ');
    if (spaceIndex != -1) {
        try {
            type2Message = new Type2Message(Base64.decodeBase64(EncodingUtil.getBytes(
                    challenge.substring(spaceIndex).trim(), "ASCII")));
        } catch (IOException e) {
            throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge, e);
        }
        this.state = TYPE2_MSG_RECEIVED;
    } else {
        this.type2Message = null;
        if (this.state == UNINITIATED) {
            this.state = INITIATED;
        } else {
            this.state = FAILED;
        }
    }
}
 
源代码3 项目: alfresco-remote-api   文件: HttpResponse.java
public String getResponse()
{
	if (responseBytes != null)
	{
		if (method instanceof HttpMethodBase)
		{
			// mimic method.getResponseBodyAsString
			return EncodingUtil.getString(responseBytes, ((HttpMethodBase)method).getResponseCharSet());
		}
		else
		{
			return new String(responseBytes);
		}
	}
	else
	{
		return null;
	}
}
 
源代码4 项目: knopflerfish.org   文件: PostMethod.java
/**
 * Generates a request entity from the post parameters, if present.  Calls
 * {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set.
 * 
 * @since 3.0
 */
protected RequestEntity generateRequestEntity() {
    if (!this.params.isEmpty()) {
        // Use a ByteArrayRequestEntity instead of a StringRequestEntity.
        // This is to avoid potential encoding issues.  Form url encoded strings
        // are ASCII by definition but the content type may not be.  Treating the content
        // as bytes allows us to keep the current charset without worrying about how
        // this charset will effect the encoding of the form url encoded string.
        String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
        ByteArrayRequestEntity entity = new ByteArrayRequestEntity(
            EncodingUtil.getAsciiBytes(content),
            FORM_URL_ENCODED_CONTENT_TYPE
        );
        return entity;
    } else {
        return super.generateRequestEntity();
    }
}
 
源代码5 项目: knopflerfish.org   文件: DigestScheme.java
/**
 * Creates a random cnonce value based on the current time.
 * 
 * @return The cnonce value as String.
 * @throws HttpClientError if MD5 algorithm is not supported.
 */
public static String createCnonce() {
    LOG.trace("enter DigestScheme.createCnonce()");

    String cnonce;
    final String digAlg = "MD5";
    MessageDigest md5Helper;

    try {
        md5Helper = MessageDigest.getInstance(digAlg);
    } catch (NoSuchAlgorithmException e) {
        throw new HttpClientError(
          "Unsupported algorithm in HTTP Digest authentication: "
           + digAlg);
    }

    cnonce = Long.toString(System.currentTimeMillis());
    cnonce = encode(md5Helper.digest(EncodingUtil.getAsciiBytes(cnonce)));

    return cnonce;
}
 
源代码6 项目: knopflerfish.org   文件: BasicScheme.java
/**
 * Returns a basic <tt>Authorization</tt> header value for the given 
 * {@link UsernamePasswordCredentials} and charset.
 * 
 * @param credentials The credentials to encode.
 * @param charset The charset to use for encoding the credentials
 * 
 * @return a basic authorization string
 * 
 * @since 3.0
 */
public static String authenticate(UsernamePasswordCredentials credentials, String charset) {

    LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)");

    if (credentials == null) {
        throw new IllegalArgumentException("Credentials may not be null"); 
    }
    if (charset == null || charset.length() == 0) {
        throw new IllegalArgumentException("charset may not be null or empty");
    }
    StringBuffer buffer = new StringBuffer();
    buffer.append(credentials.getUserName());
    buffer.append(":");
    buffer.append(credentials.getPassword());
    
    return "Basic " + EncodingUtil.getAsciiString(
            Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
}
 
源代码7 项目: webarchive-commons   文件: LaxHttpParser.java
/**
 * Read up to <tt>"\n"</tt> from an (unchunked) input stream.
 * If the stream ends before the line terminator is found,
 * the last part of the string will still be returned.
 * If no input data available, <code>null</code> is returned.
 *
 * @param inputStream the stream to read from
 * @param charset charset of HTTP protocol elements
 *
 * @throws IOException if an I/O problem occurs
 * @return a line from the stream
 * 
 * @since 3.0
 */
public static String readLine(InputStream inputStream, String charset) throws IOException {
    LOG.trace("enter LaxHttpParser.readLine(InputStream, String)");
    byte[] rawdata = readRawLine(inputStream);
    if (rawdata == null) {
        return null;
    }
    // strip CR and LF from the end
    int len = rawdata.length;
    int offset = 0;
    if (len > 0) {
        if (rawdata[len - 1] == '\n') {
            offset++;
            if (len > 1) {
                if (rawdata[len - 2] == '\r') {
                    offset++;
                }
            }
        }
    }
    return EncodingUtil.getString(rawdata, 0, len - offset, charset);
}
 
源代码8 项目: alfresco-repository   文件: ContentDataPart.java
/**
 * Write the disposition header to the output stream
 * @param out The output stream
 * @throws IOException If an IO problem occurs
 * @see org.apache.commons.httpclient.methods.multipart.Part#sendDispositionHeader(OutputStream)
 */
protected void sendDispositionHeader(OutputStream out) 
throws IOException {
    super.sendDispositionHeader(out);
    if (filename != null) {
        out.write(FILE_NAME_BYTES);
        out.write(QUOTE_BYTES);
        out.write(EncodingUtil.getAsciiBytes(filename));
        out.write(QUOTE_BYTES);
    }
}
 
源代码9 项目: javabase   文件: OldHttpClientApi.java
public void  get2(){
    String requesturl = "http://www.tuling123.com/openapi/api";
    GetMethod getMethod = new GetMethod(requesturl);
    try {
        String APIKEY = "4b441cb500f431adc6cc0cb650b4a5d0";
        String INFO ="北京时间";

        //get  参数
        NameValuePair nvp1=new NameValuePair("key",APIKEY);
        NameValuePair nvp2=new NameValuePair("info",INFO);
        NameValuePair[] nvp = new NameValuePair[2];
        nvp[0]=nvp1;
        nvp[1]=nvp2;
        String params=EncodingUtil.formUrlEncode(nvp, "UTF-8");
        //设置参数
        getMethod.setQueryString(params);

        byte[] responseBody = executeMethod(getMethod,10000);

         String content=new String(responseBody ,"utf-8");
        log.info(content);
    }catch (Exception e){
       log.error(""+e.getLocalizedMessage());
    }finally {
        //结束一定要关闭
        getMethod.releaseConnection();
    }
}
 
源代码10 项目: javabase   文件: HttpClientUtil.java
/**
 * 转换成queryString
 * @param params
 *            存档参数名与参数值的集合
 * @return queryString
 */
public static String getQueryString(Map<String, String> params) {
	String queryString = null;
	if (params != null) {
		NameValuePair[] nvp = new NameValuePair[params.size()];
		int index = 0;
		for(String key : params.keySet()) {
			nvp[index++] = new NameValuePair(key, params.get(key));
		}
		queryString = EncodingUtil.formUrlEncode(nvp, "UTF-8");
	}
	return queryString == null ? "" : queryString;
}
 
源代码11 项目: knopflerfish.org   文件: HttpParser.java
/**
 * Read up to <tt>"\n"</tt> from an (unchunked) input stream.
 * If the stream ends before the line terminator is found,
 * the last part of the string will still be returned.
 * If no input data available, <code>null</code> is returned.
 *
 * @param inputStream the stream to read from
 * @param charset charset of HTTP protocol elements
 *
 * @throws IOException if an I/O problem occurs
 * @return a line from the stream
 * 
 * @since 3.0
 */
public static String readLine(InputStream inputStream, String charset) throws IOException {
    LOG.trace("enter HttpParser.readLine(InputStream, String)");
    byte[] rawdata = readRawLine(inputStream);
    if (rawdata == null) {
        return null;
    }
    // strip CR and LF from the end
    int len = rawdata.length;
    int offset = 0;
    if (len > 0) {
        if (rawdata[len - 1] == '\n') {
            offset++;
            if (len > 1) {
                if (rawdata[len - 2] == '\r') {
                    offset++;
                }
            }
        }
    }
    final String result =
        EncodingUtil.getString(rawdata, 0, len - offset, charset);
    if (Wire.HEADER_WIRE.enabled()) {
        String logoutput = result;
        if (offset == 2)
            logoutput = result + "\r\n";
        else if (offset == 1)
            logoutput = result + "\n";
        Wire.HEADER_WIRE.input(logoutput);
    }
    return result;
}
 
源代码12 项目: knopflerfish.org   文件: MultipartRequestEntity.java
/**
 * Returns the MIME boundary string that is used to demarcate boundaries of
 * this part. The first call to this method will implicitly create a new
 * boundary string. To create a boundary string first the 
 * HttpMethodParams.MULTIPART_BOUNDARY parameter is considered. Otherwise 
 * a random one is generated.
 * 
 * @return The boundary string of this entity in ASCII encoding.
 */
protected byte[] getMultipartBoundary() {
    if (multipartBoundary == null) {
        String temp = (String) params.getParameter(HttpMethodParams.MULTIPART_BOUNDARY);
        if (temp != null) {
            multipartBoundary = EncodingUtil.getAsciiBytes(temp);
        } else {
            multipartBoundary = generateMultipartBoundary();
        }
    }
    return multipartBoundary;
}
 
源代码13 项目: knopflerfish.org   文件: StringPart.java
/**
 * Gets the content in bytes.  Bytes are lazily created to allow the charset to be changed
 * after the part is created.
 * 
 * @return the content in bytes
 */
private byte[] getContent() {
    if (content == null) {
        content = EncodingUtil.getBytes(value, getCharSet());
    }
    return content;
}
 
源代码14 项目: knopflerfish.org   文件: Part.java
/**
 * Write the content disposition header to the specified output stream
 * 
 * @param out The output stream
 * @throws IOException If an IO problem occurs.
 */
protected void sendDispositionHeader(OutputStream out) throws IOException {
    LOG.trace("enter sendDispositionHeader(OutputStream out)");
    out.write(CONTENT_DISPOSITION_BYTES);
    out.write(QUOTE_BYTES);
    out.write(EncodingUtil.getAsciiBytes(getName()));
    out.write(QUOTE_BYTES);
}
 
源代码15 项目: knopflerfish.org   文件: Part.java
/**
 * Write the content type header to the specified output stream
 * @param out The output stream
 * @throws IOException If an IO problem occurs.
 */
 protected void sendContentTypeHeader(OutputStream out) throws IOException {
    LOG.trace("enter sendContentTypeHeader(OutputStream out)");
    String contentType = getContentType();
    if (contentType != null) {
        out.write(CRLF_BYTES);
        out.write(CONTENT_TYPE_BYTES);
        out.write(EncodingUtil.getAsciiBytes(contentType));
        String charSet = getCharSet();
        if (charSet != null) {
            out.write(CHARSET_BYTES);
            out.write(EncodingUtil.getAsciiBytes(charSet));
        }
    }
}
 
源代码16 项目: knopflerfish.org   文件: Part.java
/**
 * Write the content transfer encoding header to the specified 
 * output stream
 * 
 * @param out The output stream
 * @throws IOException If an IO problem occurs.
 */
 protected void sendTransferEncodingHeader(OutputStream out) throws IOException {
    LOG.trace("enter sendTransferEncodingHeader(OutputStream out)");
    String transferEncoding = getTransferEncoding();
    if (transferEncoding != null) {
        out.write(CRLF_BYTES);
        out.write(CONTENT_TRANSFER_ENCODING_BYTES);
        out.write(EncodingUtil.getAsciiBytes(transferEncoding));
    }
}
 
源代码17 项目: knopflerfish.org   文件: FilePart.java
/**
 * Write the disposition header to the output stream
 * @param out The output stream
 * @throws IOException If an IO problem occurs
 * @see Part#sendDispositionHeader(OutputStream)
 */
protected void sendDispositionHeader(OutputStream out) 
throws IOException {
    LOG.trace("enter sendDispositionHeader(OutputStream out)");
    super.sendDispositionHeader(out);
    String filename = this.source.getFileName();
    if (filename != null) {
        out.write(FILE_NAME_BYTES);
        out.write(QUOTE_BYTES);
        out.write(EncodingUtil.getAsciiBytes(filename));
        out.write(QUOTE_BYTES);
    }
}
 
源代码18 项目: knopflerfish.org   文件: ChunkedOutputStream.java
/**
 * Writes the cache out onto the underlying stream
 * @throws IOException
 * 
 * @since 3.0
 */
protected void flushCache() throws IOException {
    if (cachePosition > 0) {
        byte chunkHeader[] = EncodingUtil.getAsciiBytes(
                Integer.toHexString(cachePosition) + "\r\n");
        stream.write(chunkHeader, 0, chunkHeader.length);
        stream.write(cache, 0, cachePosition);
        stream.write(ENDCHUNK, 0, ENDCHUNK.length);
        cachePosition = 0;
    }
}
 
源代码19 项目: knopflerfish.org   文件: NTLM.java
/** 
 * Returns the response that has been generated after shrinking the array if
 * required and base64 encodes the response.
 * @return The response as above.
 */
private String getResponse() {
    byte[] resp;
    if (currentResponse.length > currentPosition) {
        byte[] tmp = new byte[currentPosition];
        for (int i = 0; i < currentPosition; i++) {
            tmp[i] = currentResponse[i];
        }
        resp = tmp;
    } else {
        resp = currentResponse;
    }
    return EncodingUtil.getAsciiString(Base64.encodeBase64(resp));
}
 
源代码20 项目: knopflerfish.org   文件: NTLM.java
/** 
 * Extracts the server nonce out of the given message type 2.
 * 
 * @param message the String containing the base64 encoded message.
 * @return an array of 8 bytes that the server sent to be used when
 * hashing the password.
 */
public byte[] parseType2Message(String message) {
    // Decode the message first.
    byte[] msg = Base64.decodeBase64(EncodingUtil.getBytes(message, DEFAULT_CHARSET));
    byte[] nonce = new byte[8];
    // The nonce is the 8 bytes starting from the byte in position 24.
    for (int i = 0; i < 8; i++) {
        nonce[i] = msg[i + 24];
    }
    return nonce;
}
 
源代码21 项目: webarchive-commons   文件: LaxURI.java
protected static String decode(String component, String charset)
        throws URIException {
    if (component == null) {
        throw new IllegalArgumentException(
                "Component array of chars may not be null");
    }
    byte[] rawdata = null;
    //     try {
    rawdata = LaxURLCodec.decodeUrlLoose(EncodingUtil
            .getAsciiBytes(component));
    //     } catch (DecoderException e) {
    //         throw new URIException(e.getMessage());
    //     }
    return EncodingUtil.getString(rawdata, charset);
}
 
源代码22 项目: knopflerfish.org   文件: ChunkedInputStream.java
/**
 * Expects the stream to start with a chunksize in hex with optional
 * comments after a semicolon. The line must end with a CRLF: "a3; some
 * comment\r\n" Positions the stream at the start of the next line.
 *
 * @param in The new input stream.
 * @param required <tt>true<tt/> if a valid chunk must be present,
 *                 <tt>false<tt/> otherwise.
 * 
 * @return the chunk size as integer
 * 
 * @throws IOException when the chunk size could not be parsed
 */
private static int getChunkSizeFromInputStream(final InputStream in) 
  throws IOException {
        
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // States: 0=normal, 1=\r was scanned, 2=inside quoted string, -1=end
    int state = 0; 
    while (state != -1) {
    int b = in.read();
        if (b == -1) { 
            throw new IOException("chunked stream ended unexpectedly");
        }
        switch (state) {
            case 0: 
                switch (b) {
                    case '\r':
                        state = 1;
                        break;
                    case '\"':
                        state = 2;
                        /* fall through */
                    default:
                        baos.write(b);
                }
                break;

            case 1:
                if (b == '\n') {
                    state = -1;
                } else {
                    // this was not CRLF
                    throw new IOException("Protocol violation: Unexpected"
                        + " single newline character in chunk size");
                }
                break;

            case 2:
                switch (b) {
                    case '\\':
                        b = in.read();
                        baos.write(b);
                        break;
                    case '\"':
                        state = 0;
                        /* fall through */
                    default:
                        baos.write(b);
                }
                break;
            default: throw new RuntimeException("assertion failed");
        }
    }

    //parse data
    String dataString = EncodingUtil.getAsciiString(baos.toByteArray());
    int separator = dataString.indexOf(';');
    dataString = (separator > 0)
        ? dataString.substring(0, separator).trim()
        : dataString.trim();

    int result;
    try {
        result = Integer.parseInt(dataString.trim(), 16);
    } catch (NumberFormatException e) {
        throw new IOException ("Bad chunk size: " + dataString);
    }
    return result;
}
 
源代码23 项目: knopflerfish.org   文件: MultipartRequestEntity.java
public String getContentType() {
    StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE);
    buffer.append("; boundary=");
    buffer.append(EncodingUtil.getAsciiString(getMultipartBoundary()));
    return buffer.toString();
}
 
源代码24 项目: knopflerfish.org   文件: NTLM.java
/**
 * Creates the first message (type 1 message) in the NTLM authentication sequence.
 * This message includes the user name, domain and host for the authentication session.
 *
 * @param host the computer name of the host requesting authentication.
 * @param domain The domain to authenticate with.
 * @return String the message to add to the HTTP request header.
 */
public String getType1Message(String host, String domain) {
    host = host.toUpperCase();
    domain = domain.toUpperCase();
    byte[] hostBytes = EncodingUtil.getBytes(host, DEFAULT_CHARSET);
    byte[] domainBytes = EncodingUtil.getBytes(domain, DEFAULT_CHARSET);

    int finalLength = 32 + hostBytes.length + domainBytes.length;
    prepareResponse(finalLength);
    
    // The initial id string.
    byte[] protocol = EncodingUtil.getBytes("NTLMSSP", DEFAULT_CHARSET);
    addBytes(protocol);
    addByte((byte) 0);

    // Type
    addByte((byte) 1);
    addByte((byte) 0);
    addByte((byte) 0);
    addByte((byte) 0);

    // Flags
    addByte((byte) 6);
    addByte((byte) 82);
    addByte((byte) 0);
    addByte((byte) 0);

    // Domain length (first time).
    int iDomLen = domainBytes.length;
    byte[] domLen = convertShort(iDomLen);
    addByte(domLen[0]);
    addByte(domLen[1]);

    // Domain length (second time).
    addByte(domLen[0]);
    addByte(domLen[1]);

    // Domain offset.
    byte[] domOff = convertShort(hostBytes.length + 32);
    addByte(domOff[0]);
    addByte(domOff[1]);
    addByte((byte) 0);
    addByte((byte) 0);

    // Host length (first time).
    byte[] hostLen = convertShort(hostBytes.length);
    addByte(hostLen[0]);
    addByte(hostLen[1]);

    // Host length (second time).
    addByte(hostLen[0]);
    addByte(hostLen[1]);

    // Host offset (always 32).
    byte[] hostOff = convertShort(32);
    addByte(hostOff[0]);
    addByte(hostOff[1]);
    addByte((byte) 0);
    addByte((byte) 0);

    // Host String.
    addBytes(hostBytes);

    // Domain String.
    addBytes(domainBytes);

    return getResponse();
}
 
源代码25 项目: knopflerfish.org   文件: NTLM.java
/** 
 * Creates the LANManager and NT response for the given password using the
 * given nonce.
 * @param password the password to create a hash for.
 * @param nonce the nonce sent by the server.
 * @return The response.
 * @throws HttpException If {@link #encrypt(byte[],byte[])} fails.
 */
private byte[] hashPassword(String password, byte[] nonce)
    throws AuthenticationException {
    byte[] passw = EncodingUtil.getBytes(password.toUpperCase(), credentialCharset);
    byte[] lmPw1 = new byte[7];
    byte[] lmPw2 = new byte[7];

    int len = passw.length;
    if (len > 7) {
        len = 7;
    }

    int idx;
    for (idx = 0; idx < len; idx++) {
        lmPw1[idx] = passw[idx];
    }
    for (; idx < 7; idx++) {
        lmPw1[idx] = (byte) 0;
    }

    len = passw.length;
    if (len > 14) {
        len = 14;
    }
    for (idx = 7; idx < len; idx++) {
        lmPw2[idx - 7] = passw[idx];
    }
    for (; idx < 14; idx++) {
        lmPw2[idx - 7] = (byte) 0;
    }

    // Create LanManager hashed Password
    byte[] magic = {
        (byte) 0x4B, (byte) 0x47, (byte) 0x53, (byte) 0x21, 
        (byte) 0x40, (byte) 0x23, (byte) 0x24, (byte) 0x25
    };

    byte[] lmHpw1;
    lmHpw1 = encrypt(lmPw1, magic);

    byte[] lmHpw2 = encrypt(lmPw2, magic);

    byte[] lmHpw = new byte[21];
    for (int i = 0; i < lmHpw1.length; i++) {
        lmHpw[i] = lmHpw1[i];
    }
    for (int i = 0; i < lmHpw2.length; i++) {
        lmHpw[i + 8] = lmHpw2[i];
    }
    for (int i = 0; i < 5; i++) {
        lmHpw[i + 16] = (byte) 0;
    }

    // Create the responses.
    byte[] lmResp = new byte[24];
    calcResp(lmHpw, nonce, lmResp);

    return lmResp;
}
 
源代码26 项目: webarchive-commons   文件: HeaderedArchiveRecord.java
/**
 * Read header if present. Technique borrowed from HttpClient HttpParse
 * class. Using http parser code for now. Later move to more generic header
 * parsing code if there proves a need.
 * 
 * @return ByteArrayInputStream with the http header in it or null if no
 *         http header.
 * @throws IOException
 */
private InputStream readContentHeaders() throws IOException {
    // If judged a record that doesn't have an http header, return
    // immediately.
    if (!hasContentHeaders()) {
        return null;
    }
    byte [] statusBytes = LaxHttpParser.readRawLine(getIn());
    int eolCharCount = getEolCharsCount(statusBytes);
    if (eolCharCount <= 0) {
        throw new IOException("Failed to read raw lie where one " +
            " was expected: " + new String(statusBytes));
    }
    String statusLine = EncodingUtil.getString(statusBytes, 0,
        statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING);
    if (statusLine == null) {
        throw new NullPointerException("Expected status line is null");
    }
    // TODO: Tighten up this test.
    boolean isHttpResponse = StatusLine.startsWithHTTP(statusLine);
    boolean isHttpRequest = false;
    if (!isHttpResponse) {
        isHttpRequest = statusLine.toUpperCase().startsWith("GET") ||
            !statusLine.toUpperCase().startsWith("POST");
    }
    if (!isHttpResponse && !isHttpRequest) {
        throw new UnexpectedStartLineIOException("Failed parse of " +
            "status line: " + statusLine);
    }
    this.statusCode = isHttpResponse?
        (new StatusLine(statusLine)).getStatusCode(): -1;
    
    // Save off all bytes read.  Keep them as bytes rather than
    // convert to strings so we don't have to worry about encodings
    // though this should never be a problem doing http headers since
    // its all supposed to be ascii.
    ByteArrayOutputStream baos =
        new ByteArrayOutputStream(statusBytes.length + 4 * 1024);
    baos.write(statusBytes);
    
    // Now read rest of the header lines looking for the separation
    // between header and body.
    for (byte [] lineBytes = null; true;) {
        lineBytes = LaxHttpParser.readRawLine(getIn());
        eolCharCount = getEolCharsCount(lineBytes);
        if (eolCharCount <= 0) {
            throw new IOException("Failed reading headers: " +
                ((lineBytes != null)? new String(lineBytes): null));
        }
        // Save the bytes read.
        baos.write(lineBytes);
        if ((lineBytes.length - eolCharCount) <= 0) {
            // We've finished reading the http header.
            break;
        }
    }
    
    byte [] headerBytes = baos.toByteArray();
    // Save off where content body, post content headers, starts.
    this.contentHeadersLength = headerBytes.length;
    ByteArrayInputStream bais =
        new ByteArrayInputStream(headerBytes);
    if (!bais.markSupported()) {
        throw new IOException("ByteArrayInputStream does not support mark");
    }
    bais.mark(headerBytes.length);
    // Read the status line.  Don't let it into the parseHeaders function.
    // It doesn't know what to do with it.
    bais.read(statusBytes, 0, statusBytes.length);
    this.contentHeaders = LaxHttpParser.parseHeaders(bais,
        ARCConstants.DEFAULT_ENCODING);
    bais.reset();
    return bais;
}
 
源代码27 项目: http4e   文件: HttpMethodBase.java
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header.
 * 
 * Note: This will cause the entire response body to be buffered in memory. A
 * malicious server may easily exhaust all the VM memory. It is strongly
 * recommended, to use getResponseAsStream if the content length of the response
 * is unknown or resonably large.
 * 
 * @return The response body.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString() throws IOException {
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody();
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}
 
源代码28 项目: knopflerfish.org   文件: ChunkedOutputStream.java
/**
 * Writes the cache and bufferToAppend to the underlying stream
 * as one large chunk
 * @param bufferToAppend
 * @param off
 * @param len
 * @throws IOException
 * 
 * @since 3.0
 */
protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException {
    byte chunkHeader[] = EncodingUtil.getAsciiBytes(
            Integer.toHexString(cachePosition + len) + "\r\n");
    stream.write(chunkHeader, 0, chunkHeader.length);
    stream.write(cache, 0, cachePosition);
    stream.write(bufferToAppend, off, len);
    stream.write(ENDCHUNK, 0, ENDCHUNK.length);
    cachePosition = 0;
}
 
源代码29 项目: knopflerfish.org   文件: HttpMethodBase.java
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header. Buffers the response and this method can be 
 * called several times yielding the same result each time.
 * 
 * Note: This will cause the entire response body to be buffered in memory. A
 * malicious server may easily exhaust all the VM memory. It is strongly
 * recommended, to use getResponseAsStream if the content length of the response
 * is unknown or resonably large.
 * 
 * @return The response body or <code>null</code>.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString() throws IOException {
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody();
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}
 
源代码30 项目: knopflerfish.org   文件: HttpMethodBase.java
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header. Buffers the response and this method can be 
 * called several times yielding the same result each time.</p>
 * 
 * Note: This will cause the entire response body to be buffered in memory. This method is
 * safe if the content length of the response is unknown, because the amount of memory used
 * is limited.<p>
 * 
 * If the response is large this method involves lots of array copying and many object 
 * allocations, which makes it unsuitable for high-performance / low-footprint applications.
 * Those applications should use {@link #getResponseBodyAsStream()}.
 * 
 * @param maxlen the maximum content length to accept (number of bytes). Note that,
 * depending on the encoding, this is not equal to the number of characters.
 * @return The response body or <code>null</code>.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString(int maxlen) throws IOException {
    if (maxlen < 0) throw new IllegalArgumentException("maxlen must be positive");
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody(maxlen);
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}
 
 类方法
 同包方法