模仿url访问

简介:
模仿url访问
2007年11月23日 17:39

package com.bobaoo.util;

import java.io.*;
import java.net.*;
import java.util.*;

import com.bobaoo.data.*;

public class Tool {

    //不能创建实例
    private Tool() { }

    public static synchronized String loadUrl(String url) throws Exception
    {
        return loadUrl(url, null, "UTF-8");
    }

    //发请HTTP(METHOD:GET)请求,载入获得目标URL的内容
    public static String loadUrl(String url, Header[] headers, String encoding) throws Exception
    {
        int ch = -1;
        String shtml = "";
        URL oUrl = null;
        HttpURLConnection urlConn = null;
        BufferedInputStream bis = null;
        byte[] buf = new byte[256];
        ByteArrayOutputStream baos = null;
        headers = null == headers ? new Header[0] : headers;

        try
        {
            baos = new ByteArrayOutputStream(256);
            oUrl = new URL(url);
            urlConn = (HttpURLConnection)oUrl.openConnection();
            urlConn.setConnectTimeout(10000);
            urlConn.setRequestProperty("Accept", "*/*");
            urlConn.setRequestProperty("Referer", url);
            urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
            urlConn.setRequestProperty("Pragma", "no-cache");
            urlConn.setRequestProperty("Cache-Control", "no-cache");
            for (int i = 0, l = headers.length; i < l; i++)
            {
                urlConn.setRequestProperty(headers[i].name, headers[i].value);
            }
            bis = new BufferedInputStream(urlConn.getInputStream());
            while ((ch = bis.read(buf)) != -1)
            {
                baos.write(buf, 0, ch);
            }
            shtml = baos.toString(encoding);
            bis.close();
            urlConn.disconnect();
            baos.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            urlConn = null;
            oUrl = null;
            bis = null;
            baos = null;
        }
        return shtml;
    }

    //public static synchronized String loadUrl(String url, String encoding, HttpProxy proxy) throws Exception
    //{
    //    String shtml = "", line;
    //    URL oUrl = null;
    //    HttpURLConnection urlConn = null;
    //    DataInputStream reader = null;
    //    try
    //    {
    //        oUrl = new URL(url);
    //        if (null == proxy) urlConn = (HttpURLConnection)oUrl.openConnection();
    //        else urlConn = (HttpURLConnection)oUrl.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.ip, proxy.port)));
    //        urlConn.setDoInput(true);
    //        urlConn.setRequestProperty("Accept", "*/*");
    //        urlConn.setRequestProperty("Referer", url);
    //        urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
    //        urlConn.setRequestProperty("Pragma", "no-cache");
    //        urlConn.setRequestProperty("Cache-Control", "no-cache");
    //        reader = new DataInputStream(urlConn.getInputStream());
    //        while ((line = reader.readLine()) != null) shtml += new String(line.getBytes("ISO-8859-1"), encoding) + "/r/n";
    //        reader.close();
    //        urlConn.disconnect();
    //    }
    //    catch (Exception e)
    //    {
    //        throw e;
    //    }
    //    finally
    //    {
    //        if (null != reader) reader.close();
    //        urlConn = null;
    //        oUrl = null;
    //        reader = null;
    //    }
    //    return shtml;
    //}

    //获得日期字符串以作为日志文件名,一天一个
    public static String getDate()
    {
        String sDate = "";
        java.util.Date date = new java.util.Date();
        sDate = (date.getYear() + 1900) + "_" + (date.getMonth() + 1) + "_" + date.getDate();
        date = null;
        return sDate;
    }

    //获取GMT格式日期字符串如Fri, 09 Jul 2004 15:56:43 GMT
    public static String getGMTString(Date date)
    {
        date = null == date ? new Date() : date;
        String[] day = new String[] { "Sun" , "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
        String ret = day[date.getDay()] + ", " + date.toGMTString();
        date = null;
        day = null;
        return ret;
    }

    //重载记录日志的方法
    public static void log(String msg,String pos)
    {
        log(msg, pos, getDate());
    }

    //记录日志
    public static void log(String msg,String pos,String fname)
    {
        String path = "";
        File file = null;
        try
        {
            file = new File("log");
            if ((!file.exists()) || (!file.isDirectory())) file.mkdir();
            file = null;
            FileUtils.writeFile("log/" + fname + ".txt",
                "Time: " + new Date().toLocaleString() + "/r/nLocation: " + pos + "/r/nDescription: " + msg + "/r/n/r/n",
                true
            );
        }
        catch (Exception ex)
        {
            System.out.println(" Error on write log to file.");
            System.out.println("/tMessage: " + ex.getMessage());
        }
    }

    //从文件中读取配置信息
    public static HashMap loadConfig(String fname) throws Exception
    {
        HashMap info = new HashMap();

        int idx = -1;
        String text = null;
        FileInputStream fis = null;
        DataInputStream dis = null;
        try
        {
            fis = new FileInputStream(fname);
            dis = new DataInputStream(fis);
            while ((text = dis.readLine()) != null)
            {
                if (0 == text.length()) continue;
                if ('#' == text.charAt(0)) continue;
                idx = text.indexOf(" = ");
                if (0 > idx) continue;
                info.put(text.substring(0, idx), text.substring(idx + 3));
                text = null;
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            try { fis.close(); }catch (Exception e) { };
            try { dis.close(); }catch (Exception e) { };
            fis = null;
            dis = null;
        }
        return info;
    }

    //编码
    public static String escape(String src)
    {
        int i;
        char j;
        if (null == src) return "";
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length() * 6);
        for (i = 0; i < src.length(); i++)
        {
            j = src.charAt(i);
            if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
                tmp.append(j);
            else
                if (j < 256)
                {
                    tmp.append("%");
                    if (j < 16)
                        tmp.append("0");
                    tmp.append(Integer.toString(j, 16));
                }
                else
                {
                    tmp.append("%u");
                    tmp.append(Integer.toString(j, 16));
                }
        }
        return tmp.toString();
    }

    //解码
    public static String unescape(String src)
    {
        if (null == src) return "";
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length());
        int lastPos = 0, pos = 0;
        char ch;
        while (lastPos < src.length())
        {
            pos = src.indexOf("%", lastPos);
            if (pos == lastPos)
            {
                if (src.charAt(pos + 1) == 'u')
                {
                    ch = (char)Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
                    tmp.append(ch);
                    lastPos = pos + 6;

                }
                else
                {
                    ch = (char)Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
                    tmp.append(ch);
                    lastPos = pos + 3;
                }
            }
            else
            {
                if (pos == -1)
                {
                    tmp.append(src.substring(lastPos));
                    lastPos = src.length();
                }
                else
                {
                    tmp.append(src.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }
        return tmp.toString();
    }

    //IP -> 数字
    public static long ipToNumeric(String ip)
    {
        long ret = 0L;
        String[] parts = ip.split("//.");
        if (4 != parts.length) return 0;
        ret = Long.parseLong(parts[0], 10) * 256 * 256 * 256
                + Long.parseLong(parts[1], 10) * 256 * 256
                + Long.parseLong(parts[2], 10) * 256
                + Long.parseLong(parts[3], 10);
        parts = null;
        return ret;
    }
}

目录
相关文章
|
7月前
|
域名解析 网络协议 开发工具
阿里云DNS常见问题之访问重定向的url访问有问题如何解决
阿里云DNS(Domain Name System)服务是一个高可用和可扩展的云端DNS服务,用于将域名转换为IP地址,从而让用户能够通过域名访问云端资源。以下是一些关于阿里云DNS服务的常见问题合集:
|
7月前
|
网络协议 开发工具 Android开发
应用研发平台EMAS产品常见问题之接入httpdns后 访问重定向的url访问有问题如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
|
4月前
|
JavaScript Linux 应用服务中间件
【Azure 应用服务】FTP 部署 Vue 生成的静态文件至 Linux App Service 后,访问App Service URL依旧显示Azure默认页面问题
【Azure 应用服务】FTP 部署 Vue 生成的静态文件至 Linux App Service 后,访问App Service URL依旧显示Azure默认页面问题
|
4月前
|
网络协议
【Azure 应用服务】探索在Azure上设置禁止任何人访问App Service的默认域名(Default URL)
【Azure 应用服务】探索在Azure上设置禁止任何人访问App Service的默认域名(Default URL)
|
存储 NoSQL Redis
一个网站有很多页面url,做一个URL排行榜功能。排行根据url的访问次数pv排行
一个网站有很多页面url,做一个URL排行榜功能。排行根据url的访问次数pv排行
78 2
Springboot Http文件的访问 Url 转换 MultipartFile ,File 转 MultipartFile
Springboot Http文件的访问 Url 转换 MultipartFile ,File 转 MultipartFile
1008 0
|
域名解析
访问 URL 报错 500 Internal Privoxy Error
访问 URL 报错 500 Internal Privoxy Error
1233 0
|
JSON 前端开发 安全
ajax中实现访问url已阅即焚的解决方案(url动态参数、变量加密、常量不变、php加密解密、API访问验证方式)
ajax中实现访问url已阅即焚的解决方案(url动态参数、变量加密、常量不变、php加密解密、API访问验证方式)
286 0
|
前端开发 JavaScript 关系型数据库
宝塔设置PHP定时任务实战记录(定时任务、ajax异步刷新API、shell脚本、访问url)
宝塔设置PHP定时任务实战记录(定时任务、ajax异步刷新API、shell脚本、访问url)
753 0
|
测试技术
【解决方案 十】判断URL请求是否成功并检测访问效率
【解决方案 十】判断URL请求是否成功并检测访问效率
287 0