HttpEntity的类型及其使用(各种继承的使用)
package com.lyj.demo.customTests;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8;
@RunWith(SpringJUnit4ClassRunner.class)
public class HttpEntityTest {
@Test
public void basicHttpEntityTest() throws IOException {
InputStream inputStream = null;
BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
basicHttpEntity.setContent(inputStream);
basicHttpEntity.setContentLength(inputStream.available());
basicHttpEntity.setChunked(false);
}
@Test
public void byteArrayEntityTest() {
ByteArrayEntity byteArrayEntity = new ByteArrayEntity("你好呀".getBytes());
ByteArrayInputStream content = (ByteArrayInputStream) byteArrayEntity.getContent();
}
@Test
public void stringEntityTest() throws UnsupportedEncodingException {
StringBuilder stringBuilder = new StringBuilder();
Map<String, String> getenv = System.getenv();
for (Map.Entry<String,String> entry : getenv.entrySet()) {
stringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
}
String content = stringBuilder.toString();
System.out.println(content);
StringEntity entity = new StringEntity(content);
StringEntity entity1 = new StringEntity(content, UTF_8);
}
@Test
public void inputStreamEntityTest() throws IOException {
InputStream inputStream = null;
InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream, inputStream.available());
}
@Test
public void fileEntityTest() {
FileEntity fileEntity = new FileEntity(new File(""), ContentType.APPLICATION_FORM_URLENCODED);
FileEntity fileEntity1 = new FileEntity(new File(""), ContentType.APPLICATION_JSON);
System.out.println(ContentType.APPLICATION_JSON.getMimeType());
}
@Test
public void entityTempleteTest() throws IOException {
ContentProducer contentProducer = new ContentProducer() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
outputStream.write("你好呀".getBytes());
}
};
EntityTemplate entityTemplate = new EntityTemplate(contentProducer);
entityTemplate.writeTo(System.out);
}
@Test
public void httpEntityWrapperTest() throws IOException {
HttpEntityWrapper httpEntityWrapper = new HttpEntityWrapper(new HttpEntity() {
@Override
public boolean isRepeatable() {
return false;
}
@Override
public boolean isChunked() {
return false;
}
@Override
public long getContentLength() {
return 0;
}
@Override
public Header getContentType() {
return null;
}
@Override
public Header getContentEncoding() {
return null;
}
@Override
public InputStream getContent() throws IOException, UnsupportedOperationException {
return null;
}
@Override
public void writeTo(OutputStream outputStream) throws IOException {
outputStream.write("你好呀".getBytes());
}
@Override
public boolean isStreaming() {
return false;
}
@Override
public void consumeContent() throws IOException {
}
});
httpEntityWrapper.writeTo(System.out);
System.out.println(httpEntityWrapper.getContent());
}
@Test
public void bufferedHttpEntityTest() throws IOException {
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(new HttpEntity() {
@Override
public boolean isRepeatable() {
return false;
}
@Override
public boolean isChunked() {
return false;
}
@Override
public long getContentLength() {
return 0;
}
@Override
public Header getContentType() {
return null;
}
@Override
public Header getContentEncoding() {
return null;
}
@Override
public InputStream getContent() throws IOException, UnsupportedOperationException {
return null;
}
@Override
public void writeTo(OutputStream outputStream) throws IOException {
outputStream.write("你好呀".getBytes());
}
@Override
public boolean isStreaming() {
return false;
}
@Override
public void consumeContent() throws IOException {
}
});
bufferedHttpEntity.writeTo(System.out);
System.out.println(bufferedHttpEntity.getContent());
}
@Test
public void httpEntityTest1() throws IOException {
try {
HttpEntity entity = new StringEntity("这是一个字符串", StandardCharsets.UTF_8);
System.out.println(entity.getContentType());
System.out.println(entity.getContentEncoding());
System.out.println(entity.getContentLength());
System.out.println(entity.getContent().available());
System.out.println(entity.getContent());
System.out.println(EntityUtils.toString(entity));
System.out.println(EntityUtils.toByteArray(entity).length);
System.out.println("----------------------------------");
Header header = entity.getContentType();
System.out.println(header);
HeaderElement[] elements = header.getElements();
System.out.println(elements);
ContentType contentType = ContentType.get(entity);
System.out.println(contentType);
} catch (IOException | UnsupportedOperationException | UnsupportedCharsetException | ParseException e) {
throw new RuntimeException(e);
}
}
@Test
public void httpEntityTest2() throws IOException {
HttpResponse response = null;
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
try {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
EntityUtils.consume(entity);
}
}
}
}
参考链接