HttpGet httpget = new HttpGet("http://www.google.com/");
System.out.println("executing request " + httpget.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
这将得到responseBody作为“string”,如何使用httpclient.execute(..)获取btye[]?我想获取 byte[] 的原因是因为我想写入其他输出流
最佳答案
public byte[] executeBinary(URI uri) throws IOException, ClientProtocolException {
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.writeTo(baos);
return baos.toByteArray();
}
关于java - httpclient 4如何获取字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1084267/