com.xx_dev.speed_test.SpeedTestClient.java Source code

Java tutorial

Introduction

Here is the source code for com.xx_dev.speed_test.SpeedTestClient.java

Source

/*
 * Copyright (c) 2014 The APN-PROXY Project
 *
 * The APN-PROXY 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.xx_dev.speed_test;

import io.netty.bootstrap.Bootstrap;
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.*;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author xmx
 * @version $Id: com.xx_dev.speed_test.SpeedTestClient 2015-01-06 20:16 (xmx) Exp $
 */
public class SpeedTestClient {

    private static final Logger logger = Logger.getLogger(SpeedTestClient.class);

    public static void main(String[] args) {
        EventLoopGroup group = new NioEventLoopGroup();
        try {

            URL url = new URL(args[0]);

            boolean isSSL = false;
            String host = url.getHost();
            int port = 80;
            if (StringUtils.equals(url.getProtocol(), "https")) {
                port = 443;
                isSSL = true;
            }

            if (url.getPort() > 0) {
                port = url.getPort();
            }

            String path = url.getPath();
            if (StringUtils.isNotBlank(url.getQuery())) {
                path += "?" + url.getQuery();
            }

            PrintWriter resultPrintWriter = null;
            if (StringUtils.isNotBlank(args[1])) {
                String resultFile = args[1];

                resultPrintWriter = new PrintWriter(
                        new OutputStreamWriter(new FileOutputStream(resultFile, false), "UTF-8"));
            }

            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class)
                    .handler(new SpeedTestHttpClientChannelInitializer(isSSL, resultPrintWriter));

            // Make the connection attempt.
            Channel ch = b.connect(host, port).sync().channel();

            // Prepare the HTTP request.
            HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
            request.headers().set(HttpHeaders.Names.HOST, host);
            request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
            //request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

            // Send the HTTP request.
            ch.writeAndFlush(request);

            // Wait for the server to close the connection.
            ch.closeFuture().sync();

            if (resultPrintWriter != null) {
                resultPrintWriter.close();
            }

        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(), e);
        } catch (MalformedURLException e) {
            logger.error(e.getMessage(), e);
        } finally {
            group.shutdownGracefully();
        }
    }
}