Java 客户端的Netty HTTP身份验证

Java 客户端的Netty HTTP身份验证,java,netty,Java,Netty,看看我在下面写的测试代码。 使用纯java,我设置了一个验证器并进行URI调用,以获取一些xml数据并将其转换为一个对象 我编写下面的代码是为了测试(netty)与纯java(无管道)的性能 问题是,我不知道如何用hotpato或netty对我的请求进行身份验证,这两种方法的代码都是可以接受的,我只想测试性能差异(即,看看5秒内会执行多少个请求) publicstaticvoidmain(字符串[]args)引发异常{ setDefault(新的MyAuthenticator(“DummyUse

看看我在下面写的测试代码。 使用纯java,我设置了一个验证器并进行URI调用,以获取一些xml数据并将其转换为一个对象

我编写下面的代码是为了测试(netty)与纯java(无管道)的性能

问题是,我不知道如何用hotpato或netty对我的请求进行身份验证,这两种方法的代码都是可以接受的,我只想测试性能差异(即,看看5秒内会执行多少个请求)

publicstaticvoidmain(字符串[]args)引发异常{
setDefault(新的MyAuthenticator(“DummyUser”、“DummyPassword”);
int-timeToTestFor=5000;//5秒;
整数计数=0;
System.out.println(“开始时间”);
long starttime=System.currentTimeMillis();
做{
URL=新URL(
"http://example.com/rest/GetData.ashx?what=pizza&where=new%20york&visitorId=12345&sessionId=123456");
SearchResultsDocument doc=SearchResultsDocument.Factory.parse(url);
计数++;
}while(System.currentTimeMillis()-starttime>”+未来);

//如果响应>=200且,这里是仅对Netty使用基本身份验证的工作示例。使用Jetty作为需要基本身份验证的服务器进行测试

import java.net.InetSocketAddress;
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.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.base64.Base64;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpClientCodec;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.util.CharsetUtil;

public class BasicAuthTest {
private static final int PORT = 80;
private static final String USERNAME = "";
private static final String PASSWORD = "";
private static final String URI = "";
private static final String HOST = "";

public static void main(String[] args) {

    ClientBootstrap client = new ClientBootstrap(
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));

    client.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("codec", new HttpClientCodec());
            pipeline.addLast("aggregator", new HttpChunkAggregator(5242880));
            pipeline.addLast("authHandler", new ClientMessageHandler());
            return pipeline;
        }
    });

    DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, URI);

    request.addHeader(HttpHeaders.Names.HOST, HOST);

    String authString = USERNAME + ":" + PASSWORD;
    ChannelBuffer authChannelBuffer = ChannelBuffers.copiedBuffer(authString, CharsetUtil.UTF_8);
    ChannelBuffer encodedAuthChannelBuffer = Base64.encode(authChannelBuffer);
    request.addHeader(HttpHeaders.Names.AUTHORIZATION, encodedAuthChannelBuffer.toString(CharsetUtil.UTF_8));

    client.connect(new InetSocketAddress(HOST, PORT)).awaitUninterruptibly().getChannel()
            .write(request).awaitUninterruptibly();

}

public static class ClientMessageHandler extends SimpleChannelHandler {
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        e.getCause().printStackTrace();
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        HttpResponse httpResponse = (HttpResponse) e.getMessage();
        String json = httpResponse.getContent().toString(CharsetUtil.UTF_8);
        System.out.println(json);
    }
}

}

谢谢,我们非常欣赏带有管道的示例,但是有没有不使用管道的方法呢?哦,等等,您使用的是
waitunterruptibly()
我猜这意味着该示例没有使用管道?管道的使用是有原因的。如果您是指其中包含HttpClientCodec的管道。该示例非常粗糙,甚至没有停止执行。它只是说明了一个事实,即基本身份验证只是HttpRequest中的一个头。()多亏了这一点。它几乎对我起到了作用。编码提供了分块输出,通过添加一个参数:Base64.encode(authChannelBuffer,false)减轻了这一点。此外,头值只需更改一点:request.addHeader(HttpHeaders.Names.AUTHORIZATION,“Basic”+encodedAuthChannelBuffer.toString(CharsetUtil.utf8));
import java.net.InetSocketAddress;
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.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.base64.Base64;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpClientCodec;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.util.CharsetUtil;

public class BasicAuthTest {
private static final int PORT = 80;
private static final String USERNAME = "";
private static final String PASSWORD = "";
private static final String URI = "";
private static final String HOST = "";

public static void main(String[] args) {

    ClientBootstrap client = new ClientBootstrap(
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));

    client.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("codec", new HttpClientCodec());
            pipeline.addLast("aggregator", new HttpChunkAggregator(5242880));
            pipeline.addLast("authHandler", new ClientMessageHandler());
            return pipeline;
        }
    });

    DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, URI);

    request.addHeader(HttpHeaders.Names.HOST, HOST);

    String authString = USERNAME + ":" + PASSWORD;
    ChannelBuffer authChannelBuffer = ChannelBuffers.copiedBuffer(authString, CharsetUtil.UTF_8);
    ChannelBuffer encodedAuthChannelBuffer = Base64.encode(authChannelBuffer);
    request.addHeader(HttpHeaders.Names.AUTHORIZATION, encodedAuthChannelBuffer.toString(CharsetUtil.UTF_8));

    client.connect(new InetSocketAddress(HOST, PORT)).awaitUninterruptibly().getChannel()
            .write(request).awaitUninterruptibly();

}

public static class ClientMessageHandler extends SimpleChannelHandler {
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        e.getCause().printStackTrace();
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        HttpResponse httpResponse = (HttpResponse) e.getMessage();
        String json = httpResponse.getContent().toString(CharsetUtil.UTF_8);
        System.out.println(json);
    }
}

}