看图学GetPost模拟网页登录 零基础也可以

学校的网络计费登录系统:


现在,模拟它,也就是在程序里输入账号、密码,然后点击运行,便实现在浏览器里按连接网络的效果。

登录时,用chrome的开发者工具,得到了:


可看到CheckLogin.jsp第一时间被触发了,接着index.jsp被checkLogin.jsp调用。

让我们细看CheckLogin.jsp,发现:


发现是post方式提交的,还有一些键值对。但action是什么呢?

点击Form Data旁边的view source,看到:


action的真实值是什么呢?

查看一下网页的源代码:


看第二行的action字符串,想必就应该是action的对象了。

ip怎么得到呢。这登录系统在学校哪里都可以登录,可以无线,也可以在宿舍里登录。ip地址也是变的。如何自动得到呢。

还记得第一张图吗?


这里网页自动提供了ip地址,想必是登录系统知道是哪个ip在访问它,我们直接从网页抓取就行,不必操这个心。

chrome提供了很方便的工具,让我们迅速定位网页某一部分的代码。Firefox里也有个叫firebug的插件很强,可以轻松定位。


就是用这个放大镜,点一下,再点一下ip地址。


看到下面的标签层层递进,ip地址在一个叫名叫span,class为login_txt下的text中。





select函数找到满足cssQuery的标签,first返回第一个。

所以我们得到了所有的键值对,可以模拟了。



post把请求的键值对保存在entity中。这里的post.abort()把post中断掉。因为后面我们还要用这个httpClient来发送请求,而org.apache的http相关类又想尽可能地复用httpConnection,所以当一个请求还没终止的时候,又去发送另一个请求,会发生异常。我也不是很懂。。

为什么后面还要用这个httpClient呢?稍后解释。

这样还不行,仅仅是checkLogin,这个jsp用来检查账号密码是否正确。


在上面加入这行代码可以看到返回值。

如果返回200,表明密码正确,如果是303,表明错误。而404表明连接有错误。

上上面可以看到第二个运行的jsp脚本为index.jsp,


这个是Get请求,相当容易。




在这之后,还是不能联网,还要模拟一个请求connect_action.jsp的东西,这个要求提供userid=88888类似的键值对,相对对应我们学生的一个id。发现我们班上前一个学号的同学他的userid刚好比我的要小1。这头痛了,怎么去获得这个东西呢?!蛋疼的!

还好我足智多谋,想到了刚刚ip地址从网页上得到,userid可不可以呢?


这个其实是断网那个jsp的截图,不影响。在connect.jsp也是一样的有这个userid,也即我们请求的第二个jsp返回的网页源代码里包含“下一个我们要请求的jsp”的键值对。

同样,来一次抓取:


这里用到了正则表达式,group(0)是userid=88888,group(1)就是括号里的内容。\d表示任意一个数字,后面的+表示\d出现一次或一次以上。find()表示表达式有没匹配部分子串,而matches()方法表示表达式是否完全匹配整个字符串。所以src类似<frame userid=88888&ip="的话,matches()将返回false,因为所给的正则表达式不是匹配了整个字符串。

源码:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.util.ArrayList;  
  5. import java.util.LinkedList;  
  6. import java.util.List;  
  7. import java.util.regex.Matcher;  
  8. import java.util.regex.Pattern;  
  9.   
  10. import org.apache.http.HttpEntity;  
  11. import org.apache.http.HttpResponse;  
  12. import org.apache.http.NameValuePair;  
  13. import org.apache.http.client.HttpClient;  
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  15. import org.apache.http.client.methods.HttpGet;  
  16. import org.apache.http.client.methods.HttpPost;  
  17. import org.apache.http.client.utils.URLEncodedUtils;  
  18. import org.apache.http.cookie.Cookie;  
  19. import org.apache.http.impl.client.AbstractHttpClient;  
  20. import org.apache.http.impl.client.DefaultHttpClient;  
  21. import org.apache.http.message.BasicNameValuePair;  
  22. import org.apache.http.protocol.HTTP;  
  23. import org.apache.http.util.EntityUtils;  
  24. import org.jsoup.Jsoup;  
  25. import org.jsoup.nodes.Document;  
  26. import org.jsoup.nodes.Element;  
  27. import org.jsoup.select.Elements;  
  28.   
  29. public class Login{  
  30.   
  31.     static void print(String format, Object... args) {  
  32.         System.out.println(String.format(format, args));  
  33.     }  
  34.       
  35.     public static void main(String[] args) {  
  36.         HttpClient httpClient = new DefaultHttpClient();  
  37.         String ip;  
  38.         String userId;  
  39.         String username="130888888";  
  40.         String password = "88888888";  
  41.           
  42.         ip=cssQueryFirstText("http://login.bjfu.edu.cn/index.jsp","span.login_txt");  
  43.           
  44.         doPost(httpClient,"http://login.bjfu.edu.cn/checkLogin.jsp",  
  45.                 "username",username,"password",password,  
  46.                 "ip",ip,"action","checkLogin.jsp");  
  47.         String content=doGet(httpClient,"http://login.bjfu.edu.cn/user/index.jsp",  
  48.           "ip",ip,"action","connect");  
  49.         userId=userId(content);  
  50.         doGet(httpClient,"http://login.bjfu.edu.cn/user/network/connect_action.jsp",  
  51.             "userid",userId,"ip",ip,"type","2");  
  52.     }  
  53.   
  54.     static String userId(String html){  
  55.         Document doc=Jsoup.parse(html);  
  56.         Element elem=doc.select("frame#main").first();  
  57.         String src=elem.attr("src");  
  58.         Pattern pattern=Pattern.compile("userid=(\\d+)");  
  59.         Matcher matcher=pattern.matcher(src);  
  60.         String ans="";  
  61.         if(matcher.find()){  
  62.           ans=matcher.group(1);  
  63.         }  
  64.         return ans;  
  65.     }  
  66.       
  67.     static String cssQueryFirstText(String url,String cssQuery) {  
  68.         String ip=null;  
  69.         try{  
  70.             Document doc=Jsoup.connect(url).get();  
  71.             Elements elems=doc.select(cssQuery);  
  72.             Element elem=elems.first();   
  73.             ip=elem.text();  
  74.         }catch(Exception e){  
  75.             e.printStackTrace();  
  76.         }  
  77.         return ip;  
  78.     }  
  79.   
  80.     static String doGet(HttpClient httpClient, String url, String... pairs) {  
  81.         String entityContent=null;  
  82.         try {  
  83.             url = makeGetSrl(url, pairs);  
  84.             HttpGet get = new HttpGet(url);  
  85.             HttpResponse response = httpClient.execute(get);  
  86.             //print("%d", response.getStatusLine().getStatusCode());  
  87.             entityContent = entity(response);  
  88.             EntityUtils.consume(response.getEntity());  
  89.         } catch (IOException e) {  
  90.             e.printStackTrace();  
  91.         }  
  92.         return entityContent;  
  93.     }  
  94.   
  95.     static void printEntity(HttpResponse rp){  
  96.         print("%s",entity(rp));  
  97.     }  
  98.   
  99.     static String entity(HttpResponse response) {  
  100.         StringBuilder sb = new StringBuilder("");  
  101.         try {  
  102.             HttpEntity entity = response.getEntity();  
  103.             if (entity != null) {  
  104.                 BufferedReader br = new BufferedReader(new InputStreamReader(  
  105.                         entity.getContent()));  
  106.                 String line = null;  
  107.                 while ((line = br.readLine()) != null) {  
  108.                     sb.append(line + "\n");  
  109.                 }  
  110.             }  
  111.         } catch (Exception e) {  
  112.             e.printStackTrace();  
  113.         }  
  114.         return sb.toString();  
  115.     }  
  116.       
  117.     static String makeGetSrl(String url,String... pairs) {  
  118.         if(!url.endsWith("?")){  
  119.             url+="?";  
  120.         }  
  121.         List<NameValuePair>params=new LinkedList<NameValuePair>();  
  122.         int len=pairs.length;  
  123.         for(int i=0;i<len/2;i++){  
  124.             params.add(new BasicNameValuePair(pairs[2*i],pairs[2*i+1]));  
  125.         }  
  126.         String paramsStr=URLEncodedUtils.format(params,"utf-8");  
  127.         url+=paramsStr;  
  128.         return url;  
  129.     }  
  130.   
  131.     static String doPost(HttpClient httpClient, String url, String... pairs) {  
  132.         String entityContent = null;  
  133.         try {  
  134.             HttpPost post = new HttpPost(url);  
  135.             List<NameValuePair> params = new ArrayList<NameValuePair>();  
  136.             for (int i = 0; i < pairs.length / 2; i++) {  
  137.                 params.add(new BasicNameValuePair(pairs[2 * i], pairs[2 * i + 1]));  
  138.             }  
  139.             post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));  
  140.             HttpResponse response = httpClient.execute(post);  
  141.             entityContent = entity(response);  
  142.             //print("%d", response.getStatusLine().getStatusCode());  
  143.             post.abort();  
  144.         } catch (Exception e) {  
  145.             e.printStackTrace();  
  146.         }  
  147.         return entityContent;  
  148.     }  
  149.       
  150.     private static String getCookies(HttpClient client) {  
  151.         StringBuilder sb = new StringBuilder();  
  152.         List<Cookie> cookies = ((AbstractHttpClient)  
  153.                 client).getCookieStore().getCookies();  
  154.         for(Cookie cookie: cookies)  
  155.             sb.append(cookie.getName() + "=" + cookie.getValue() + ";");  
  156.         return sb.toString();  
  157.    }  
  158. }  

github源码   可能你运行不了,因为没有我们学校的上网账号密码。

另外,之所以一直用一个HttpClient,是因为里面自动保存了cookie内容。jsp脚本那边是看cookie来判断是否是同一段时间发送的请求。所以,时常拷贝淘宝的网址到其它浏览器的时候,会提醒说超时啥的。

jsoup,正则表达式都是好东西。正则表达式还有工具来练习的,在线也有。Hun fun by using it!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值