好的,这是我的申请:
允许我通过移动应用程序而非网站向CokeZone.co.uk提交CokeZone代码的Android应用。
所以我编写了这段代码来执行post logon命令,然后检查我是否登录后。
问题是 - 我发送post命令后从主页获取的html是默认的 - 好像我没有登录 - 所以出了问题。
任何人都可以帮忙!它可能是我发送POST的URL,或POST命令中的params - 我还没有完成很多这样的东西,所以它可能是显而易见的。
到目前为止,我的代码如下:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
thisResponse = printPage(entity.getContent());
Log.e("debug",thisResponse);
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
HttpPost httpost = new HttpPost("https://secure.cokezone.co.uk/home/blank.jsp?_DARGS=/home/login/login.jsp");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("_dyncharset", "ISO-8859-1"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.name","renegadeandy%40gmail.com"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.name", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.cookiedUser", "false"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.cookiedUser", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.password", "passwordval"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.password", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.rememberMe", "yes"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.rememberMe", "false"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aSuccessURL", "http://www.cokezone.co.uk/home/index.jsp"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.aSuccessURL", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "http://www.cokezone.co.uk/home/index.jsphttps://secure.cokezone.co.uk/home/index.jsp"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.explicitLogin", "true"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.explicitLogin", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "login"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "LOGIN"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
nvps.add(new BasicNameValuePair("_DARGS", "/home/login/login.jsp"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
thisResponse = printPage(entity.getContent());
entity.consumeContent();
}
Log.e("debug",thisResponse);
Log.e("debug","done");
httpget = new HttpGet("http://www.cokezone.co.uk/home/index.jsp");
response = httpclient.execute(httpget);
entity = response.getEntity();
TextView points = (TextView)findViewById(R.id.points);
points.setText(getPoints(entity.getContent()).toString());
debug.setText(thisResponse);
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
答案 0 :(得分:3)
对登录请求的响应包含一个Cookie,其中包含登录会话的会话ID。您必须在后续请求中将其发回服务器,否则其他请求与您的登录无关。
编辑:如果HttpClient实际上是在管理您的cookie(请查看http://hc.apache.org/httpcomponents-client/tutorial/html/statemgmt.html),那么...登录后登录可能会将您重定向到其他地方,您需要遵循该重定向才能获取Cookie吗?有点猜测,但这听起来好像你没有完成登录。