All Downloads are FREE. Search and download functionalities are using the official Maven repository.

link.jfire.baseutil.http.SimpleHttpTool Maven / Gradle / Ivy

The newest version!
package link.jfire.baseutil.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import link.jfire.baseutil.collection.ByteCache;
import link.jfire.baseutil.collection.StringCache;

public class SimpleHttpTool
{
    private static ThreadLocal   cacheLocal  = new ThreadLocal() {
                                                            protected ByteCache initialValue()
                                                            {
                                                                return new ByteCache();
                                                            }
                                                        };
    private static ThreadLocal stringLocal = new ThreadLocal() {
                                                            protected StringCache initialValue()
                                                            {
                                                                return new StringCache();
                                                            }
                                                        };
    
    /**
     * 对一个url进行get请求。并且设置请求的头部信息
     * 
     * @param url
     * @param charset
     * @param requestHeaders
     * @return
     */
    public static ByteCache get(String url, Charset charset, Map requestHeaders)
    {
        HttpURLConnection httpUrlConn;
        try
        {
            httpUrlConn = (HttpURLConnection) new URL(url).openConnection();
        }
        catch (IOException e1)
        {
            throw new RuntimeException(e1);
        }
        try
        {
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod("GET");
            if (requestHeaders != null)
            {
                for (Entry each : requestHeaders.entrySet())
                {
                    httpUrlConn.setRequestProperty(each.getKey(), each.getValue());
                }
            }
            httpUrlConn.connect();
            InputStream inputStream = httpUrlConn.getInputStream();
            ByteCache cache = cacheLocal.get();
            cache.clear();
            cache.ensureCapacity(1024);
            int readSum = -1;
            while ((readSum = inputStream.read(cache.getDirectArray(), cache.getCount(), 1000)) != -1)
            {
                cache.setCount(cache.getCount() + readSum);
                cache.ensureCapacity(1000 + cache.getCount());
            }
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            return cache;
        }
        catch (IOException e)
        {
            httpUrlConn.disconnect();
            throw new RuntimeException(e);
        }
    }
    
    public static String post(String url, Charset charset, Map params, Map headers)
    {
        HttpURLConnection httpUrlConn;
        try
        {
            httpUrlConn = (HttpURLConnection) new URL(url).openConnection();
        }
        catch (IOException e1)
        {
            throw new RuntimeException(e1);
        }
        try
        {
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod("POST");
            if (headers != null)
            {
                for (Entry each : headers.entrySet())
                {
                    httpUrlConn.setRequestProperty(each.getKey(), each.getValue());
                }
            }
            ByteCache cache = cacheLocal.get().clear();
            OutputStream outputStream = httpUrlConn.getOutputStream();
            if (params != null)
            {
                outputStream.write(buildParam(params).getBytes(charset));
            }
            InputStream inputStream = httpUrlConn.getInputStream();
            cache.clear();
            cache.ensureCapacity(1024);
            int readSum = -1;
            while ((readSum = inputStream.read(cache.getDirectArray(), cache.getCount(), 1000)) != -1)
            {
                cache.setCount(cache.getCount() + readSum);
                cache.ensureCapacity(1000);
            }
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            return cache.toString(charset);
        }
        catch (IOException e)
        {
            httpUrlConn.disconnect();
            throw new RuntimeException(e);
        }
    }
    
    private static String buildParam(Map map)
    {
        StringCache stringCache = stringLocal.get();
        stringCache.clear();
        boolean append = false;
        for (Entry each : map.entrySet())
        {
            if (each.getValue() != null)
            {
                stringCache.append(each.getKey()).append('=').append(each.getValue()).append('&');
                append = true;
            }
        }
        if (append)
        {
            stringCache.deleteLast();
        }
        return stringCache.toString();
    }
    
    /**
     * 向url地址post方式发送一段文本信息
     * 
     * @param url
     * @param postStr
     * @param charset
     * @return
     */
    public static String post(String url, String postStr, Charset charset)
    {
        HttpURLConnection httpUrlConn;
        try
        {
            httpUrlConn = (HttpURLConnection) new URL(url).openConnection();
        }
        catch (IOException e1)
        {
            throw new RuntimeException(e1);
        }
        try
        {
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod("POST");
            ByteCache cache = cacheLocal.get().clear();
            httpUrlConn.getOutputStream().write(postStr.getBytes(charset));
            httpUrlConn.getOutputStream().flush();
            InputStream inputStream = httpUrlConn.getInputStream();
            cache.clear();
            cache.ensureCapacity(1024);
            int readSum = -1;
            while ((readSum = inputStream.read(cache.getDirectArray(), cache.getCount(), 1000)) != -1)
            {
                cache.setCount(cache.getCount() + readSum);
                cache.ensureCapacity(1000);
            }
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            return cache.toString(charset);
        }
        catch (IOException e)
        {
            httpUrlConn.disconnect();
            throw new RuntimeException(e);
        }
    }
    
    public static String getIp()
    {
        try
        {
            String text = SimpleHttpTool.get("http://1111.ip138.com/ic.asp", Charset.forName("gb2312"), null).toString(Charset.forName("utf8"));
            int left = text.indexOf('[');
            int right = text.indexOf(']');
            return text.substring(left + 1, right);
        }
        catch (Exception e)
        {
            return null;
        }
    }
    
    public static String getPhoneInfo(String phone)
    {
        String text = SimpleHttpTool.get("http://m.ip138.com/mobile.asp?mobile=" + phone, Charset.forName("gb2312"), null).toString(Charset.forName("utf8"));
        int start = text.indexOf("区 号") + 26;
        int end = text.indexOf("", start);
        String areaCode = text.substring(start, end);
        start = text.indexOf("卡 类 型") + 28;
        end = text.indexOf("", start);
        String operator = text.substring(start, end).substring(0, 2);
        return areaCode + ',' + operator;
    }
    
    public static void main(String[] args)
    {
        System.out.println(getPhoneInfo("13705955910"));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy