Java 使用android登录网站
基本上,我使用firefox扩展来获取帖子,并通过登录我的学校网站获取数据。我想能够登录,但当我尝试它说:“会话已过期,请重新登录!”。这是我目前掌握的代码,有人能告诉我我做错了什么吗?我认为这与重定向有关,因为第一篇文章应该重定向到正确的位置,但我无法让它工作,并且试图获取位置标题会抛出一个空指针Java 使用android登录网站,java,android,cookies,https,Java,Android,Cookies,Https,基本上,我使用firefox扩展来获取帖子,并通过登录我的学校网站获取数据。我想能够登录,但当我尝试它说:“会话已过期,请重新登录!”。这是我目前掌握的代码,有人能告诉我我做错了什么吗?我认为这与重定向有关,因为第一篇文章应该重定向到正确的位置,但我无法让它工作,并且试图获取位置标题会抛出一个空指针 try { super.onCreate(icicle); CookieManager cookieManager = new CookieManag
try {
super.onCreate(icicle);
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
BasicClientCookie netscapeCookie = new BasicClientCookie("UserLoginInfo", "SelectedTab=StudentLogin&RedirectURL=");
netscapeCookie.setVersion(0);
netscapeCookie.setDomain("sisk12.hannibal.k12.mo.us");
netscapeCookie.setPath("/");
cookieStore.addCookie(netscapeCookie);
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpPost httpost = new HttpPost("https://sisk12.hannibal.k12.mo.us/hb/Default.aspx?__EVENTTARGET=&__EVENTARGUMENT=&txtUserName=xxxx&txtPassword=xxxxxx&btnLogin=Login&txtSelectedTab=StudentLogin");
HttpResponse response = httpclient.execute(httpost, localContext);
HttpEntity entity = response.getEntity();
// readResponse(response.getEntity().getContent());
//Log.w("****","Login form get: " + response.getFirstHeader("Location").getValue());
// readResponse(response.getEntity().getContent());
if (entity != null) {
entity.consumeContent();
}
Header[] headers = response.getHeaders("Location");
if (headers != null && headers.length != 0) {
String newUrl = headers[headers.length - 1].getValue();
// call again with new URL
HttpGet httpget = new HttpGet(newUrl);
entity = response.getEntity();
Log.w("****", "Login form get: " + response.getStatusLine());
response = httpclient.execute(httpget, localContext);
readResponse(response.getEntity().getContent());
} else {
Log.w("****", "No location header");
}
if (cookieStore.getCookies().isEmpty()) {
Log.w("****", "None");
} else {
for (int i = 0; i < cookieStore.getCookies().size(); i++) {
Log.w("****", "- " + cookieStore.getCookies().get(i).toString());
}
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
} catch (IOException ex) {
Log.e("****", Log.getStackTraceString(ex));
}
public String readResponse(InputStream input) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuilder sb = new StringBuilder("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line).append(NL);
}
in.close();
String page = sb.toString();
Log.i("****", page);
return page;
}
代码中有一个重定向,应该由post调用,但我不知道apache是否自动处理该重定向,如果不是,如何处理该重定向?它可能与ASP.NET会话cookie有关 在浏览器中加载登录页面时(
https://sisk12.hannibal.k12.mo.us/hb/Default.aspx
)它设置一个会话cookie,ASP.NET\u SessionId
。然后在浏览器中单击“登录”按钮时提交该cookie
但是您的代码没有提交这样的cookie。如果你做这样的事情,也许会有用:
这可能与ASP.NET会话cookie有关 在浏览器中加载登录页面时(
https://sisk12.hannibal.k12.mo.us/hb/Default.aspx
)它设置一个会话cookie,ASP.NET\u SessionId
。然后在浏览器中单击“登录”按钮时提交该cookie
但是您的代码没有提交这样的cookie。如果你做这样的事情,也许会有用:
我不确定,但您的问题可能是由SSL连接引起的。尝试在HttpClient中设置ssl连接
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
SchemeRegistry sr = new SchemeRegistry();
sr.register(new Scheme(RequestScheme.HTTP.name(), PlainSocketFactory.getSocketFactory(), RequestScheme.HTTP.port));
sr.register(new Scheme(RequestScheme.HTTPS.name(), SSLSocketFactory.getSocketFactory(), RequestScheme.HTTPS.port));
ClientConnectionManager manager = new ThreadSafeClientConnManager(params, sr);
Client client = new Client(manager, params)
我不确定,但您的问题可能是由SSL连接引起的。尝试在HttpClient中设置ssl连接
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
SchemeRegistry sr = new SchemeRegistry();
sr.register(new Scheme(RequestScheme.HTTP.name(), PlainSocketFactory.getSocketFactory(), RequestScheme.HTTP.port));
sr.register(new Scheme(RequestScheme.HTTPS.name(), SSLSocketFactory.getSocketFactory(), RequestScheme.HTTPS.port));
ClientConnectionManager manager = new ThreadSafeClientConnManager(params, sr);
Client client = new Client(manager, params)
以下是我的建议
CookieManager
)。HttpClient能够透明地管理cookie,而无需显式处理。(假设您的httpclient实例类似于浏览器实例,并且您将其用于GET和POST)\uuu VIEWSTATE
的参数,它也与凭据一起发布。这对于确保登录帖子不是除浏览器以外的任何来源似乎至关重要。如果这是强制性的,那么这将使您的工作变得棘手,因为您需要解析登录页面html(您可以使用该html进行解析)获取该参数并在post请求中发送相同的内容String getURL = "https://sisk12.hannibal.k12.mo.us/hb/";
String postURL = getURL;
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(getURL);
int statusCode = client.executeMethod(getMethod);
//String getResponse = getMethod.getResponseBodyAsString();
//System.out.println(getResponse);
PostMethod postMethod = new PostMethod(postURL);
postMethod.addParameter("__EVENTARGUMENT", "");
postMethod.addParameter("__EVENTTARGET", "");
postMethod.addParameter("__VIEWSTATE", "");//to be parsed from login page
postMethod.addParameter("btnLogin", "Login");
postMethod.addParameter("txtPassword", "xxxx");
postMethod.addParameter("txtSelectedTab", "StudentLogin");
postMethod.addParameter("txtUserName", "xxxx");
client.executeMethod(postMethod);
String postResponse = postMethod.getResponseBodyAsString();
System.out.println(postResponse);
如果遇到重定向,您可能需要编写一些附加代码。以下是我的建议
CookieManager
)。HttpClient能够透明地管理cookie,而无需显式处理。(假设您的httpclient实例类似于浏览器实例,并且您将其用于GET和POST)\uuu VIEWSTATE
的参数,它也与凭据一起发布。这对于确保登录帖子不是除浏览器以外的任何来源似乎至关重要。如果这是强制性的,那么这将使您的工作变得棘手,因为您需要解析登录页面html(您可以使用该html进行解析)获取该参数并在post请求中发送相同的内容String getURL = "https://sisk12.hannibal.k12.mo.us/hb/";
String postURL = getURL;
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(getURL);
int statusCode = client.executeMethod(getMethod);
//String getResponse = getMethod.getResponseBodyAsString();
//System.out.println(getResponse);
PostMethod postMethod = new PostMethod(postURL);
postMethod.addParameter("__EVENTARGUMENT", "");
postMethod.addParameter("__EVENTTARGET", "");
postMethod.addParameter("__VIEWSTATE", "");//to be parsed from login page
postMethod.addParameter("btnLogin", "Login");
postMethod.addParameter("txtPassword", "xxxx");
postMethod.addParameter("txtSelectedTab", "StudentLogin");
postMethod.addParameter("txtUserName", "xxxx");
client.executeMethod(postMethod);
String postResponse = postMethod.getResponseBodyAsString();
System.out.println(postResponse);
如果遇到重定向,您可能需要编写一些附加代码。您是否设置了httpclient来处理https请求(不是http)?无论如何,请删除该url中的id和密码。我不确定如果没有用户名和密码,我是否会得到任何帮助,但我现在删除了它们。把他们留在网上让我有点紧张不,除了这里的代码,我什么都没做。我没有意识到我需要做那样的事情。你能举个例子说明怎么做吗?这是我的代码片段。HttpParams params=新的BasicHttpParams();HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);HttpProtocolParams.setContentCharset(参数,“UTF-8”);SchemeRegistry sr=新Sch