使用JAVA/Android登录网站(重定向问题)
我正在尝试使用HttpPost登录到一个网站。我的目标是登录并获得网站提供的必要cookie。到目前为止,我已经成功地“登录”,但重定向问题仍然存在。登录后,我从服务器收到成功消息,但实际上,网站重定向到不同的url,并提供会话所需的cookie。我已经用chrome开发者工具检查了网站是如何“工作”的,我有必要的参数来完成这篇文章 我的客户目前没有收到所需的cookies,因为它没有跟踪重定向。我想知道下一步基本上做什么。我如何在登录后跟踪服务器重定向,以获取所需的cookie?我应该使用什么类/方法?我读过htmlUnit,但我想避免它,因为库/数据的大小非常大,而我的应用程序只有0.5mb 我现在正在做的事情(简化): 连接->登录(httpPost)->成功(200)->我只收到一个 饼干(这不是我需要的) 我认为我应该做什么(简化): 连接->登录->成功(200)->跟踪重定向-> 获取cookies(总共5个) 谢谢您的时间,这是代码。我现在用的饼干是不正确的使用JAVA/Android登录网站(重定向问题),java,android,redirect,cookies,login,Java,Android,Redirect,Cookies,Login,我正在尝试使用HttpPost登录到一个网站。我的目标是登录并获得网站提供的必要cookie。到目前为止,我已经成功地“登录”,但重定向问题仍然存在。登录后,我从服务器收到成功消息,但实际上,网站重定向到不同的url,并提供会话所需的cookie。我已经用chrome开发者工具检查了网站是如何“工作”的,我有必要的参数来完成这篇文章 我的客户目前没有收到所需的cookies,因为它没有跟踪重定向。我想知道下一步基本上做什么。我如何在登录后跟踪服务器重定向,以获取所需的cookie?我应该使用什么
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = null;
Log.d("LOGGING" , "Posting...");
HttpPost httpost = new HttpPost(url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user", "myUser"));
nvps.add(new BasicNameValuePair("passwrd", "myPw"));
nvps.add(new BasicNameValuePair("openid_identifier", ""));
nvps.add(new BasicNameValuePair("cookielength", "-1"));
nvps.add(new BasicNameValuePair("hash_passwrd", ""));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Log.d("LOGGING" , "Getting login response...");
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
Log.d("LOGGING" , "Login form getStatus: " + response.getStatusLine());
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
Log.d("LOGGING" , "Cookie: " + cookies.toString());
String SESSION_NAME = "PHPSESSID";
String[] sessionValue = cookies.toString().split("value: ");
String[] sessionValue1 = sessionValue[1].split("]");
String realSessionValue = sessionValue1[0].toString();
Log.d("LOGGING", "SESSION NAME:" + realSessionValue);
/*
Connection.Response loginForm = Jsoup.connect(url)
.method(Connection.Method.GET)
.referrer(referrer)
.execute();
Document login = Jsoup.connect(url)
.data("user", "myUser")
.data("passwrd", "myPw")
.data("cookielength", "1440")
.data("openid_identifier", "")
.data("hash_password", "")
.referrer(referrer)
.cookie(SESSION_NAME, realSessionValue)
.post();
*/
doc = Jsoup.connect(url)
.cookie(SESSION_NAME, realSessionValue)
.get();
//String body = doc.select("body").html();
//Log.d("LOGGING", "Body: " + body.toString());
//Check login
Elements bodies = doc.select("body").first().select("div#main-container-radio").first().select("div#radiorow").first().select("div.bghue").first().select("form#shoutbox-form-r");
Log.d("LOGGING", "Login status: " + bodies.toString());
DefaultHttpClient-httpclient=newdefaulthttpclient();
HttpResponse响应=null;
Log.d(“记录”、“发布…”);
HttpPost HttpPost=新的HttpPost(url);
List-nvps=newarraylist();
添加(新的BasicNameValuePair(“用户”、“我的用户”);
添加(新的BasicNameValuePair(“passwrd”、“myPw”);
添加(新的BasicNameValuePair(“openid_标识符”);
nvps.add(新的BasicNameValuePair(“cookielength”)和“-1”);
add(新的BasicNameValuePair(“hash_passwrd”,即“);
试一试{
setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));
}捕获(不支持DencodingException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
d(“记录”,“获取登录响应…”);
response=httpclient.execute(httpost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
HttpEntity=response.getEntity();
Log.d(“日志记录”,“登录表单getStatus:+response.getStatusLine());
列表cookies=httpclient.getCookieStore().getCookies();
Log.d(“LOGGING”,“Cookie:+cookies.toString());
字符串SESSION_NAME=“PHPSESSID”;
String[]sessionValue=cookies.toString().split(“值:”);
字符串[]sessionValue1=sessionValue[1]。拆分(“]”);
字符串realSessionValue=sessionValue1[0]。toString();
Log.d(“日志记录”,“会话名称:”+realSessionValue);
/*
Connection.Response loginForm=Jsoup.connect(url)
.method(Connection.method.GET)
.推荐人(推荐人)
.execute();
documentlogin=Jsoup.connect(url)
.数据(“用户”、“我的用户”)
.数据(“passwrd”、“myPw”)
.数据(“cookielength”、“1440”)
.data(“openid\u标识符”,“”)
.data(“hash_密码”,“”)
.推荐人(推荐人)
.cookie(会话名称、realSessionValue)
.post();
*/
doc=Jsoup.connect(url)
.cookie(会话名称、realSessionValue)
.get();
//字符串body=doc.select(“body”).html();
//Log.d(“LOGGING”,“Body:+Body.toString());
//检查登录
元素body=doc.select(“body”).first().select(“div#main container radio”).first().select(“div#radiorow”).first().select(“div.bghue”).first().select(“form#shoutbox-form-r”);
Log.d(“日志记录”,“登录状态:+body.toString());
谢谢大家。这是我自己的代码中的一个问题,我错误地使用了错误的url,因为他们有3个不同的url用于登录。否则它运行得很好。您还有其他方法可以做到这一点吗?你对这个网站有控制权吗?很抱歉,这不是最有效的回复,但这似乎真的是一种艰难的方式。谢谢你的回复。我对该网站没有任何控制权,很可能无法收到任何文档。我正在尝试为他们的Android流做一个非官方的无线电客户端(正在工作),现在我正在尝试添加使用shoutbox的功能(在评论之前登录是必需的)。我认为,使用HttpClient,你必须设置默认的重定向策略。文章中有一个例子:在这种情况下,您可以构建一个代理,用户可以将其凭据传递给该代理,该代理将完成登录的所有繁重工作/etct感谢您的评论。我来看看这些建议。重定向策略的问题是,我正在从服务器接收200条消息,但我没有收到必要的cookie。当我通过他们的网站登录时,我收到的不是200条,而是3xx条信息。使用Java方法,我总是得到200。很明显,我在登录时没有得到一些东西。