14

如何获得OutputStreamusing org.apache.http.impl.client.DefaultHttpClient

我正在寻找将长字符串写入输出流。

使用HttpURLConnection你会像这样实现它:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
writeXml(wout);

有没有使用DefaultHttpClient类似于我上面的方法?我将如何写入OutputStreamusingDefaultHttpClient而不是HttpURLConnection

例如

DefaultHttpClient client = new DefaultHttpClient();

OutputStream outstream = (get OutputStream somehow)
Writer wout = new OutputStreamWriter(out);
4

4 回答 4

30

我知道另一个答案已经被接受,只是为了记录,这就是人们如何使用 HttpClient 写出内容而无需在内存中进行中间缓冲。

    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);
于 2012-04-14T09:52:15.410 回答
15

您无法直接从 BasicHttpClient 获取 OutputStream。您必须创建一个HttpUriRequest对象并为其提供一个HttpEntity封装您要发送的内容的对象。例如,如果您的输出足够小以适合内存,您可以执行以下操作:

// Produce the output
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writeXml(writer);

// Create the request
HttpPost request = new HttpPost(uri);
request.setEntity(new ByteArrayEntity(out.toByteArray()));

// Send the request
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);

如果数据足够大以至于您需要对其进行流式传输,则变得更加困难,因为没有HttpEntity接受 OutputStream 的实现。您需要写入临时文件并使用FileEntity或可能设置管道并使用InputStreamEntity

编辑请参阅 oleg 的答案以获取演示如何流式传输内容的示例代码 - 毕竟您不需要临时文件或管道。

于 2012-04-13T19:50:05.017 回答
3

这在安卓上运行良好。它也应该适用于大文件,因为不需要缓冲。

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
于 2014-04-25T09:12:00.590 回答
2

我写了一个 Apache 的 HTTP 客户端 API [PipedApacheClientOutputStream]的反转,它使用 Apache Commons HTTP Client 4.3.4 为 HTTP POST 提供了一个 OutputStream 接口。

调用代码如下所示:

// Calling-code manages thread-pool
ExecutorService es = Executors.newCachedThreadPool(
  new ThreadFactoryBuilder()
  .setNameFormat("apache-client-executor-thread-%d")
  .build());


// Build configuration
PipedApacheClientOutputStreamConfig config = new      
  PipedApacheClientOutputStreamConfig();
config.setUrl("http://localhost:3000");
config.setPipeBufferSizeBytes(1024);
config.setThreadPool(es);
config.setHttpClient(HttpClientBuilder.create().build());

// Instantiate OutputStream
PipedApacheClientOutputStream os = new     
PipedApacheClientOutputStream(config);

// Write to OutputStream
os.write(...);

try {
  os.close();
} catch (IOException e) {
  logger.error(e.getLocalizedMessage(), e);
}

// Do stuff with HTTP response
...

// Close the HTTP response
os.getResponse().close();

// Finally, shut down thread pool
// This must occur after retrieving response (after is) if interested   
// in POST result
es.shutdown();

注意-实际上,相同的客户端、执行器服务和配置可能会在应用程序的整个生命周期中重复使用,因此上述示例中的外部准备和关闭代码可能会存在于引导/初始化和终结代码中,而不是直接内联OutputStream 实例化。

于 2016-03-07T17:03:59.480 回答