#序
华理体育
可以查晨跑次数 唯一有用的功能
现在来模拟登陆
#包
主要是用httpclient和jsoup两个包
#类
写了一个SportScore类
构造函数写入用户名和密码
init()方法验证
然后getMorningRun()就可以获取晨跑次数了
#Code
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class SportScore {
String username;
String password;
String html;
boolean ok;
final String DEFAULT_URL = "http://59.78.92.38:8888/sportscore/default.aspx";
final String SCORE_URL = "http://59.78.92.38:8888/sportscore/stScore.aspx";
CloseableHttpClient httpClient;
String getContent(String url) throws Exception{
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
Scanner scanner = new Scanner(entity.getContent());
String s = new String();
while (scanner.hasNextLine()) {
s += (scanner.nextLine() + '\n');
}
return s;
}
boolean init() throws Exception{
httpClient = HttpClients.createDefault();
String s = getContent(DEFAULT_URL);
Document doc = Jsoup.parse(s);
Elements es = doc.getElementsByTag("input");
HttpPost httpPost = new HttpPost(DEFAULT_URL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Element e : es) {
String name = e.attr("name");
String value = e.attr("value");
if (name.equals("txtuser")) value = username;
if (name.equals("txtpwd")) value = password;
if (name.equals("btnok")) continue;
nvps.add(new BasicNameValuePair(name, value));
}
nvps.add(new BasicNameValuePair("dlljs", "st"));
nvps.add(new BasicNameValuePair("btnok.x", "0"));
nvps.add(new BasicNameValuePair("btnok.y", "0"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
ok = false;
return false;
}
html = getContent(SCORE_URL);
return true;
}
int getMorningRun() throws Exception{
Document document = Jsoup.parse(html);
Elements elements = document.select("td[class=\"bottom-line\"]");
int res = Integer.valueOf(elements.get(1).text());
return res;
}
SportScore(String s0, String s1){
username = s0;
password = s1;
ok = true;
}
}
public class Main {
public static void main(String[] args) throws Exception {
SportScore st = new SportScore("", "");
if (st.init()) {
System.out.println(st.getMorningRun());
}else {
System.out.println("Wrong Password");
}
}
}