2017-12-28 5 views
2

유지 : 그 requiest를 인식하지 못하는 WebService에이 부분이기 때문에 그 RC2 때마다 401HttpURLConnection의 세션에게 내가 서버에 평안한 서비스를 사용하여 수행 로그인 후 동일한 세션 유지하기 위해 노력하고

URL endpoint = new URL(getString(R.string.url_login)); 
// Create connection 
HttpURLConnection myConnection = (HttpURLConnection) endpoint.openConnection(); 
myConnection.setRequestMethod("POST"); 
myConnection.setRequestProperty("User-Agent", "my-rest-app-v0.1"); 
// Create the data 
String myData = "username=" + username + "&password=" + pwd; 
// Enable data writing 
myConnection.setDoOutput(true); 
// Write the data 
myConnection.getOutputStream().write(myData.getBytes()); 
rc = myConnection.getResponseCode(); 
if (rc == 200) { 
     String Cookie= myConnection.getHeaderField("Set-Cookie"); 
     URL endpoint2 = new URL(getString(R.string.url_checkUserType)); 
     HttpURLConnection myConnection2 = 
       (HttpURLConnection)endpoint2.openConnection(); 
     myConnection2.setRequestMethod("GET"); 
     myConnection2.setRequestProperty("User-Agent", "my-rest-app-v0.1"); 
     myConnection2.setRequestProperty("Cookie", Cookie); 
     rc2 = myConnection2.getResponseCode(); 
.... 
     }.... 

문제는이다 같은 세션의 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

해결책은 [Android 용 스프링 RESTful 서비스] (https://spring.io/guides/gs/consuming-rest-android/)를 사용하는 것이 었습니다. – AlexP

답변

0

서버 측에서

  1. 을 다음과 같이 할 수 있습니다 들어오는 요청은 "sessionId가"쿠키가있는 경우, 그것은 검사 :하지 않을 경우, 하나를 생성하고 응답을 반환을; 네,이 요청이 성공적으로 로그인 한 후, 알려진 세션 클라이언트 측에서
  2. 에 속하는 알고 있다면, 응답에서 "sessionId가"를 검색하고 다음과 같은 요청에서 설정

==

connection.setRequestProperty("Cookie", "JSESSIONID=" + URLEncoder.encode(jSessionId, "UTF-8")); 
+0

왜 jSessionId를 인코딩해야합니까? 변수'Cookie'는'JSESSIONID = FA4D1226F11D2EBCB85361D5D9CA703F;와 같은 문자열을 포함하고 있습니다. 경로 =/My_Server; HttpOnly' 당신은 똑같지 않습니까? – AlexP

+0

문자열을 application/x-www-form-urlencoded 형식으로 변환하는 것이므로 URL에 어떤 문자도 사용할 수 없습니다. –

+0

하지만 서버에서 반환하는 경우 허용해야합니까? 스프링 보안 (* 내가 서버 측에서 사용하는 방법)은 여전히 ​​그것을 거부합니다. – AlexP

관련 문제