besthttp 作为高性能HTTP 客户端框架的实践,改进地方和原理文档一直没有时间整理,借助infoQ 平台做了普及和释疑。
尊重版本所有,附上 HTTP客户端演进之路-InfoQ
文中提及【场景1】代码示例
第一代:同步模式,解决网络交互【代码1】
同步模式组件Apache HttpClient 代码示例:
public void processResponse(HttpResponse response){
HttpEntity resEntity = response.getEntity();
// 响应结果逻辑处理
if (resEntity != null) {
EntityUtils.toString(resEntity, Charset.forName("UTF-8"));
}
EntityUtils.consume(resEntity);
}
public void upload(){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(URL_STR);
FileBody fileBody = new FileBody(new File(LOCALFILE));
StringBody userName = new StringBody("nixian", ContentType.create("text/plain", Consts.UTF_8));
StringBody password = new StringBody("handsome", ContentType.create("text/plain", Consts.UTF_8));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("fileBody", fileBody)
.addPart("userName", userName)
.addPart("password ", password)
.build();
httpPost.setEntity(reqEntity);
response = httpClient.execute(httpPost);
processResponse(response);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(response != null){
response.close();
}
} catch (IOException e) {
.printStackTrace();
}
try {
if(httpClient != null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
第二代:异步模式 ,面对高并发
异步模式组件HttpAsyncClient,基于事件驱动的编程模型,代码示例:
public void upload(){
CloseableHttpClient httpClient = null;
try {
httpClient = HttpAsyncClients.createDefault();
httpClient.start();
HttpPost httpPost = new HttpPost(URL_STR);
File file = new File(LOCALFILE);
ByteArrayBody body;
body = new ByteArrayBody(FileUtils.readFileToByteArray(file), file.getName());
HttpEntity mEntity = MultipartEntityBuilder.create()
.setBoundary("----" + "XXXXXX")
.addPart("fileBody", body)
.build();
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
mEntity.writeTo(baoStream);
HttpEntity nByteEntity = new NByteArrayEntity(baoStream.toByteArray(), ContentType.MULTIPART_FORM_DATA);
httpPost.setEntity(nByteEntity);
httpClient.execute(httpPost, new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse response) {
processResponse(response);
}
@Override
public void failed(Exception e) {
}
@Override
public void cancelled() {
}
});
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(httpClient != null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
第三代: 极致模式,besthttp Client
极致模式组件 besthttp 代码示例:
public Callback upload() {
Callback back,call;
call = (back = Callback.create()).thencall(res->{return processResponse(res);});
List<MultipartParam> params = new ArrayList<MultipartParam>();
params.add( FileUploadParam.create(new File(TXT)).setCharset("utf-8")
.setMimeType("txt").setName("fileBody"));
params.add( FileUploadParam.create(new File(JPEG)).setCharset("utf-8")
.setMimeType("jpeg").setName("fileBody"));
params.add( StringParam.create("nixian").setCharset("utf-8").setMimeType("text/plain").setName("userName"));
params.add( StringParam.create("handsome").setCharset("utf-8").setMimeType("text/plain").setName("password"));
CloseableHttpAsyncClient httpClient = HttpClients.getClient();
try {
httpClient.execute(
BestHttpAsyncMethods.createMultipartPostUploadStrict(
params,
HeaderParam.create(URL_STR).setCharset("utf-8")
),
BestHttpAsyncMethods.createConsumer(),
back
);
}
catch (Exception e) {
e.printStackTrace();
return Callback.create(e);
}
return call;
}