HTTP2C(明文)客户端到HTTP2C服务器

时间:2016-03-21 13:44:39

标签: http2

我正在运行HTTP2C嵌入式Jetty 9.x服务器...请注意服务器连接器显示h2c ...

2016-03-21 09:25:44.082:INFO:oejs.ServerConnector:main: Started ServerConnector@66c7bd3f{HTTP/1.1,[http/1.1, h2c, h2c-17, h2c-16, h2c-15, h2c-14]}{0.0.0.0:8080}

我有一个OkHttpClient 3试图将HTTP2C与此服务器通话,但它总是被降级为HTTP / 1.1,我错过了什么?哪个Java客户端API支持HTTP2C?我的客户端代码如下......

package http2;

import java.util.Collections;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

    public class GetClear {

    public static void main(String[] args) throws Exception {

        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT).build();
        OkHttpClient client = new OkHttpClient.Builder().connectionSpecs(Collections.singletonList(spec)).build();

        Request request = new Request.Builder().url("http://localhost:8080/test").build();

        Response response = client.newCall(request).execute();
        System.out.println (response.body().string());
        System.out.println("****");

        response.body().close();

    }

}

[服务器从Jetty servlet打印'request.getProtocol'并显示HTTP / 1.1而不是HTTP / 2]。

TLS上的HTTP / 2服务器和客户端使用HTTP / 2工作得很好(客户端代码和服务器代码当然不同)。

任何帮助都将得到真正的赞赏。

3 个答案:

答案 0 :(得分:1)

Jetty日志行显示您已配置服务器连接器以使HTTP / 1.1成为默认协议(在包含支持的协议列表的括号之前是大写的“HTTP / 1.1”)。

您不显示服务器端代码,但有两种选择:

  1. 明确配置服务器连接器的默认协议:
  2. ConnectionFactory
    1. 以正确的顺序将HttpConfiguration httpConfig = new HttpConfiguration(); ConnectionFactory h1 = new HttpConnectionFactory(httpConfig); ConnectionFactory h2c = new HTTP2CServerConnectionFactory(httpConfig); ServerConnector serverConnector = new ServerConnector(server, h2c, h1); 个对象传递给服务器连接器,因为第一个将是默认协议:
    2. id

答案 1 :(得分:1)

使用Jetty HTTP2C客户端,相同的服务器代码可以正常工作。我猜OkHTTPClient不支持HTTP2C。

答案 2 :(得分:1)

使用jetty doc中的HelloHandler示例完整的h2c示例:

public class HelloServer {
    public static class HelloHandler extends AbstractHandler {
        final String greeting;
        final String body;

        public HelloHandler() {
            this("Hello World");
        }

        public HelloHandler(String greeting) {
            this(greeting, null);
        }

        public HelloHandler(String greeting, String body) {
            this.greeting = greeting;
            this.body = body;
        }

        public void handle(String target,
                           Request baseRequest,
                           HttpServletRequest request,
                           HttpServletResponse response) throws IOException,
                ServletException {
            response.setContentType("text/html; charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);

            PrintWriter out = response.getWriter();

            out.println("<h1>" + greeting + "</h1>");
            if (body != null) {
                out.println(body);
            }

            baseRequest.setHandled(true);
        }
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server();
        server.setHandler(new HelloHandler());

        HttpConfiguration httpConfig = new HttpConfiguration();
        ConnectionFactory h1 = new HttpConnectionFactory(httpConfig);
        ConnectionFactory h2c = new HTTP2CServerConnectionFactory(httpConfig);
        ServerConnector serverConnector = new ServerConnector(server, h1, h2c);
        serverConnector.setPort(8080);

        server.setConnectors(new ServerConnector[] { serverConnector });
        server.start();
        server.join();
    }
}