Cam*_*rde 3 java post login playframework
我使用Java和Playframework 1.2.3创建了一个Web爬虫.现在,我想抓取一些受经典登录/密码表单保护的网页.
事实上,这就像做这个游戏测试:
@Test
public void someTestOfASecuredAction() {
Map<String, String> loginUserParams = new HashMap<String, String>();
loginUserParams.put("username", "admin");
loginUserParams.put("password", "admin");
Response loginResponse = POST("/login", loginUserParams);
Request request = newRequest();
request.cookies = loginResponse.cookies; // this makes the request authenticated
request.url = "/some/secured/action";
request.method = "POST";
request.params.put("someparam", "somevalue");
Response response = makeRequest(request);
assertIsOk(response); // Passes!
}
Run Code Online (Sandbox Code Playgroud)
但不是通过播放生成的网站,而是与外部网站.
所以,我设法使用播放网络服务器来做到这一点:
Map<String,String> params = new HashMap<String, String>();
params.put( "telecom_username",
Play.configuration.getProperty("telecom.access.user") );
params.put( "telecom_password",
Play.configuration.getProperty("telecom.access.pass") );
HttpResponse response = WS.url(loginUrl)
.setParameters(params)
.followRedirects(true)
.post();
Run Code Online (Sandbox Code Playgroud)
当我这样做时,如果我查看response.getString(),我发现在继续之前设置了cookie的重定向页面,但是,如果我得到一个受保护的页面,我仍然没有登录.它就像是cookie从未设置过,并且HttpResponse对象没有任何与cookie相关的功能,就像之前测试代码中的响应一样.
我也尝试过ws.url()上的authenticate()方法,但它也不起作用.
我真的不知道我正在尝试做什么是通过使用播放网络服务器,但我可以使用这个^^的帮助
非常感谢 !
好的,我找到了一种方法,但我做得很难,这里是:
首先,我们存储会话cookie的GET,请考虑我正在使用的字符集,并且我知道我正在寻找的cookie的名称,您可以将它们全部存储起来.此外,您可能想要加密它们.
HttpResponse wsResponse = WS.url(comercialYComunUrl).get();
String responseString = wsResponse.getString("ISO-8859-1");
if (wsResponse.getStatus() == 200) {
List<Header> headers = wsResponse.getHeaders();
// get all the cookies
List<String> cookies = new ArrayList<String>();
for (Header header: headers) {
if (header.name.equals("Set-Cookie")) {
cookies = header.values;
}
}
// look for the session cookies
String sessionCookie = "";
for (String cookie : cookies) {
if (cookie.toUpperCase().contains("ASPSESSIONID")) {
sessionCookie = cookie.split(";", 2)[0];
}
}
// store it on the session
session.put("COOKIE", sessionCookie);
}
Run Code Online (Sandbox Code Playgroud)
现在邮报:
String url = "http://www.url.com/";
String charset = "ISO-8859-1";
String param1 = "value1";
String param2 = "value2";
String param3 = "value3";
String query = String.format("param1=%s¶m2=%s¶m2=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset),
URLEncoder.encode(param3, charset));
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=" + charset);
connection.addRequestProperty("Cookie", session.get("COOKIE"));
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
InputStream responseStream = connection.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(responseStream, writer);
String response = writer.toString();
Run Code Online (Sandbox Code Playgroud)
这对我有用,这是我的来源,这是一个很棒的帖子: 如何使用java.net.URLConnection来触发和处理HTTP请求?
- - - - - - - - - - - - - - 编辑 - - - - - - - - - - - ----------------
好吧,我对答案都不满意,所以我找到了一个更好的方法:
String url = "http://www.url.com/";
String charset = "ISO-8859-1";
String param1 = "value1";
String param2 = "value2";
String param3 = "value3";
WSRequest wsRequest = WS.url(url);
wsRequest.parameters.put("param1", param1);
wsRequest.parameters.put("param2", param2);
wsRequest.parameters.put("param3", param3);
wsRequest.headers.put("Cookie", session.get("COOKIE"));
HttpResponse wsResponse = wsRequest.post();
String responseString = wsResponse.getString(charset);
Run Code Online (Sandbox Code Playgroud)
它的工作原理^.^