Search in sources :

Example 1 with HttpClient

use of com.generallycloud.baseio.codec.http11.HttpClient in project baseio by generallycloud.

the class TestSimpleHttpClient2 method main.

public static void main(String[] args) throws IOException {
    String host = "www.baidu.com";
    host = "generallycloud.com";
    host = "127.0.0.1";
    int port = 443;
    port = 8080;
    HttpIOEventHandle eventHandleAdaptor = new HttpIOEventHandle();
    ServerConfiguration c = new ServerConfiguration(host, port);
    SocketChannelContext context = new NioSocketChannelContext(c);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setProtocolFactory(new ClientHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    if (port == 443) {
        context.setSslContext(SSLUtil.initClient(true));
    }
    SocketSession session = connector.connect();
    HttpClient client = new HttpClient(session);
    HttpFuture future = new ClientHttpFuture(context, "/");
    HttpFuture res = client.request(future, 99990000);
    System.out.println();
    System.out.println(new String(res.getBodyContent()));
    System.out.println();
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ClientHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) HttpIOEventHandle(com.generallycloud.baseio.codec.http11.HttpIOEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) HttpClient(com.generallycloud.baseio.codec.http11.HttpClient)

Example 2 with HttpClient

use of com.generallycloud.baseio.codec.http11.HttpClient in project baseio by generallycloud.

the class TestSimpleHttpClient method main.

public static void main(String[] args) throws Exception {
    HttpIOEventHandle eventHandleAdaptor = new HttpIOEventHandle();
    // ServerConfiguration c = new ServerConfiguration("localhost",80);
    ServerConfiguration c = new ServerConfiguration("generallycloud.com", 443);
    SocketChannelContext context = new NioSocketChannelContext(c);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    SslContext sslContext = SSLUtil.initClient(true);
    context.setProtocolFactory(new ClientHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setSslContext(sslContext);
    SocketSession session = connector.connect();
    HttpClient client = new HttpClient(session);
    HttpFuture future = new ClientHttpFuture(context, "/test-show-memory");
    HttpFuture res = client.request(future, 10000);
    System.out.println();
    System.out.println(new String(res.getBodyContent()));
    System.out.println();
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ClientHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) HttpIOEventHandle(com.generallycloud.baseio.codec.http11.HttpIOEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) HttpClient(com.generallycloud.baseio.codec.http11.HttpClient) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Aggregations

ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)2 HttpClient (com.generallycloud.baseio.codec.http11.HttpClient)2 HttpIOEventHandle (com.generallycloud.baseio.codec.http11.HttpIOEventHandle)2 ClientHttpFuture (com.generallycloud.baseio.codec.http11.future.ClientHttpFuture)2 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)2 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)2 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)2 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)2 SocketSession (com.generallycloud.baseio.component.SocketSession)2 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)2 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)2 SslContext (com.generallycloud.baseio.component.ssl.SslContext)1