Android Open Source - Common-Library Cookie Share From Project Back to project page Common-Library .
License The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code package com.morgan.library.snippet;
/ * f r o m w w w . j a v a 2 s . c o m * /
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import com.morgan.library.app.APPContext;
import com.morgan.library.utils.Logger;
/**
* ?????????Webview???????????cookie?????????cookie???
*
* @author Morgan.Ji
*
*/
public class CookieShare {
/**
* ?????????????????????cookie?????
*
* @param name
* @param pass
* @return
*/
public boolean login(String name, String pass) {
boolean result = false;
try {
String loginUrl = "http://10.1.1.157:8080/cas/login" ;
BasicCookieStore bcsc = new BasicCookieStore();
DefaultHttpClient client = new DefaultHttpClient();
client.setCookieStore(bcsc);
// get????????
HttpGet get = new HttpGet(loginUrl);
HttpResponse response = client.execute(get);
// ?body
String bodyAsString = EntityUtils.toString(response.getEntity());
// ?lt
String lt = bodyAsString.substring(
bodyAsString.indexOf("name=\"lt\"" ) + 17,
bodyAsString.indexOf("name=\"lt\"" ) + 56);
Logger.i("lt" , lt);
// ?excution
String execution = "e1s1" ;
// post????????
HttpPost post = new HttpPost(loginUrl);
// ???????
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username" , name));
nvps.add(new BasicNameValuePair("password" , pass));
nvps.add(new BasicNameValuePair("lt" , lt));
nvps.add(new BasicNameValuePair("execution" , execution));
nvps.add(new BasicNameValuePair("_eventId" , "submit" ));
post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8" ));
response = client.execute(post);
bodyAsString = EntityUtils.toString(response.getEntity());
Logger.i("cookie" , bcsc.getCookies().toString());
// Logger.i("login response", bodyAsString);
// ????????????cookie
if (bodyAsString.indexOf("id=\"msg\" class=\"success\"" ) > 0) {
result = true;
synCookies("http://10.1.1.175" , bcsc.getCookies());
// ?????api
get = new HttpGet(
"http://10.1.1.175:8088/ioc/restapi/emergencyEvent/getEventByReportName" );
response = client.execute(get);
bodyAsString = EntityUtils.toString(response.getEntity());
Logger.i("get list response" , bodyAsString);
}
} catch (Exception e) {
Logger.i("error" , e.getMessage());
}
return result;
}
/**
* ??????cookie
*
* @param url
* cookie???url,??url?????domain????(cookie??domain??????????????http://
* );
* @param cookieList
* cookie??
*/
public void synCookies(String url, List<Cookie> cookieList) {
String cookie = "" ;
if (cookieList.isEmpty()) {
return ;
}
CookieSyncManager.createInstance(APPContext.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
// ?????cookie??????????,???????
// cookieManager.removeSessionCookie();
for (int i = 0; i < cookieList.size(); i++) {
cookie = cookieList.get(i).getName() + "="
+ cookieList.get(i).getValue() + ";domain="
+ cookieList.get(i).getDomain();
cookieManager.setCookie(url, cookie);
}
CookieSyncManager.getInstance().sync();
}
}
Java Source Code List com.morgan.library.app.APPContext.java com.morgan.library.app.AppManager.java com.morgan.library.app.BaseActivity.java com.morgan.library.app.BaseFragmentActivity.java com.morgan.library.app.BaseListActivity.java com.morgan.library.app.BaseScreenShotActivity.java com.morgan.library.app.CommonApplication.java com.morgan.library.async.CustomAsyncTask.java com.morgan.library.async.Destroyable.java com.morgan.library.async.IFeedback.java com.morgan.library.async.TaskManager.java com.morgan.library.model.NetResult.java com.morgan.library.model.SpinnerItem.java com.morgan.library.model.WeatherType.java com.morgan.library.model.Weather.java com.morgan.library.net.ApiClient.java com.morgan.library.net.ApiManager.java com.morgan.library.net.ApiUrl.java com.morgan.library.net.IApiClient.java com.morgan.library.net.JsonUtils.java com.morgan.library.net.MockApiClient.java com.morgan.library.service.LocationManager.java com.morgan.library.service.WeatherManager.java com.morgan.library.snippet.CookieShare.java com.morgan.library.snippet.CustomService.java com.morgan.library.snippet.ExceptionCatcher.java com.morgan.library.snippet.ProxyUsage.java com.morgan.library.snippet.SocketClient.java com.morgan.library.snippet.SocketServer.java com.morgan.library.snippet.UDPClient.java com.morgan.library.snippet.UDPServer.java com.morgan.library.snippet.XMLAnalyze.java com.morgan.library.task.GetWeatherTask.java com.morgan.library.utils.AppUtils.java com.morgan.library.utils.DateUtils.java com.morgan.library.utils.DensityUtils.java com.morgan.library.utils.FileUtils.java com.morgan.library.utils.GB2Alpha.java com.morgan.library.utils.HttpClientUtil.java com.morgan.library.utils.HttpClientUtils.java com.morgan.library.utils.HttpURLUtil.java com.morgan.library.utils.ImageUtils.java com.morgan.library.utils.KeyBoardUtils.java com.morgan.library.utils.LocationUtils.java com.morgan.library.utils.Logger.java com.morgan.library.utils.NetUtils.java com.morgan.library.utils.PhoneUtils.java com.morgan.library.utils.SDCardUtils.java com.morgan.library.utils.StrUtils.java com.morgan.library.widget.AutoHideMenuWidget.java com.morgan.library.widget.BadgeView.java com.morgan.library.widget.CityPickerWidget.java com.morgan.library.widget.CustomGridView.java com.morgan.library.widget.CustomListView.java com.morgan.library.widget.CustomToast.java com.morgan.library.widget.CustomViewPager.java com.morgan.library.widget.DatePickerWidget.java com.morgan.library.widget.DirectionScrollView.java com.morgan.library.widget.FallBallView.java com.morgan.library.widget.HeightPickerWidget.java com.morgan.library.widget.InterceptPressLayout.java com.morgan.library.widget.ItalicTextView.java com.morgan.library.widget.PopupMenuWidget.java com.morgan.library.widget.PullToRefreshListView.java com.morgan.library.widget.ScreenShotView.java com.morgan.library.widget.SexPickerWidget.java com.morgan.library.widget.SlideUpOpenWidget.java com.morgan.library.widget.SwipeListView.java com.morgan.library.widget.TimePickerWidget.java com.morgan.library.widget.TouchZoomImageView.java com.morgan.library.widget.WaitScreenWidget.java com.morgan.library.widget.WebViewDialog.java com.morgan.library.widget.WeightPickerWidget.java com.morgan.library.widget.calendar.CalendarGridView.java com.morgan.library.widget.calendar.CalendarScrollView.java com.morgan.library.widget.calendar.CalendarViewFlipper.java com.morgan.library.widget.calendar.CalendarWidget.java com.morgan.library.widget.numberpicker.NumberPicker.java com.morgan.library.widget.numberpicker.Scroller.java com.morgan.library.widget.slidemenu.ScrollDetectorFactory.java com.morgan.library.widget.slidemenu.ScrollDetectors.java com.morgan.library.widget.slidemenu.SlideMenu.java