zoukankan      html  css  js  c++  java
  • 一个android访问http资源的便捷工具类——HttpHelper

    HttpHelper.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    package com.newcj.net;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.*;
    import org.apache.http.util.ByteArrayBuffer;
     
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Handler;
    import android.util.Log;
     
    /**
     * 帮助你访问 http 资源的工具类
     *
     * @author <a href="mailto:newcj@qq.com">newcj</a>
     * @version 1.0 2010/5/9
     */
    public final class HttpHelper {
        public final static String TAG = "HttpHelper";
     
        private final static String CONTENT_TYPE = "application/x-www-form-urlencoded";
        private final static String ACCEPT = "*/*";
        private final static String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5";
     
        /**
         * 1024 byte
         */
        private final static int BUFFER_LENGTH = 1024;
     
        private String referer;
        private Cookies cookies;
        private int timeout = 300000;
     
        public HttpHelper() {
            cookies = new Cookies();
        }
     
        /**
         * 获取超时时间,毫秒单位,默认为300000毫秒即5分钟
         *
         * @return
         */
        public int getTimeout() {
            return timeout;
        }
     
        /**
         * 设置超时时间 ReadTimeOut 与 ConnectTimeout 均设置为该超时时间,毫秒单位
         *
         * @param timeout
         */
        public void setTimeout(int timeout) {
            this.timeout = timeout;
        }
     
        /**
         * 获取 Referer
         *
         * @return
         */
        public String getReferer() {
            return referer;
        }
     
        /**
         * 设置 Referer
         *
         * @return
         */
        public void setReferer(String referer) {
            this.referer = referer;
        }
     
        /**
         * 以GET方法新建一个线程获取网页,编码方式为 gb2312,超时或编码错误返回null
         *
         * @param strUrl
         *            网页URL地址
         * @param handler
         *            用于向发起本次调用的线程发送结果信息
         * @param what
         *            handler中的what标记
         */
        public void getHtmlByThread(String strUrl, Handler handler, int what) {
            getHtmlByThread(strUrl, null, false, "gb2312", handler, what);
        }
     
        /**
         * 以GET方法新建一个线程获取网页,超时或编码错误返回null
         *
         * @param strUrl
         *            网页URL地址
         * @param encoding
         *            编码方式
         * @param handler
         *            用于向发起本次调用的线程发送结果信息
         * @param what
         *            handler中的what标记
         */
        public void getHtmlByThread(String strUrl, String encoding,
                Handler handler, int what) {
            getHtmlByThread(strUrl, null, false, encoding, handler, what);
        }
     
        /**
         * 根据GET或POST方法新建一个线程获取网页,超时返回null
         *
         * @param strUrl
         *            网页URL地址
         * @param strPost
         *            POST 的数据
         * @param isPost
         *            是否 POST,true 则为POST ,false 则为 GET
         * @param encoding
         *            编码方式
         * @param handler
         *            用于向发起本次调用的线程发送结果信息
         * @param what
         *            handler中的what标记
         */
        public void getHtmlByThread(String strUrl, String strPost, boolean isPost,
                String encoding, Handler handler, int what) {
            if (handler == null)
                throw new NullPointerException("handler is null.");
     
            Thread t = new Thread(new Runner(strUrl, strPost, isPost, encoding,
                    handler, what, Runner.TYPE_HTML));
            t.setDaemon(true);
            t.start();
        }
     
        /**
         * 以GET方法获取网页,编码方式为 gb2312,超时或编码错误返回null
         *
         * @param strUrl
         *            网页URL地址
         * @return 返回网页的字符串
         */
        public String getHtml(String strUrl) {
            return getHtml(strUrl, null, false, "gb2312");
        }
     
        /**
         * 以GET方法获取网页,超时或编码错误返回null
         *
         * @param strUrl
         *            网页URL地址
         * @param encoding
         *            编码方式
         * @return 返回网页的字符串
         */
        public String getHtml(String strUrl, String encoding) {
            return getHtml(strUrl, null, false, encoding);
        }
     
        /**
         * 根据GET或POST方法获取网页,超时返回null
         *
         * @param strUrl
         *            网页URL地址
         * @param strPost
         *            POST 的数据
         * @param isPost
         *            是否 POST,true 则为POST ,false 则为 GET
         * @param encoding
         *            编码方式
         * @return 返回网页的字符串
         */
        public String getHtml(String strUrl, String strPost, boolean isPost,
                String encoding) {
            String ret = null;
            try {
                byte[] data = getHtmlBytes(strUrl, strPost, isPost, encoding);
                if (data != null)
                    ret = new String(data, encoding);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
            }
            return ret;
        }
     
        /**
         * 根据GET或POST方法获取网络数据,超时返回null
         *
         * @param strUrl
         *            网页URL地址
         * @param strPost
         *            POST 的数据
         * @param isPost
         *            是否POST,true则为POST,false则为 GET
         * @param encoding
         *            编码方式
         * @return 返回bytes
         */
        public byte[] getHtmlBytes(String strUrl, String strPost, boolean isPost,
                String encoding) {
            byte[] ret = null;
            HttpURLConnection httpCon = null;
            InputStream is = null;
            try {
                URL url = new URL(strUrl);
                httpCon = (HttpURLConnection) url.openConnection();
                httpCon.setReadTimeout(timeout);
                httpCon.setConnectTimeout(timeout);
                httpCon.setUseCaches(false);
                httpCon.setInstanceFollowRedirects(true);
                httpCon.setRequestProperty("Referer", referer);
                httpCon.setRequestProperty("Content-Type", CONTENT_TYPE);
                httpCon.setRequestProperty("Accept", ACCEPT);
                httpCon.setRequestProperty("User-Agent", USER_AGENT);
                httpCon.setRequestProperty("Cookie", cookies.toString());
     
                if (isPost) {
                    httpCon.setDoOutput(true);
                    httpCon.setRequestMethod("POST");
                    httpCon.connect();
     
                    OutputStream os = null;
                    try {
                        os = httpCon.getOutputStream();
                        os.write(URLEncoder.encode(strPost, encoding).getBytes());
                        os.flush();
                    } finally {
                        if (os != null)
                            os.close();
                    }
                }
     
                // 获取数据
                is = httpCon.getInputStream();
                ByteArrayBuffer baBuffer = null;
                byte[] buffer = new byte[BUFFER_LENGTH];
                int rNum = 0;
                // 若读取的数据长度小于 BUFFER_LENGTH,说明读取的
                // 内容小于 BUFFER_LENGTH 已经一次性读取完毕
                // 这个时候并不用创建 ByteArrayBuffer
                //
                // 以上并不适用
                // if ((rNum = is.read(buffer)) < BUFFER_LENGTH) {
                // ret = buffer;
                // } else {
                baBuffer = new ByteArrayBuffer(BUFFER_LENGTH << 1);
                // baBuffer.append(buffer, 0, BUFFER_LENGTH);
                while ((rNum = is.read(buffer)) != -1) {
                    baBuffer.append(buffer, 0, rNum);
                }
                ret = baBuffer.toByteArray();
                // }
     
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e(TAG, e.getMessage() + ":" + e.getCause());
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
     
                    }
                }
                // 更新 Cookie
                if (httpCon != null) {
                    cookies.putCookies(httpCon.getHeaderField("Set-Cookie"));
                    // 更新 referer
                    referer = strUrl;
                    httpCon.disconnect();
                }
            }
            return ret;
        }
     
        /**
         * 新建一个线程获取一张网页图片
         *
         * @param strUrl
         * @param handler
         *            用于向发起本次调用的线程发送结果信息
         * @param what
         *            handler中的what标记
         */
        public void getBitmapByThread(String strUrl, Handler handler, int what) {
            if (handler == null)
                throw new NullPointerException("handler is null.");
     
            Thread t = new Thread(new Runner(strUrl, null, false, null, handler,
                    what, Runner.TYPE_IMG));
            t.setDaemon(true);
            t.start();
        }
     
        /**
         * 获取一张网页图片
         *
         * @param strUrl
         *            网页图片的URL地址
         * @return
         */
        public Bitmap getBitmap(String strUrl) {
            byte[] data = getHtmlBytes(strUrl, null, false, null);
            return BitmapFactory.decodeByteArray(data, 0, data.length);
        }
     
        private class Runner implements Runnable {
            public final static int TYPE_HTML = 1;
            public final static int TYPE_IMG = 2;
     
            private String strUrl;
            private String strPost;
            private boolean isPost;
            private String encoding;
            private Handler handler;
            private int what;
            private int type;
     
            public Runner(String strUrl, String strPost, boolean isPost,
                    String encoding, Handler handler, int what, int type) {
                this.strUrl = strUrl;
                this.strPost = strPost;
                this.isPost = isPost;
                this.encoding = encoding;
                this.handler = handler;
                this.what = what;
                this.type = type;
            }
     
            @Override
            public void run() {
                // TODO Auto-generated method stub
                Object obj = null;
                switch (type) {
                case TYPE_HTML:
                    obj = getHtml(strUrl, strPost, isPost, encoding);
                    break;
                case TYPE_IMG:
                    obj = getBitmap(strUrl);
                    break;
                }
                synchronized (handler) {
                    handler.sendMessage(handler.obtainMessage(what, obj));
                }
            }
     
        }
    }

    Cookies.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    package com.newcj.net;
     
    import java.util.HashMap;
    import java.util.Map.Entry;
    import java.util.Set;
     
    /**
     * 非同步保存Cookie的键值
     *
     * @author SomeWind
     *
     */
    public class Cookies {
     
        private HashMap<String, String> hashMap;
     
        public Cookies() {
            hashMap = new HashMap<String, String>();
        }
     
        /**
         * 清除 Cookies 里面的所有 Cookie 记录
         */
        public void clear() {
            hashMap.clear();
        }
     
        /**
         * 根据 key 获取对应的 Cookie 值
         *
         * @param key
         *            要获取的 Cookie 值的 key
         * @return 如果不存在 key 则返回 null
         */
        public String getCookie(String key) {
            return hashMap.get(key);
        }
     
        /**
         * 在 Cookies 里设置一个 Cookie
         *
         * @param key
         *            要设置的 Cookie 的 key
         * @param value
         *            要设置的 Cookie 的 value
         */
        public void putCookie(String key, String value) {
            hashMap.put(key, value);
        }
     
        /**
         * 在 Cookies 里面设置传入的 cookies
         *
         * @param cookies
         */
        public void putCookies(String cookies) {
            if (cookies == null)
                return;
     
            String[] strCookies = cookies.split(";");
            for (int i = 0; i < strCookies.length; i++) {
                for (int j = 0; j < strCookies[i].length(); j++) {
                    if (strCookies[i].charAt(j) == '=') {
                        this.putCookie(
                                strCookies[i].substring(0, j),
                                strCookies[i].substring(j + 1,
                                        strCookies[i].length()));
                    }
                }
            }
        }
     
        /**
         * 获取 Cookies 的字符串
         */
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            if (hashMap.isEmpty())
                return "";
     
            Set<Entry<String, String>> set = hashMap.entrySet();
            StringBuilder sb = new StringBuilder(set.size() * 50);
            for (Entry<String, String> entry : set) {
                sb.append(String.format("%s=%s;", entry.getKey(), entry.getValue()));
            }
            sb.delete(sb.length() - 1, sb.length());
            return sb.toString();
        }
    }
  • 相关阅读:
    pigeon
    servlet
    Linux (centos6.5) 安装Node和pm2
    git 常用命令
    git本地仓库推送代码到远程仓库
    linux 服务器 磁盘空间查看清理
    阿里云 Kubenetes容器 时区相差8小时 设置环境变量
    推荐一个简易易懂的ElasticSearch 入门学习站
    ElasticSearch 设置索引mapping 文档类型,重置elastic密码
    linux 查看系统各项指标(资源,内存)
  • 原文地址:https://www.cnblogs.com/top5/p/2489743.html
Copyright © 2011-2022 走看看