发送帖子请求没有框架

时间:2013-03-18 11:20:10

标签: netty

public class ClientTestHttp {

    private final URI uri;
    private ChannelBuffer firstMessage;

    public ClientTestHttp(URI uri) {
        this.uri = uri;
    }

    public void run() {
        // Configure the client.
        ClientBootstrap bootstrap = new ClientBootstrap(
                new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool()));

        // Set up the event pipeline factory.
        bootstrap.setPipelineFactory(new ClientHttpPipeLineFactory(false));

        // Start the connection attempt.
        ChannelFuture future = bootstrap.connect(new InetSocketAddress("10.0.0.91", 8181));

        // Wait until the connection attempt succeeds or fails.
        Channel channel = future.awaitUninterruptibly().getChannel();
        if (!future.isSuccess()) {
            future.getCause().printStackTrace();
            bootstrap.releaseExternalResources();
                        return;
        }

        // Prepare the HTTP request.
        String sss =  uri.getRawPath();
        HttpRequest request = new DefaultHttpRequest(
                HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toASCIIString());
        request.setHeader(HttpHeaders.Names.HOST, "10.0.0.91");
        request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);


        // Set some example cookies.
        CookieEncoder httpCookieEncoder = new CookieEncoder(false);
        httpCookieEncoder.addCookie("my-cookie", "foo");
        httpCookieEncoder.addCookie("another-cookie", "bar");
        request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());

        request.setHeader("LAlala", "asdasdasd");

        // Send the HTTP request.
        request.addHeader("sdasd", "asdasdasdasd".getBytes());

        channel.write(request);
        System.out.println(channel.isOpen());

        // Wait for the server to close the connection.
        channel.getCloseFuture().awaitUninterruptibly();

        // Shut down executor threads to exit.
        bootstrap.releaseExternalResources();
    }

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

        URI uri = new URI("http://10.0.0.91:8181");
        new ClientTestHttp(uri).run();
    }
}

嘿。向netty发送帖子请求时遇到问题。这就是问题所在:

  

警告:下游出现意外异常。   java.lang.IllegalArgumentException:空文本   结论:java.lang.IllegalArgumentException:空文本

1 个答案:

答案 0 :(得分:0)

请参阅HttpUploadClient包中的org.jboss.netty.example.http.upload示例:

http://netty.io/3.6/xref/org/jboss/netty/example/http/upload/package-summary.html