关闭

http client chunked implement via sun

1068人阅读 评论(0) 收藏 举报

private void sendRequestHeader()
        throws IOException
    {
        if(!isOwnerTrusted)
            reqProperties.addProperty("User-Agent", "UNTRUSTED/1.0");
        if(getRequestProperty("Content-Length") == null)
            setRequestField("Content-Length", Integer.toString(bytesToWrite));
        StringBuffer reqLine = new StringBuffer(256);
        String filename = url.path;
        if(filename == null)
            filename = "/";
        reqLine.append(method);
        reqLine.append(" ");
        reqLine.append(filename);
        if(url.query != null)
        {
            reqLine.append("?");
            reqLine.append(url.query);
        }
        reqLine.append(" ");
        reqLine.append(HTTP_VERSION);
        reqLine.append("/r/n");
        setRequestField("Host", url.authority);
        if(chunkedOut)
            setRequestField("Transfer-Encoding", "chunked");
        int numberOfKeys = reqProperties.size();
        for(int i = 0; i < numberOfKeys; i++)
        {
            String key = reqProperties.getKeyAt(i);
            if(key.equals("Content-Length"))
            {
                if(!chunkedOut)
                {
                    if(writebuf == null)
                    {
                        reqLine.append("Content-Length: 0");
                    } else
                    {
                        reqLine.append("Content-Length: ");
                        reqLine.append(bytesToWrite);
                    }
                    reqLine.append("/r/n");
                }
            } else
            {
                reqLine.append(key);
                reqLine.append(": ");
                reqLine.append(reqProperties.getValueAt(i));
                reqLine.append("/r/n");
            }
        }

        reqLine.append("/r/n");
        streamOutput.write(reqLine.toString().getBytes());

    }

  protected void finishRequestGetResponseHeader()
        throws IOException
    {
        requestFinished = true;
        if(chunkedOut)
            streamOutput.write("0/r/n/r/n".getBytes());
        streamOutput.flush();

        readResponseMessage(streamInput);
        readHeaders(streamInput);
        if(responseCode == 100)
        {
            readResponseMessage(streamInput);
            readHeaders(streamInput);
        }
    }


 protected void sendRequestBody()
        throws IOException
    {
        if(bytesToWrite == 0)
            return;
        if(!useChunks)
        {
            byte buff[] = writeStreamBuff.toByteArray();
            streamOutput.write(buff, 0, buff.length);
            bytesToWrite = 0;
            return;
        }
        if(writebuf == null)
            return;
        int start = 24;
        int endOfData = 24 + bytesToWrite;
        int length = bytesToWrite;
        if(chunkedOut)
        {
            String temp = Integer.toHexString(bytesToWrite);
            int tempLen = temp.length();
            writebuf[--start] = 10;
            writebuf[--start] = 13;
            for(int i = tempLen - 1; i >= 0; i--)
                writebuf[--start] = (byte)temp.charAt(i);

            length += tempLen + 2;
            writebuf[endOfData++] = 13;
            writebuf[endOfData++] = 10;
            length += 2;
        }
        streamOutput.write(writebuf, start, length);
        bytesToWrite = 0;
    }
//protocol .class

   private void sendRequest(boolean chunkData, boolean readResponseHeader)
        throws IOException
    {
        if(sendingRequest || requestFinished)
            return;
        sendingRequest = true;
        if(chunkData)
            chunkedOut = true;
        int bytesToRetry = bytesToWrite;
        try
        {
            startRequest();
            sendRequestBody();
            if(readResponseHeader)
                finishRequestGetResponseHeader();
        }
        catch(IOException ioe)
        {
            if(!(streamConnection instanceof StreamConnectionElement))
                throw ioe;
            try
            {
                connectionPool.remove((StreamConnectionElement)streamConnection);
            }
            catch(Exception e) { }
            if(firstChunkSent)
                throw new IOException("Persistent connection dropped after first chunk sent, cannot retry");
            streamConnection = null;
            streamInput = null;
            streamOutput = null;
            bytesToWrite = bytesToRetry;
            startRequest();
            sendRequestBody();
            if(readResponseHeader)
                finishRequestGetResponseHeader();
        }
        if(chunkedOut)
            firstChunkSent = true;
        sendingRequest = false;
        break MISSING_BLOCK_LABEL_169;
        Exception exception;
        exception;
        sendingRequest = false;
        throw exception;
    }

   

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:402133次
    • 积分:5558
    • 等级:
    • 排名:第4713名
    • 原创:120篇
    • 转载:225篇
    • 译文:0篇
    • 评论:72条
    最新评论
    俺的朋友们
    我的生活博客