http client chunked implement via sun

转载 2007年09月25日 14:40:00

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;
    }

   

相关文章推荐

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport e

摘要:在调用webservice客户端代码的时候报如下错误 Invoking reportLossByRdid... Exception in thread "main" com.sun.xml.i...

Socks via HTTP 1.2 Client

  • 2008-03-17 23:08
  • 73KB
  • 下载

Implement NAT via iptables

DMZ ( demilitarized zone ) 和 Virtual server 是NAT ( Network Address Translation ) 两种不同层次的实现. D...

Steps to Set up and Implement Web Service via Eclipse and Axis2 1.6

Part I Setup Step 1. Install Axis2 environment to c:\axis\ and run it. This step is simple. Ju...

用java发送Http请求,并对chunked解码的介绍

很多时候,都要去互联网上抓取一些网页的数据,目前普遍的都使用chunked的编码,如果自己通过socket去获取数据,就需要对这些数据进行解码,这里我简单介绍下如何对chunked进行编码。给出了一个...

HTTP响应Chunked编码

最近公司有个应用要为第3方系统提供接口,大概要求就是对方向我方系统发送一个XML报文,我方根据请求报文响应数据,并以XML格式进行返回.测试的时候发现一个比较诡异的问题,有时候请求响应会无故多生成一些...

http协议中content-length 以及chunked编码分析

0.序 1.http/1.1协议中与chunked编码的相关字段 1)Entity Body 2)Content-length 3)Message Length 4)content-le...

HTTP1.1中CHUNKED编码解析

HTTP1.1中CHUNKED编码解析一般HTTP通信时,会使用Content-Length头信息性来通知用户代理(通常意义上是浏览器)服务器发送的文档内容长度,该头信息定义于HTTP1.0协议RFC...

Http 响应头中Transfer-Encoding: chunked

在http 的响应头中有时会见到这样的字段:Transfer-Encoding: chunked,这是一种分段传输数据的方式。如果对此格式不了解,直接将响应体以某一编码转换成字符串,就会出现乱码。re...

http协议中content-length 以及chunked编码分析

转载请注明出处 http://blog.csdn.net/yankai0219/article/details/8269922  0.序 1.http/1.1协议中与chunked...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)