异步HTTP和WebSocket的Java客户端:Async Http Client
jopen
10年前
异步HTTP和WebSocket的客户端库Java类库。允许Java应用程序轻松地执行HTTP请求并异步处理HTTP响应。该库还支持WebSocket协议。Async Http Client使用简单。
import com.ning.http.client.*; import java.util.concurrent.Future; AsyncHttpClient c = new AsyncHttpClient(); Future<String> f = c.prepareGet("http://www.ning.com/").execute(new AsyncHandler<String>() { private ByteArrayOutputStream bytes = new ByteArrayOutputStream(); @Override public STATE onStatusReceived(HttpResponseStatus status) throws Exception { int statusCode = status.getStatusCode(); // The Status have been read // If you don't want to read the headers,body or stop processing the response if (statusCode >= 500) { return STATE.ABORT; } } @Override public STATE onHeadersReceived(HttpResponseHeaders h) throws Exception { Headers headers = h.getHeaders(); // The headers have been read // If you don't want to read the body, or stop processing the response return STATE.ABORT; } @Override public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { bytes.write(bodyPart.getBodyPartBytes()); return STATE.CONTINUE; } @Override public String onCompleted() throws Exception { // Will be invoked once the response has been fully read or a ResponseComplete exception // has been thrown. // NOTE: should probably use Content-Encoding from headers return bytes.toString("UTF-8"); } @Override public void onThrowable(Throwable t) { } }); String bodyResponse = f.get();