android 保持session

public class HttpUtil {

    public static String httpGet(String url, String param)
            throws Exception {
        String result = "";
        if (param != null && !param.equals("")) {
            if (url.indexOf("?") < 0) {
                url += "?" + param;
            } else {
                url += "&" + param;
            }
        }
        HttpGet httpGet = new HttpGet(url);

        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setSoTimeout(httpParams, 30 * 1000);
        HttpConnectionParams.setConnectionTimeout(httpParams, 30 * 1000);
        HttpConnectionParams.setTcpNoDelay(httpParams, true);
        HttpConnectionParams.setSocketBufferSize(httpParams,
                8192);

        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setUserAgent(httpParams, "Chlient");

        HttpClient httpClient = new DefaultHttpClient(httpParams);
        httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
        if (SysConstant.cookie.length()>0) {
            httpGet.setHeader("Cookie", "JSESSIONID=" + SysConstant.cookie);
        }
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
                List<Cookie> cookies = ((DefaultHttpClient)httpClient).getCookieStore().getCookies();
                for(Cookie cookie : cookies){
                    if("JSESSIONID".equals(cookie.getName())){
                        SysConstant.cookie = cookie.getValue();
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            httpGet.abort();
            httpClient = null;
        }
        return result;
    }
    
    public static String post(String url, Map<String,String> param) {
        String result="";
        // POST方式
        HttpPost httppost = new HttpPost(url);
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setSoTimeout(httpParams, 30 * 1000);
        HttpConnectionParams.setConnectionTimeout(httpParams, 30 * 1000);
        HttpConnectionParams.setTcpNoDelay(httpParams, true);
        HttpConnectionParams.setSocketBufferSize(httpParams,
                8192);

        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setUserAgent(httpParams, "Chlient");
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        if (SysConstant.cookie.length()>0) {
            httppost.setHeader("Cookie", "JSESSIONID=" + SysConstant.cookie);
        }
        // 申明键值对集合
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> entry : param.entrySet()) {
            params.add(new BasicNameValuePair(entry.getKey().toString(),
                    entry.getValue().toString()));
        }
        // 具体流程
        try {
            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            //执行post请求体
            HttpResponse response = httpClient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == 200) {// 正常返回200状态码
                result = EntityUtils.toString(response.getEntity());
                List<Cookie> cookies = ((DefaultHttpClient)httpClient).getCookieStore().getCookies();
                for(Cookie cookie : cookies){
                    if("JSESSIONID".equals(cookie.getName())){
                        SysConstant.cookie = cookie.getValue();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值