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;
}
}
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;
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件