How to Login in a JSF page using java
我正在尝试登录 .jsf 内网页面,这是其中的一部分:
1 2 3 4 5 6 | <form method="POST" action="j_security_check" name="loginForm" id="loginForm"> <input name="j_username" type="text" class="textbox" maxlength="30"/> <input name="j_password" type="password" class="textbox" maxlength="30"/> <input type=submit value="Enter" class="button"> <input type=submit value="Exit" class="button"> </form> |
我已经在 java 中搜索并尝试过类似的东西,但没有成功,我得到了与结果相同的页面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | HttpPost post = new HttpPost("http://xxxx/login.jsf"); List <NameValuePair> parameters = new ArrayList <NameValuePair>(); parameters.add(new BasicNameValuePair("j_username","92232776")); parameters.add(new BasicNameValuePair("j_password","(7110oi14)")); UrlEncodedFormEntity sendentity; sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8); post.setEntity(sendentity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post, httpContext); System.out.print(convertInputStreamToString(response.getEntity().getContent())); |
(我正在使用 httpcomponents-client-4.2)
我需要做什么才能登录这个页面?我需要对代码按钮"发送"做些什么吗?
谢谢你们。
登录信息存储在会话中。会话由 cookie 支持。您需要跨请求维护 cookie。基本上,只要 cookie 有效,您就需要在同一会话中的每个后续 HTTP 请求中将检索到的
在 HttpClient 中,您需要准备一个
1 2 3 4 5 6 7 8 9 10 11 | HttpClient httpClient = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // ... HttpResponse response1 = httpClient.execute(method1, httpContext); // ... HttpResponse response2 = httpClient.execute(method2, httpContext); // ... |
请注意,此