Replaced HttpClient example with more generic wget style one
This commit is contained in:
parent
5c494dd045
commit
be88a53b3b
@ -1,92 +0,0 @@
|
|||||||
/*
|
|
||||||
* JBoss, Home of Professional Open Source
|
|
||||||
* Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
|
|
||||||
* by the @authors tag. See the copyright.txt in the distribution for a
|
|
||||||
* full listing of individual contributors.
|
|
||||||
*
|
|
||||||
* This is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU Lesser General Public License as
|
|
||||||
* published by the Free Software Foundation; either version 2.1 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This software is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
|
||||||
* License along with this software; if not, write to the Free
|
|
||||||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
||||||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
||||||
*/
|
|
||||||
package org.jboss.netty.example.http;
|
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffers;
|
|
||||||
import org.jboss.netty.channel.Channel;
|
|
||||||
import org.jboss.netty.channel.ChannelFactory;
|
|
||||||
import org.jboss.netty.channel.ChannelFuture;
|
|
||||||
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|
||||||
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpMethod;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpRequest;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpVersion;
|
|
||||||
import org.jboss.netty.handler.codec.http.QueryStringEncoder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
|
||||||
*/
|
|
||||||
public class HttpClient {
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
// Parse options.
|
|
||||||
String host = "localhost";
|
|
||||||
int port = 8080;
|
|
||||||
|
|
||||||
// Configure the client.
|
|
||||||
ChannelFactory factory =
|
|
||||||
new NioClientSocketChannelFactory(
|
|
||||||
Executors.newCachedThreadPool(),
|
|
||||||
Executors.newCachedThreadPool());
|
|
||||||
|
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(factory);
|
|
||||||
HttpPipelineFactory handler = new HttpPipelineFactory(new HttpResponseHandler());
|
|
||||||
bootstrap.setPipelineFactory(handler);
|
|
||||||
bootstrap.setOption("tcpNoDelay", true);
|
|
||||||
bootstrap.setOption("keepAlive", true);
|
|
||||||
// Start the connection attempt.
|
|
||||||
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
|
|
||||||
|
|
||||||
// Wait until the connection attempt succeeds or fails.
|
|
||||||
Channel channel = future.awaitUninterruptibly().getChannel();
|
|
||||||
if (!future.isSuccess()) {
|
|
||||||
future.getCause().printStackTrace();
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
String message = "It's Hello From me";
|
|
||||||
ChannelBuffer buf = ChannelBuffers.wrappedBuffer(message.getBytes());
|
|
||||||
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, new URI("/netty/"));
|
|
||||||
request.addHeader(HttpHeaders.HOST, host);
|
|
||||||
request.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buf.writerIndex()));
|
|
||||||
request.setContent(buf);
|
|
||||||
ChannelFuture lastWriteFuture = channel.write(request);
|
|
||||||
buf = ChannelBuffers.wrappedBuffer(message.getBytes());
|
|
||||||
QueryStringEncoder queryStringEncoder = new QueryStringEncoder("/netty/");
|
|
||||||
queryStringEncoder.addParam("testparam", "hey ho");
|
|
||||||
queryStringEncoder.addParam("testparam2", "hey ho again");
|
|
||||||
request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, queryStringEncoder.toUri());
|
|
||||||
request.addHeader(HttpHeaders.HOST, host);
|
|
||||||
request.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buf.writerIndex()));
|
|
||||||
request.setContent(buf);
|
|
||||||
lastWriteFuture = channel.write(request);
|
|
||||||
lastWriteFuture.awaitUninterruptibly();
|
|
||||||
}
|
|
||||||
}
|
|
@ -33,10 +33,10 @@ import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
|||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
*/
|
*/
|
||||||
public class HttpPipelineFactory implements ChannelPipelineFactory {
|
public class HttpClientPipelineFactory implements ChannelPipelineFactory {
|
||||||
private final ChannelHandler handler;
|
private final ChannelHandler handler;
|
||||||
|
|
||||||
public HttpPipelineFactory(HttpResponseHandler handler) {
|
public HttpClientPipelineFactory(HttpResponseHandler handler) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
@ -22,6 +22,7 @@
|
|||||||
package org.jboss.netty.example.http;
|
package org.jboss.netty.example.http;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.URI;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
import org.jboss.netty.bootstrap.ClientBootstrap;
|
||||||
@ -34,18 +35,33 @@ import org.jboss.netty.handler.codec.http.HttpHeaders;
|
|||||||
import org.jboss.netty.handler.codec.http.HttpMethod;
|
import org.jboss.netty.handler.codec.http.HttpMethod;
|
||||||
import org.jboss.netty.handler.codec.http.HttpRequest;
|
import org.jboss.netty.handler.codec.http.HttpRequest;
|
||||||
import org.jboss.netty.handler.codec.http.HttpVersion;
|
import org.jboss.netty.handler.codec.http.HttpVersion;
|
||||||
import org.jboss.netty.handler.codec.http.QueryStringEncoder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Andy Taylor (andy.taylor@jboss.org)
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
||||||
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
*/
|
*/
|
||||||
public class HttpWebserverClient {
|
public class HttpWebserverClient {
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
// Parse options.
|
public static void main(String[] args) throws Exception {
|
||||||
String host = "www.jboss.org";
|
if (args.length != 1) {
|
||||||
int port = 80;
|
System.err.println(
|
||||||
|
"Usage: " + HttpWebserverClient.class.getSimpleName() +
|
||||||
|
" <URL>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
URI uri = new URI(args[0]);
|
||||||
|
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
||||||
|
String host = uri.getHost() == null? "localhost" : uri.getHost();
|
||||||
|
int port = uri.getPort() == -1? 80 : uri.getPort();
|
||||||
|
|
||||||
|
if (!scheme.equals("http")) {
|
||||||
|
// We can actually support HTTPS fairly easily by inserting
|
||||||
|
// an SslHandler to the pipeline - left as an exercise.
|
||||||
|
System.err.println("Only HTTP is supported.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Configure the client.
|
// Configure the client.
|
||||||
ChannelFactory factory =
|
ChannelFactory factory =
|
||||||
@ -54,8 +70,9 @@ public class HttpWebserverClient {
|
|||||||
Executors.newCachedThreadPool());
|
Executors.newCachedThreadPool());
|
||||||
|
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(factory);
|
ClientBootstrap bootstrap = new ClientBootstrap(factory);
|
||||||
HttpPipelineFactory handler = new HttpPipelineFactory(new HttpResponseHandler());
|
HttpClientPipelineFactory handler = new HttpClientPipelineFactory(new HttpResponseHandler());
|
||||||
bootstrap.setPipelineFactory(handler);
|
bootstrap.setPipelineFactory(handler);
|
||||||
|
|
||||||
// Start the connection attempt.
|
// Start the connection attempt.
|
||||||
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
|
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
|
||||||
|
|
||||||
@ -65,8 +82,9 @@ public class HttpWebserverClient {
|
|||||||
future.getCause().printStackTrace();
|
future.getCause().printStackTrace();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
QueryStringEncoder uriBuilder = new QueryStringEncoder("/netty/");
|
|
||||||
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, uriBuilder.toUri());
|
// Send the HTTP request.
|
||||||
|
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, uri);
|
||||||
request.addHeader(HttpHeaders.HOST, host);
|
request.addHeader(HttpHeaders.HOST, host);
|
||||||
ChannelFuture lastWriteFuture = channel.write(request);
|
ChannelFuture lastWriteFuture = channel.write(request);
|
||||||
lastWriteFuture.awaitUninterruptibly();
|
lastWriteFuture.awaitUninterruptibly();
|
||||||
|
Loading…
Reference in New Issue
Block a user