android 와 asp.net 서버 에서 session 을 공유 하 는 방법 에 대한 자세 한 설명
5273 단어 androidsession 공유asp.net
최근 에 업무 의 수요 로 인해 하나의 기능 을 실현 하려 면 문자 메 시 지 를 보 내 서 등록 해 야 합 니 다.지금 은 문자 인증 코드 를 서버 의 session 값 에 넣 으 려 고 합 니 다.클 라 이언 트 가 문 자 를 받 고 문자 메 시 지 를 제출 할 때 asp.net 서버 에서 판단 합 니 다.그러면 이 session 을 어떻게 공유 하 는 지 안 드 로 이 드 클 라 이언 트 에 몇 줄 의 코드 를 추가 해 야 합 니 다.
실현 방법
아래 와 같이 조작 하 다.첫 번 째 데이터 요청 시 이 쿠키 의 이름 을 가 져 오고 이 쿠키 의 값 을 가 져 옵 니 다.이것 은 sessionid 의 값 이 고 정적 변수 에 저 장 된 다음 에 두 번 째 요청 데 이 터 를 가 져 올 때 이 sessionid 를 쿠키 에 함께 넣 어 서버 에 보 냅 니 다.서비스 기 는 이 sessionid 를 통 해 그 클 라 이언 트 가 요청 한 데 이 터 를 식별 합 니 다.asp.net 에서 이 sessionid 의 이름 은 ASP.NET 입 니 다.SessionId,물론 프로그램 에서 가 져 올 수 있 습 니 다.
다음 코드:
// sessionid
/* cookieStore */
List<Cookie> cookies = cookieStore.getCookies();
for(int i=0;i<cookies.size();i++){
String sessionid = cookies.get(i).getName(); sessionid, ASP.NET_SessionId
}
sessionid 값 가 져 오기
for(int i=0;i<cookies.size();i++){
if("ASP.NET_SessionId".equals(cookies.get(i).getName())){
JSESSIONID = cookies.get(i).getValue(); // sessionid
break;
}
}
완전한 httputils 코드 는 다음 과 같 습 니 다:
public static DefaultHttpClient httpClient = null;
private static String JSESSIONID; // , sessionID
public static String getRequest(String url)
{
httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try
{
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);//
HttpConnectionParams.setSoTimeout(params, 15000);//
get.setParams(params);
get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
if(null != JSESSIONID){
get.setHeader("Cookie", "ASP.NET_SessionId="+JSESSIONID);
}
// , HttpResponse
HttpResponse httpResponse = httpClient.execute(get);
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
//
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
}
catch (ClientProtocolException e)
{
return null;
}
catch (IOException e)
{
return null;
}
return null;
}
public static String postRequest(String url, HashMap<String, String> rawParams) throws Exception
{
httpClient = new DefaultHttpClient();
// POST
HttpPost post = new HttpPost(url);
HttpParams cparams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(cparams, 10000);//
HttpConnectionParams.setSoTimeout(cparams, 15000);//
post.setParams(cparams);
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
if(null != JSESSIONID){
post.setHeader("Cookie", "ASP.NET_SessionId="+JSESSIONID);
}
// NameValuePair , BasicNameValuePair
// add NameValuePair
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
for(String key : rawParams.keySet())
{
//
params.add(new BasicNameValuePair(key, rawParams.get(key)));
}
//post
HttpEntity httpEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// httpRequest
post.setEntity(httpEntity);
// POST
HttpResponse httpResponse = null;
try
{
httpResponse = httpClient.execute(post);
}
catch(Exception ex)
{
String ee = ex.getMessage();
}
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
/* cookieStore
ASP.NET_SessionId 。
*/
CookieStore cookieStore = httpClient.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
for(int i=0;i<cookies.size();i++){
if("ASP.NET_SessionId".equals(cookies.get(i).getName())){
JSESSIONID = cookies.get(i).getValue();
break;
}
}
return result;
}
return null;
}
총결산
이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.