HttpClient sessionID获取与设置

来源:互联网 发布:淘宝助手上传失败 编辑:程序博客网 时间:2024/05/24 07:18
 public static final String operatorHttpPost(String urlStr) throws Exception{
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(urlStr);
        if(null != sessionId){
             httpPost.setHeader("Cookie", "JSESSIONID=" + sessionId);
             Log.i("cookie", sessionId+"");
        }
        //new UrlEncodedFormEntity(nameValuePair)
        HttpResponse response =  client.execute(httpPost);
        
        StatusLine statusLine = response.getStatusLine();
        String respon = null;
        int statuscode=statusLine.getStatusCode();
        if(statuscode==200){
            cookieStore = ((AbstractHttpClient) client).getCookieStore();
            HttpEntity entity=response.getEntity();
            InputStream content=entity.getContent();
            respon = getResponse(content);
            
            List cookies = cookieStore.getCookies();
                 for (int i = 0; i < cookies.size(); i++) {
                     //这里是读取Cookie['JSPSESSID']的值存在静态变量中,保证每次都是同一个值
                     if ("JSESSIONID".equals(cookies.get(i).getName())) {
                             sessionId = cookies.get(i).getValue();
                             Log.i("cookie", sessionId+"");
                         break;
                     }
            }
        }
        return respon;
    }
0 0
原创粉丝点击