How do I write to an OutputStream using DefaultHttpClient?

后端 未结 4 1893
挽巷
挽巷 2020-12-09 05:10

How do I get an OutputStream using org.apache.http.impl.client.DefaultHttpClient?

I\'m looking to write a long string to an output stream.

4条回答
  •  粉色の甜心
    2020-12-09 05:57

    This worked well on android. It should also work for large files, as no buffering is needed.

    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream();
    out.connect(in);
    new Thread() {
        @Override
        public void run() {
            //create your http request
            InputStreamEntity entity = new InputStreamEntity(in, -1);
            request.setEntity(entity);
            client.execute(request,...);
            //When this line is reached your data is actually written
        }
    }.start();
    //do whatever you like with your outputstream.
    out.write("Hallo".getBytes());
    out.flush();
    //close your streams
    

提交回复
热议问题