HttpClient封装

package com.exmyth.net;

public interface KeyLoadListener {
    public void onSuccess(byte[] key);

//    public void onFailure(int statusCode, String content);
}
package com.exmyth.net;

public abstract class LoadDataHandler {
    /**
     * 加载数据时调用
     */
    public void onStart() {};

    /**
     * 加载数据调用,得到缓存数据
     *
     * @param data
     */
//    protected void onLoadCaches(String data) {};

    /**
     * 当调用服务器接口成功获取数据时,调用这个方法
     *
     * @param data
     */
    public abstract void onSuccess(String data);

    /**
     * 当调用服务器接口获取数据失败时,调用这个方法
     *
     * @param statusCode
     *            出错原因
     * @param message
     *            出错原因描述
     */
    public void onFailure(int statusCode, String message) {
    };

    /**
     * 加载完成时调用
     */
    public void onFinish() {

    }

    protected void onSuccess(byte[] content) {

        this.onSuccess(new String(content));
    }

    protected void onFailure(int statusCode, byte[] content) {
        this.onFailure(statusCode,new String(content));
    }
}
package com.exmyth.net;
import org.apache.http.Header;

import com.loopj.android.http.AsyncHttpResponseHandler;

public class WPHttpResponseHandler extends AsyncHttpResponseHandler {
    private LoadDataHandler mHandler;  

    public WPHttpResponseHandler(LoadDataHandler mHandler) {
        this.mHandler = mHandler;
    }
    @Override
    public void onStart() {
        super.onStart();
        mHandler.onStart();
    }  

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] content, Throwable error) {
        // TODO Auto-generated method stub
        super.onFailure(statusCode, headers, content, error);
//        if (content != null && 0 < content.length ) {
            mHandler.onFailure(statusCode, content);
//        }
    }

    @Override
    public void onFinish() {
        super.onFinish();
        mHandler.onFinish();
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] content) {
        super.onSuccess(statusCode, headers, content);
        switch (statusCode) {
        case 200:
//            if (content != null && 0 < content.length ) {
                mHandler.onSuccess(content);
//            }
            break;
        case 401:
            mHandler.onFailure(401, "没有登录");

            break;
        case 403:
            mHandler.onFailure(403, "没有权限");
            break;
        default:
            break;
        }

    }
}  
package com.exmyth.utils;

import com.exmyth.net.KeyLoadListener;
import com.exmyth.net.LoadDataHandler;
import com.exmyth.net.WPHttpResponseHandler;

public class HttpUtils{

    private static String versionId = "24";
    private static String appVersion = "24";//当前的appVersion,如果版本过低,会有强制更新提示
    private static final String CLIENT_KEY_STORE_PASSWORD = "198345";
    private static Context mContext;
    private static AsyncHttpClient client =new AsyncHttpClient();    //实例话对象
    public static byte[] key = null;
    public static boolean encrypt = false;

    public HttpUtils(Context context){
        mContext = context;
        client.setTimeout(10000);   //设置链接超时,如果不设置,默认为10s
        PersistentCookieStore myCookieStore = new PersistentCookieStore(mContext);
         BasicClientCookie newCookie = new BasicClientCookie("name", CommonUtil.mac);
         newCookie.setVersion(1);
         newCookie.setDomain("mydomain.com");
         newCookie.setPath("/");
         myCookieStore.addCookie(newCookie);
         client.setCookieStore(myCookieStore);
            try {

                KeyStore keystore = KeyStore.getInstance("BKS");
                keystore.load(mContext.getResources().openRawResource(
                    R.raw.client), CLIENT_KEY_STORE_PASSWORD.toCharArray());

                KeyStore trustkeystore = KeyStore.getInstance("BKS");
                trustkeystore.load(mContext.getResources().openRawResource(
                    R.raw.server_trust), CLIENT_KEY_STORE_PASSWORD.toCharArray());

                SSLSocketFactory factory = new SSLSocketFactory(keystore, CLIENT_KEY_STORE_PASSWORD, trustkeystore);
                Scheme sch = new Scheme("https", factory, 443);
                client.getHttpClient().getConnectionManager().getSchemeRegistry().register(sch);
            }catch(Exception e){
                e.printStackTrace();
            }
    }

    public static void post(final String url, final Map<String, String> param,final LoadDataHandler callback)
    {
        if(encrypt && null != param && 0 < param.keySet().size()){
            if (key == null) {
                getKey(0, callback, new KeyLoadListener() {

                    @Override
                    public void onSuccess(byte[] key) {
                        doPost(0, url, key,param, callback);
                    }
                });
            }
            else{
                doPost(0, url, key,param, callback);
            }
        }else{
            doPost(0, url, null,param, callback);
        }
    }
    //3000    获取key的接口失败(onFailure)
    //4000    app问题,请求失败
    //5000    获取key的接口不能顺利请求(statusCode != 200)
    //5100    服务器端getKey异常
    //5200    服务器端返回结果不合法,或者APP解析结果异常
    //5300    从服务端获取的key是空的(TextUtils.isEmpty(key)==true)
    //6000    第一次请求获取key失败
    //7000    获取到key后,第二请求后仍然失效
    //8000    {success:false}正常aes加密/非加密请求,但是success为false
    //8100    第二次请求aes加密请求仍然失败
    //8200    服务器端返回结果不合法,或者APP解析结果异常
    private static void doPost(final int index, final String url,
            byte[] key, final Map<String, String> param , final LoadDataHandler callback) {

        if (1 < index) {
            callback.onFailure(7000, "获取到key后,第二请求后仍然失效");
        }

        RequestParams requestParams = new RequestParams();

        if(null != param && 0 < param.keySet().size()&& null != key && 0 < key.length){
            AESUtil aes = new AESUtil(key);
            for (Map.Entry<String, String> entry : param.entrySet()) {
                requestParams.put(entry.getKey(), defaultEncodeBase64String(aes, entry.getValue()));
            }
        }

        //无参数,不需要做aes加密
        requestParams.put("versionId", versionId);

        client.post(url, requestParams, new WPHttpResponseHandler(
            new LoadDataHandler() {

                @Override
                public void onSuccess(String data) {
                    try {
                        JSONObject result = new JSONObject(data);
                        if("true".equals(result.optString("success"))){
                            callback.onSuccess(data);
                        }
                        else{
                            //{"errMsg":"Aes Decode error","errorCode":"255","success":false}
                            if ("255".equals(result.optString("errorCode"))) {
                                if(0 < index){
                                    callback.onFailure(8100, "第二次请求aes加密请求仍然失败");
                                }
                                else{
                                    //第一次请求aes加密请求失败,就请求获取key继续调用接口
                                    getKey(0, callback,new KeyLoadListener() {

                                        @Override
                                        public void onSuccess(byte[] key) {
                                            doPost((index+1), url, key, param, callback);
                                        }
                                    });

                                }

                            }
                            else{
                                //{success:false}正常aes加密/非加密请求,但是success为false
                                callback.onFailure(8000, data);
                            }
                        }
                    } catch (JSONException e) {
//                        e.printStackTrace();
                        callback.onFailure(8200, data);
                    }
                }

                @Override
                public void onFailure(int statusCode, String message) {
                    super.onFailure(statusCode, message);
                    callback.onFailure(4000, message);
                }
            }));
    }

      //获得key
      private static void getKey(final int index, final LoadDataHandler callback, final KeyLoadListener listener) {
          if (0 < index) {
            callback.onFailure(6000, "第一次请求获取key失败");
        }
          else{
              client.post(RequestConfig.Key_URL, null,
                      new AsyncHttpResponseHandler(){

                  @Override
                  public void onSuccess(int statusCode,Header[] headers, byte[] content) {

                      if(200 == statusCode){
                          try {
                              JSONObject jsonObject = new JSONObject(new String(content));
                              String success = jsonObject.optString("success");
                              //{"data":"1234567890ABCDEF1234567890ABCDEF","success":true}
                              if("true".equals(success)){
                                  String data = jsonObject.optString("data");
                                  if (!TextUtils.isEmpty(data)) {
                                      key = Base64.decodeBase64(data.getBytes());
                                      listener.onSuccess(key);
                                  }
                                  else{
//                                      getKey((index + 1), callback,listener);
//                                      listener.onFailure(5300,"从服务端获取的key是空的(TextUtils.isEmpty(key)==true)");
                                      callback.onFailure(5300, "从服务端获取的key是空的(TextUtils.isEmpty(key)==true)");
                                  }
                              }
                              else{
                                  //{"errMsg":"errorCode","success":false}
                                  callback.onFailure(5100, "服务器端getKey异常");
                                  //getKey((index + 1), callback,listener);
                              }
                          } catch (JSONException e) {
//                              e.printStackTrace();
                              callback.onFailure(5200, "服务器端返回结果不合法,或者APP解析结果异常");
                          }
                      }
                      else{
                          callback.onFailure(5000, "获取key的接口不能顺利请求(statusCode != 200)");
                      }
                }

                  @Override
                public void onFailure(int statusCode, Header[] headers,
                        byte[] content, Throwable error) {
                    super.onFailure(statusCode, headers, content, error);
//                    getKey((index + 1), callback,listener);
                    callback.onFailure(3000, "获取key的接口失败(onFailure)");
                }
              });
        }
      }

    // 加密密码
    public static String md5(String str) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] byteArray = messageDigest.digest();
        StringBuffer md5StrBuff = new StringBuffer();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                md5StrBuff.append("0").append(
                        Integer.toHexString(0xFF & byteArray[i]));
            else
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return md5StrBuff.toString();
    }

    //获取默认请求参数字节
    private static String defaultEncodeBase64String(AESUtil aes, String param){
        //如果是加密请求,调动此方法
        return encodeBase64String(true,aes,param);
        //如果是非加密请求,调动此方法
//        return encodeBase64String(false,null,param);
    }

    private static String encodeBase64String(boolean encrypt,AESUtil aes, String param){
        if(null == param){
            return null;
        }
        if(encrypt){
            if(null == aes){
                if (null == key){
                    key = "123456789001234567891234567890CD".getBytes();
                }
                aes = new AESUtil(key);
            }
            return Base64.encodeBase64String(aes.aesEncrypt(param.getBytes()));
        }
//        return Base64.encodeBase64String(param.getBytes());
        return param;
    }

}
时间: 2024-09-29 21:04:10

HttpClient封装的相关文章

Cocos2d-x3.1 HttpClient封装与使用

1.首先给出封装后的工具类:HttpManager #include "cocos2d.h" #include "../cocos2d/extensions/cocos-ext.h" #include "network/HttpClient.h" USING_NS_CC; USING_NS_CC_EXT; using namespace network; class HTTPManagerDelegate { public: virtual vo

JavaWeb之抓包之旅(三) :HttpClient封装工具类

谈到httpClient相信大家都不陌生,网上也有一大推别人总结的.HttpClient是Apache Jakarta Common下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议. 详细请参考文档:HttpClient 我们在对数据进行请求的时候经常使用. 前不久在做一个百度地图定位的(通过GPS判断你在某个学校,但是并不是每个学校地图上都有,而且如何确定范围呢?) 类似于饿了么,我一直在想它为什么能定位到具体的某个宿舍呢

.Net Standard HttpClient封装Htt请求常用操作整理

一.常用Http操作 1.Get请求,有参数,无参数 2.Post 请求,有参数,无参数 3.文件简单下载 /// <summary> /// http协议请求方法封装 /// </summary> public class NetHelper { #region Get请求 /// <summary> /// get请求,无参 /// </summary> /// <param name="url"></param&g

httpclient 封装 http(get put post delete)请求

@SuppressWarnings("deprecation") public class HttpClientUtil { private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); private static final String ContentEncoding = "UTF-8"; private static final int SocketTimeout

HttpClient封装方法

//post请求 public static string PostRequest(string url, HttpContent data) { var handler = new HttpClientHandler() { UseCookies = false }; HttpClient client = new HttpClient(handler); var message = new HttpRequestMessage(HttpMethod.Post, url); message.C

HTTP基础与Android之(安卓与服务器通信)——使用HttpClient和HttpURLConnection

查看原文:http://blog.csdn.net/sinat_29912455/article/details/51122286 1客户端连接服务器实现内部的原理 GET方式和POST方式的差别 HTTP返回请求数据的三种方式 2使用HTTP协议访问网络 3HttpCient 简单来说用HttpClient发送请求接收响应都很简单只需要五大步骤即可要牢记 4DefaultHttpClient GET方式 POST方式 5Java中使用HTTPHttpURLConnection GET方式 PO

Android--Apache HttpClient

上一篇文章介绍了使用HttpURLConnection来完成对于HTTP协议的支持.现在介绍一个新的方式来访问Web站点,那就是HttpClient. HttpClient是Apache开源组织提供的一个开源的项目,从名字上就可以看出,它是一个简单的HTTP客户端(并不是浏览器),可以发送HTTP请求,接受HTTP响应.但是不会缓存服务器的响应,不能执行HTTP页面中签入嵌入的JS代码,自然也不会对页面内容进行任何解析.处理,这些都是需要开发人员来完成的. 现在Android已经成功集成了Htt

Android系列之网络(一)----使用HttpClient发送HTTP请求

[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4004983.html 联系方式:[email protected] 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是Internet上目前使用最广泛的应用层协议,它基

HttpClient+jsoup登录+解析 163邮箱

找了几个,只有这个靠谱,用的是httpclient4,另外还需要commons-lang和jsoup包 http://jsoup.org/ http://www.oschina.net/code/snippet_128625_12592?p=2 ------------------------------------------------------------ 如题: 支用用jsoup解析页面非常方便,当时jsoup做登录就比较麻烦,反正我不知道怎么做. HttpClient做登录比较方便因