Quantcast
Channel: BlogJava-方枪枪的java世界-文章分类-Socket
Viewing all articles
Browse latest Browse all 7

01 一个简单的Http客户端_SimpleHttpClient(Socket)

$
0
0

package com.tianhe.ot.http.bak;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import com.tianhe.frm.http.server.Constant;
import com.tianhe.frm.utils.SocketUtil;

public class SimpleHttpClientSocket implements Runnable
{
    private ResUrlBean resUrlBean;
   
    private String resName;
   
    private String resUrl;
   
    public void setResUrlBean(ResUrlBean resUrlBean)
    {
        this.resUrlBean = resUrlBean;
    }
   
    public void setResName(String resName)
    {
        this.resName = resName;
    }
   
    public void setResUrl(String resUrl)
    {
        this.resUrl = resUrl;
    }
   
    public SimpleHttpClientSocket(ResUrlBean resUrlBean, String resUrl, String resName)
    {
        this.resUrlBean = resUrlBean;
        this.resName = resName;
        this.resUrl = resUrl;
    }
   
    public void run()
    {
        long beginTime = System.currentTimeMillis();
       
        String vsTemp;
       
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
       
        BufferedReader in = null;
       
        int resSize = 0;
       
        try
        {
            socket = createSocket(resUrlBean.getHost(), resUrlBean.getPort());
            is = socket.getInputStream();
            os = socket.getOutputStream();
           
            resUrl = "http://www.zhuoku.com/zhuomianbizhi/show-fengjingou/20120412001426(80).htm#turn";
           
            // ////////////////////////////////////////////////////////////////////////
            // 鍙戦?璇锋眰
            StringBuffer sb = new StringBuffer();
            appendLn(sb, "GET " + resUrl + " HTTP/1.1");
            appendLn(sb,
                    "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            appendLn(sb, "Accept-Language: zh-cn");
            appendLn(sb, "UA-CPU: x86");
            appendLn(sb, "Accept-Encoding: gzip, deflate");
            appendLn(sb,
                    "Operator-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)");
            appendLn(sb, "Host: " + resUrlBean.getHost());
            appendLn(sb, "Connection: Keep-Alive");
            appendLn(sb);
            os.write(sb.toString().getBytes());
            os.flush();
           
            // ////////////////////////////////////////////////////////////////////////
            // 鎺ユ敹鍝嶅簲
            in = new BufferedReader(new InputStreamReader(is));
            int contentLength = 0;
            String line = in.readLine();
            while ((line = in.readLine()) != null)
            {
               
                if (line.startsWith(Constant.CONTENT_LENGTH))
                {
                    vsTemp = line.substring(Constant.CONTENT_LENGTH.length());
                    contentLength = Integer.valueOf(vsTemp);
                }
               
                System.out.println(line);
               
                if (line.equals(""))
                    break;
            }
           
            // 鍐欏叆鏂囦欢
            if (contentLength > 0)
            {
                File file = resUrlBean.getTargetFile(resName);
                if (!file.exists())
                {
                    file.createNewFile();
                }
               
                int len = -1;
                byte[] buff = new byte[8192];
                FileOutputStream fos = new FileOutputStream(file);
                while ((len = is.read(buff)) != -1)
                {
                    fos.write(buff, 0, len);
                    resSize = resSize + len;
                }
               
                fos.flush();
                fos.close();
                os.close();
                in.close();
            }
           
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            SocketUtil.close(is);
            SocketUtil.close(os);
            SocketUtil.close(socket);
        }
       
        long used = System.currentTimeMillis() - beginTime;
        System.out.println(resUrl + " succeed, used=" + used + ", resSize=" + resSize);
    }
   
    private StringBuffer appendLn(StringBuffer sb, String xxx)
    {
        sb.append(xxx).append("\r\n");
        return sb;
    }
   
    private StringBuffer appendLn(StringBuffer sb)
    {
        sb.append("\r\n");
        return sb;
    }
   
    private static String getAllUrl(String[] args)
    {
        String temp = "http://bizhi.zhuoku.com/2012/04/11/shanshui/Shanshui02.jpg";
        if (args != null && args.length > 0)
        {
            temp = args[0];
        }
        return temp;
    }
   
    private static String getTargetDir(String[] args)
    {
        String temp = "d:/wallpaper";
        if (args != null && args.length > 1)
        {
            temp = args[1];
        }
        return temp;
    }
   
    private static String getProxyHost(String[] args)
    {
        String temp = "";
        if (args != null && args.length > 2)
        {
            temp = args[2];
        }
        return temp;
    }
   
    private static int getProxyPort(String[] args)
    {
        int temp = 80;
        if (args != null && args.length > 3)
        {
            temp = Integer.valueOf(args[3]);
        }
        return temp;
    }
   
    private static ResUrlBean createResUrlBean(String proxyHost, int proxyPort, String allUrl, String targetDir)
    {
        ResUrlBean rub = null;
        if (proxyHost != null && (!proxyHost.trim().equals("")))
        {
            // for url use
            rub = new ResUrlBean(true);
            rub.setHost(proxyHost);
            rub.setPort(proxyPort);
        }
        else
       
        {
            // for socket use, not for url use
            rub = new ResUrlBean();
            rub.setHost(getHostFromUrl(allUrl));
            rub.setPort(getPortFromUrl(allUrl));
        }
        rub.setAllUrl(allUrl);
        rub.setTargetDir(targetDir);
       
        {
            int pos1 = -1;
            int pos2 = -1;
            int pos3 = -1;
           
            String ts = "";
           
            pos1 = allUrl.indexOf("://") + 3;
            pos2 = allUrl.indexOf("/", pos1) + 1;
            pos3 = allUrl.lastIndexOf("/");
            ts = allUrl.substring(pos2, pos3);
            rub.setRelativePath(ts);
        }
       
        rub.setBaseUrl(allUrl.substring(0, allUrl.lastIndexOf("/") + 1));
        rub.setSuffix(allUrl.substring(allUrl.length() - 4));
        rub.setStartIndex(1);
       
        {
            String ts = allUrl.substring(allUrl.lastIndexOf("/") + 1, allUrl.length() - 4);
            int pos = 0;
            char[] tc = ts.toCharArray();
            for (int i = tc.length - 1; i > 0; i--)
            {
                int ti = Integer.valueOf(tc[i]);
                if (ti < 48 || ti > 57)
                {
                    pos = i + 1;
                    break;
                }
            }
            rub.setPrefix(ts.substring(0, pos));
            rub.setEndIndex(Integer.valueOf(ts.substring(pos)));
            rub.setFormat("%0" + ts.substring(pos).length() + "d");
        }
       
        System.out.println("resUrlBean=" + rub.toString());
        return rub;
    }
   
    private static String getHostFromUrl(String allUrl)
    {
        // allUrl = "http://bizhi.zhuoku.com/2012/04/11/shanshui/Shanshui01.jpg";
        // allUrl = "http://bizhi.zhuoku.com:9090/2012/04/11/shanshui/Shanshui01.jpg";
       
        int pos1 = -1;
        int pos2 = -1;
        String ts = "";
       
        pos1 = allUrl.indexOf("://") + 3;
        pos2 = allUrl.indexOf("/", pos1);
       
        ts = allUrl.substring(pos1, pos2);
        pos1 = ts.indexOf(":");
        if (pos1 > 0)
        {
            ts = ts.substring(0, pos1);
        }
       
        // System.out.println("getHostFromUrl:" + ts);
        return ts;
    }
   
    private static int getPortFromUrl(String allUrl)
    {
        // allUrl = "http://bizhi.zhuoku.com/2012/04/11/shanshui/Shanshui01.jpg";
        // allUrl = "http://bizhi.zhuoku.com:9090/2012/04/11/shanshui/Shanshui01.jpg";
       
        int port = 80;
       
        int pos1 = -1;
        int pos2 = -1;
        String ts = "";
       
        pos1 = allUrl.indexOf("://") + 3;
        pos2 = allUrl.indexOf("/", pos1);
       
        ts = allUrl.substring(pos1, pos2);
        pos1 = ts.indexOf(":");
        if (pos1 > 0)
        {
            ts = ts.substring(pos1) + 1;
            port = Integer.valueOf(ts);
        }
       
        // System.out.println("getPortFromUrl:" + port);
        return port;
    }
   
    private static String createResName(ResUrlBean rub, int i)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(rub.getPrefix());
        String format = rub.getFormat();
        if (format != null && (!format.trim().equals("")))
        {
            sb.append(String.format(format, i));
        }
        sb.append(rub.getSuffix());
        return sb.toString();
    }
   
    private static SimpleHttpClientSocket createSimpleHttpClient(ResUrlBean rub, int i)
    {
        String resName = createResName(rub, i);
        String resUrl = rub.getBaseUrl() + resName;
        SimpleHttpClientSocket client = new SimpleHttpClientSocket(rub, resUrl, resName);
        return client;
    }
   
    // http://bizhi.zhuoku.com/2012/04/11/shanshui/Shanshui01.jpg
    public static void main(String[] args)
    {
        String allUrl = getAllUrl(args);
        System.out.println("allUrl=" + allUrl);
       
        String targetDir = getTargetDir(args);
        System.out.println("targetDir=" + targetDir);
       
        String proxyHost = getProxyHost(args);
        System.out.println("proxyHost=" + proxyHost);
       
        int proxyPort = getProxyPort(args);
        System.out.println("proxyPort=" + proxyPort);
       
        ResUrlBean rub = createResUrlBean(proxyHost, proxyPort, allUrl, targetDir);
       
        try
        {
            for (int i = rub.getStartIndex(); i <= rub.getEndIndex(); i++)
            {
                SimpleHttpClientSocket client = createSimpleHttpClient(rub, i);
                new Thread(client).start();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
       
    }
   
    private Socket createSocket(String proxyHost, int proxyPort) throws Exception
    {
        Socket socket = null;
        try
        {
            socket = new Socket(proxyHost, proxyPort);
        }
        catch (Exception e)
        {
            SocketUtil.close(socket);
            throw e;
        }
       
        if (socket == null)
        {
            System.exit(1);
        }
       
        return socket;
    }
}



秋天的山谷 2012-07-09 23:07 发表评论

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles



Latest Images