How do I write to an OutputStream using DefaultHttpClient?

后端 未结 4 1892
挽巷
挽巷 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:58

    I know that another answer has already been accepted, just for the record this is how one can write content out with HttpClient without intermediate buffering in memory.

        AbstractHttpEntity entity = new AbstractHttpEntity() {
    
            public boolean isRepeatable() {
                return false;
            }
    
            public long getContentLength() {
                return -1;
            }
    
            public boolean isStreaming() {
                return false;
            }
    
            public InputStream getContent() throws IOException {
                // Should be implemented as well but is irrelevant for this case
                throw new UnsupportedOperationException();
            }
    
            public void writeTo(final OutputStream outstream) throws IOException {
                Writer writer = new OutputStreamWriter(outstream, "UTF-8");
                writeXml(writer);
                writer.flush();
            }
    
        };
        HttpPost request = new HttpPost(uri);
        request.setEntity(entity);
    

提交回复
热议问题