Apache의 HttpCommons 3.1 패키지를 사용하여 웹 사이트에 로그인하고 있습니다.Java 응용 프로그램에서 프로그래밍 방식으로 웹 사이트에 로그인
웹 사이트는 j_username 및 j_password와 함께 j_security_check을 사용하는 양식의 JSP 페이지입니다. 브라우저에서
, 나는 할 것이다 다음과 같이의 login.jsp 페이지 사용자 이름과 암호를에
- 이동으로 유효한 사용자 이름과 암호, 나는 제한된 자원에 들어갈 것입니다. 간단하게 들리지만 문제는 Apache의 HttpCommons 3.1 패키지 (HttpClient, GetMethod, PostMethod 등)를 사용하는 간단한 Java 응용 프로그램을 통해이 작업을 수행하려고 시도한 결과이며 서버에서 302 리소스를 가져 왔습니다. 여기 내 소스 코드 : 나는 PHP 코드 (CURL)를 사용하여 동일한 서버에 대해 예를 들어 동일한 유형을 시도했다
String strURL = "http://host:port/app/login/LoginForm.jsp"; HttpState initialState = new HttpState(); HttpClient httpclient = new HttpClient(); httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); httpclient.setState(initialState); httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); GetMethod httpget = new GetMethod(strURL); int result = httpclient.executeMethod(httpget); System.out.println("Response status code: " + result); Cookie[] cookies = httpclient.getState().getCookies(); String res1 = httpget.getResponseBodyAsString(); httpget.releaseConnection(); PostMethod postMethod = new PostMethod("http://host:port/app/j_security_check"); NameValuePair[] postData = new NameValuePair[2]; postData[0] = new NameValuePair("j_username", "username"); postData[1] = new NameValuePair("j_password", "password"); postMethod.addParameters(postData); postMethod.addRequestHeader("Referer",strURL); for (int i = 0; i < cookies.length; i++) { initialState.addCookie(cookies[i]); } httpclient.setState(initialState); try { httpclient.executeMethod(postMethod); } catch (HttpException httpe) { System.err.print("HttpException"); System.err.println(httpe.getMessage()); httpe.printStackTrace(); } catch (IOException ioe) { System.err.print("IOException"); System.err.println(ioe.getMessage()); ioe.printStackTrace(); } String res2 = postMethod.getResponseBodyAsString(); postMethod.releaseConnection(); System.out.println(res2); } catch (IOException ex) { //print out ... }
그리고 내가 성공 후 기대 페이지로 전달합니까 로그인.
Java에서 작동하지 않는 이유는 무엇입니까?
감사합니다, - MK