您的位置:首页 > 移动开发 > Android开发

android 保持session

2015-06-10 11:59 555 查看
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;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android session