概述
Jetty提供了两个HTTP / 2 Java客户端API.两者都需要Java 8和强制使用ALPN,如
here所述.
低级API
这些API基于HTTP2Client,它基于会话和流的HTTP / 2概念,并使用侦听器来通知从服务器到达的HTTP / 2帧.
// Setup and start the HTTP2Client.
HTTP2Client client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
client.addBean(sslContextFactory);
client.start();
// Connect to the remote host to obtains a Session.
FuturePromise sessionPromise = new FuturePromise<>();
client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);
Session session = sessionPromise.get(5, TimeUnit.SECONDS);
// Use the session to make requests.
HttpFields requestFields = new HttpFields();
requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://webtide.com/"), HttpVersion.HTTP_2, requestFields);
HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter()
{
@Override
public void onHeaders(Stream stream, HeadersFrame frame)
{
// Response headers.
System.err.println(frame);
}
@Override
public void onData(Stream stream, DataFrame frame, Callback callback)
{
// Response content.
System.err.println(frame);
callback.succeeded();
}
});
高级API
Jetty的HttpClient提供了一种使用different transports的方法,其中一个是HTTP/2 transport.应用程序将使用更高级别的HTTP API,但Jetty下面将使用HTTP / 2来传输HTTP语义.
通过这种方式,应用程序可以透明地使用HttpClient提供的高级API,并分析在配置或启动代码中使用的传输.
// Setup and start HttpClient with HTTP/2 transport.
HTTP2Client http2Client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
httpClient.start();
// Make a request.
ContentResponse response = httpClient.GET("https://webtide.com/");
最后
以上就是眼睛大月饼为你收集整理的java http apache_Apache或其他一些CLIENT JAVA实现是否支持HTTP / 2?的全部内容,希望文章能够帮你解决java http apache_Apache或其他一些CLIENT JAVA实现是否支持HTTP / 2?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复