Blackberry Java-通过HTTP连接固定长度流式传输帖子正文

Blackberry Java-通过HTTP连接固定长度流式传输帖子正文,blackberry,java-me,Blackberry,Java Me,我正在编写一些代码,这些代码通常通过HTTP将大数据包发布到IIS上的REST服务器。我正在使用RIM/JavaME类 据我所知,HTTPConnection在将全部内容发送到服务器之前使用内部缓冲区“收集”输出流。我并不感到惊讶,因为HttpURLConnect在默认情况下也是这样工作的。(我假设它这样做是为了正确设置内容长度。)但在JavaSE中,我可以通过使用该方法重写此行为,以便在我对输出流调用flush时,它将发送流的“块”。在手机上,这种额外的缓冲在内存方面太昂贵了 在Blackbe

我正在编写一些代码,这些代码通常通过HTTP将大数据包发布到IIS上的REST服务器。我正在使用RIM/JavaME类

据我所知,HTTPConnection在将全部内容发送到服务器之前使用内部缓冲区“收集”输出流。我并不感到惊讶,因为HttpURLConnect在默认情况下也是这样工作的。(我假设它这样做是为了正确设置内容长度。)但在JavaSE中,我可以通过使用该方法重写此行为,以便在我对输出流调用flush时,它将发送流的“块”。在手机上,这种额外的缓冲在内存方面太昂贵了


在Blackberry Java中,当您事先知道内容长度时,有没有一种方法可以对HTTP请求执行固定长度的流式传输

所以,我从来没有找到一种方法来实现这一点,那就是HTTPConnection的基本API。因此,我创建了一个套接字,并用我自己的简单HTTPClient包装它,它确实支持分块

下面是我在BB7.0上使用和测试的原型

package mypackage;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;

public class MySimpleHTTPClient{

    SocketConnection sc;
    String HttpHeader;
    OutputStreamWriter outWriter;
    InputStreamReader inReader;

    public void init(
            String Host, 
            String port, 
            String path, 
            int ContentLength, 
            String  ContentType ) throws IllegalArgumentException, IOException
    {
        String _host = (new StringBuffer())
                    .append("socket://")
                    .append(Host)
                    .append(":")
                    .append(port).toString();
        sc = (SocketConnection)Connector.open(_host );
        sc.setSocketOption(SocketConnection.LINGER, 5);
        StringBuffer _header = new StringBuffer();
        //Setup the HTTP Header.
        _header.append("POST ").append(path).append(" HTTP/1.1\r\n");
        _header.append("Host: ").append(Host).append("\r\n");
        _header.append("Content-Length: ").append(ContentLength).append("\r\n");
        _header.append("Content-Type: ").append(ContentType).append("\r\n");
        _header.append("Connection: Close\r\n\r\n");
        HttpHeader = _header.toString();
    }

    public void openOutputStream() throws IOException{
        if(outWriter != null) 
            return;
        outWriter = new OutputStreamWriter(sc.openOutputStream());
        outWriter.write( HttpHeader, 0 , HttpHeader.length() );
    }

    public void openInputStream() throws IOException{
        if(inReader != null) 
            return;
        inReader = new InputStreamReader(sc.openDataInputStream());
    }

    public void writeChunkToServer(String Chunk) throws Exception{
        if(outWriter == null){
            try {
                openOutputStream();
            } catch (IOException e) {e.printStackTrace();}
        } 
        outWriter.write(Chunk, 0, Chunk.length());
    }

    public String readFromServer() throws IOException {
        if(inReader == null){
            try {
                openInputStream();
            } catch (IOException e) {e.printStackTrace();}
        }
        StringBuffer sb = new StringBuffer();
        int data = inReader.read();
        //Note ::  This will also read the HTTP headers..
        // If you need to parse the headers, tokenize on \r\n for each 
        // header, the header section is done when you see \r\n\r\n
        while(data != -1){
            sb.append( (char)data  );
            data = inReader.read();
        }
        return sb.toString();
    }

    public void close(){
        if(outWriter != null){
            try {
                outWriter.close();
            } catch (IOException e) {}
        }
        if(inReader != null){
            try {
                inReader.close();
            } catch (IOException e) {}
        }
        if(sc != null){
            try {
                sc.close();
            } catch (IOException e) {}
        }
    }
}
下面是它的用法示例:

MySimpleHTTPClient myConn = new MySimpleHTTPClient() ;
String chunk1 = "ID=foo&data1=1234567890&chunk1=0|";
String chunk2 = "ID=foo2&data2=123444344&chunk1=1";
try {
    myConn.init(
            "pdxsniffe02.webtrends.corp", 
            "80",
            "TableAdd/234234234443?debug=1",
            chunk1.length() + chunk2.length(), 
            "application/x-www-form-urlencoded" 
    );

    myConn.writeChunkToServer(chunk1);
    //The frist chunk is already on it's way.
    myConn.writeChunkToServer(chunk2);
    System.out.println( myConn.readFromServer() );

} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}finally{
    myConn.close();
}