com.digisky.outerproxy.testclient.HttpSnoopClient.java Source code

Java tutorial

Introduction

Here is the source code for com.digisky.outerproxy.testclient.HttpSnoopClient.java

Source

/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.digisky.outerproxy.testclient;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

import java.net.URI;

import testtimer.TestTimer;

/**
 * A simple HTTP client that prints out the content of the HTTP response to
 * {@link System#out} to test {@link HttpSnoopServer}.
 */
public final class HttpSnoopClient {

    static final String URL = System.getProperty("url", "http://192.168.61.216:8980/");
    static URI uri = null;

    public static void main(String[] args) throws Exception {
        uri = new URI(URL);
        String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
        int port = uri.getPort();
        if (port == -1) {
            if ("http".equalsIgnoreCase(scheme)) {
                port = 80;
            } else if ("https".equalsIgnoreCase(scheme)) {
                port = 443;
            }
        }

        if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
            System.err.println("Only HTTP(S) is supported.");
            return;
        }

        // 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.
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class).handler(new HttpSnoopClientInitializer(sslCtx));

            // Make the connection attempt.
            Channel ch = b.connect("192.168.60.163", 8089).sync().channel();
            TestTimer.add("main()::85");
            //test(ch, "username_register", R_username_register.j);
            //test(ch, "normal_login", R_normal_login.j_username);
            //test(ch, "normal_login", R_normal_login.j_openid);
            //test(ch, "normal_login", R_normal_login.j_telphone);
            //test(ch, "normal_login", R_normal_login.j_devid);
            //test(ch, "normal_login", R_normal_login.j_email);
            //test(ch, "email_bind", R_email_bind.j);
            //test(ch, "telphone_bind", R_telphone_bind.j);
            //test(ch, "req_telphone_vcode", R_req_telphone_vcode.j);
            //test(ch, "auth", R_auth.j);
            //test(ch, "telphone_register", R_register.j_telphone_register);
            //test(ch, "email_register", R_register.j_email_register);
            //test(ch, "req_register_mode", R_req_register_mode.j);
            //test(ch, "identify_exist", R_identify_exist.j);
            test(ch, "third_login", R_identify_exist.j);

        } finally {
            group.shutdownGracefully();
        }
    }

    public static void test(Channel ch, String uri, String sjson) {
        HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
        request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
        request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

        request.setMethod(HttpMethod.POST);
        request.setUri("/" + uri);
        HttpPostRequestEncoder bodyRequestEncoder = null;
        try {
            bodyRequestEncoder = new HttpPostRequestEncoder(request, false);
        } catch (ErrorDataEncoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // false => not multipart
          //***********************************************************************
        try {
            bodyRequestEncoder.addBodyAttribute("val", sjson);
        } catch (ErrorDataEncoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //***********************************************************************
        try {
            request = bodyRequestEncoder.finalizeRequest();
        } catch (ErrorDataEncoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ch.writeAndFlush(request);
        try {
            ch.closeFuture().sync();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void testWithEncode(Channel ch, String uri, String sjson) {
        HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
        request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
        request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

        request.setMethod(HttpMethod.POST);
        request.setUri("/" + uri);
        HttpPostRequestEncoder bodyRequestEncoder = null;
        try {
            bodyRequestEncoder = new HttpPostRequestEncoder(request, false);
        } catch (ErrorDataEncoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // false => not multipart
          //***********************************************************************
        ByteBuf b = Unpooled.buffer();
        b.writeBytes("{}".getBytes());
        try {
            bodyRequestEncoder.addBodyAttribute("val", sjson);
        } catch (ErrorDataEncoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //***********************************************************************
        try {
            request = bodyRequestEncoder.finalizeRequest();
        } catch (ErrorDataEncoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ch.writeAndFlush(request);
        try {
            ch.closeFuture().sync();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}