Example usage for io.netty.handler.codec.http HttpMethod POST

List of usage examples for io.netty.handler.codec.http HttpMethod POST

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpMethod POST.

Prototype

HttpMethod POST

To view the source code for io.netty.handler.codec.http HttpMethod POST.

Click Source Link

Document

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

Usage

From source file:com.twitter.http2.HttpRequestProxyTest.java

License:Apache License

@Test
public void testSetMethod() {
    assertSame(httpRequestProxy, httpRequestProxy.setMethod(HttpMethod.POST));
    assertEquals(httpRequest.getMethod(), httpRequestProxy.getMethod());
}

From source file:com.weibo.api.motan.transport.netty4.http.NettyHttpRequestHandlerTest.java

License:Apache License

private FullHttpRequest buildHttpRequest(String requestPath) throws Exception {
    PooledByteBufAllocator allocator = new PooledByteBufAllocator();
    ByteBuf buf = allocator.buffer(0);// w  w w . j av a 2  s  .c o m
    FullHttpRequest httpReqeust = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, requestPath,
            buf);
    return httpReqeust;
}

From source file:com.weibo.api.motan.transport.netty4.yar.YarMessageHandlerWarpperTest.java

License:Apache License

private FullHttpRequest buildHttpRequest(YarRequest yarRequest, String requestPath) throws Exception {
    PooledByteBufAllocator allocator = new PooledByteBufAllocator();
    ByteBuf buf = allocator.buffer(2048, 1024 * 1024);
    if (yarRequest != null) {
        buf.writeBytes(YarProtocol.toProtocolBytes(yarRequest));
    }//from w  w  w.j  a va  2s . c  om
    FullHttpRequest httpReqeust = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, requestPath,
            buf);
    return httpReqeust;
}

From source file:com.zhang.client.NettyHttpClient.java

License:Apache License

public NettyHttpResponseFuture doPost(NettyHttpRequest request) throws Exception {

    HttpRequest httpRequest = NettyHttpRequestUtil.create(request, HttpMethod.POST);
    InetSocketAddress route = new InetSocketAddress(request.getUri().getHost(), request.getUri().getPort());

    return channelPool.sendRequest(route, httpRequest);
}

From source file:com.zhang.util.NettyHttpRequestUtil.java

License:Apache License

public static HttpRequest create(NettyHttpRequest request, HttpMethod httpMethod) {
    HttpRequest httpRequest = null;//from   www  .j  a  va 2s . c  o  m
    if (HttpMethod.POST == httpMethod) {
        httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod,
                request.getUri().getRawPath(), request.getContent().retain());

        httpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.getContent().readableBytes());
    } else {
        httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod,
                request.getUri().getRawPath());
    }
    for (Entry<String, Object> entry : request.getHeaders().entrySet()) {
        httpRequest.headers().set(entry.getKey(), entry.getValue());
    }
    httpRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    httpRequest.headers().set(HttpHeaders.Names.HOST, request.getUri().getHost());

    return httpRequest;
}

From source file:com.zhucode.longio.transport.netty.HttpClientHandler.java

License:Open Source License

private ChannelFuture sendForHttp(ChannelHandlerContext ctx, byte[] bytes) {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            uri.toASCIIString(), Unpooled.wrappedBuffer(bytes));

    // http//from   w w w .j  a  v  a2s. co m
    request.headers().set(HttpHeaders.Names.HOST, uri.getHost());
    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());

    return ctx.writeAndFlush(request);
}

From source file:de.dfki.kiara.http.HttpTransport.java

License:Open Source License

public ChannelFutureAndConnection connect(URI uri, Map<String, Object> settings) throws IOException {
    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;/*from www.  j  av a  2 s. c o m*/
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        throw new IOException("Only HTTP(S) is supported.");
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    final HttpHandler httpClientHandler = new HttpHandler(this, uri, HttpMethod.POST);
    Bootstrap b = new Bootstrap();
    b.group(getEventLoopGroup()).channel(NioSocketChannel.class)
            .handler(new HttpClientInitializer(sslCtx, httpClientHandler));
    return new ChannelFutureAndConnection(b.connect(host, port), httpClientHandler);
}

From source file:de.dfki.kiara.tcp.TcpBlockTransport.java

License:Open Source License

public ChannelFutureAndConnection connect(URI uri, Map<String, Object> settings) throws IOException {
    if (uri == null) {
        throw new NullPointerException("uri");
    }/*ww w. ja va  2  s.co m*/

    final String scheme = uri.getScheme();

    if (!"tcp".equalsIgnoreCase(scheme) && !"tcps".equalsIgnoreCase(scheme)) {
        throw new IllegalArgumentException("URI has neither tcp nor tcps scheme");
    }

    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = DEFAULT_TCP_PORT;
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = DEFAULT_TCPS_PORT;
        }
    }

    // Configure SSL context if necessary.
    final boolean ssl = "tcps".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    final TcpHandler tcpClientHandler = new TcpHandler(this, uri, HttpMethod.POST);
    Bootstrap b = new Bootstrap();
    b.group(getEventLoopGroup()).channel(NioSocketChannel.class)
            .handler(new TcpClientInitializer(sslCtx, tcpClientHandler));
    return new ChannelFutureAndConnection(b.connect(host, port), tcpClientHandler);
}

From source file:de.ocarthon.core.network.HttpClient.java

License:Apache License

public synchronized String postRequest(String query, List<Map.Entry<String, String>> postParameters,
        String filePostName, String fileName, ByteBuf fileData, String mime) {
    if (bootstrap == null) {
        setupBootstrap();//from   w w  w. j a va2 s. com
    }

    if (channel == null || forceReconnect) {
        ChannelFuture cf = bootstrap.connect(host, port);
        forceReconnect = false;
        cf.awaitUninterruptibly();
        channel = cf.channel();

        channel.pipeline().addLast("handler", new SimpleChannelInboundHandler<HttpObject>() {
            @Override
            protected void messageReceived(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
                if (msg instanceof HttpResponse) {
                    HttpResponse response = ((HttpResponse) msg);
                    String connection = (String) response.headers().get(HttpHeaderNames.CONNECTION);
                    if (connection != null && connection.equalsIgnoreCase(HttpHeaderValues.CLOSE.toString()))
                        forceReconnect = true;
                }

                if (msg instanceof HttpContent) {
                    HttpContent chunk = (HttpContent) msg;
                    String message = chunk.content().toString(CharsetUtil.UTF_8);

                    if (!message.isEmpty()) {
                        result[0] = message;

                        synchronized (result) {
                            result.notify();
                        }
                    }
                }
            }
        });
    }
    boolean isFileAttached = fileData != null && fileData.isReadable();
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            scheme + "://" + host + ":" + port + "/" + query);
    HttpPostRequestEncoder bodyReqEncoder;
    try {
        bodyReqEncoder = new HttpPostRequestEncoder(httpDataFactory, request, isFileAttached);

        for (Map.Entry<String, String> entry : postParameters) {
            bodyReqEncoder.addBodyAttribute(entry.getKey(), entry.getValue());
        }

        if (isFileAttached) {
            if (mime == null)
                mime = "application/octet-stream";

            MixedFileUpload mfu = new MixedFileUpload(filePostName, fileName, mime, "binary", null,
                    fileData.capacity(), DefaultHttpDataFactory.MINSIZE);
            mfu.addContent(fileData, true);
            bodyReqEncoder.addBodyHttpData(mfu);
        }

        HttpHeaders headers = request.headers();
        headers.set(HttpHeaderNames.HOST, host);
        headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
        headers.set(HttpHeaderNames.USER_AGENT, "OcarthonCore HttpClient");
        request = bodyReqEncoder.finalizeRequest();
    } catch (Exception e) {
        throw new NullPointerException("key or value is empty or null");
    }

    channel.write(request);

    if (bodyReqEncoder.isChunked()) {
        channel.write(bodyReqEncoder);
    }
    channel.flush();

    synchronized (result) {
        try {
            result.wait();
        } catch (InterruptedException e) {
            return null;
        }
    }

    return result[0];
}

From source file:discord4j.rest.route.Route.java

License:Open Source License

public static <T> Route<T> post(String uri, Class<T> responseType) {
    return new Route<>(HttpMethod.POST, uri, responseType);
}