HttpClient sessionID获取与设置
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+"");
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+"");
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: JAVA中快速解析JSON对象里包含的JSON数组
- 下一篇:没有了