Java模拟Web登陆(Post模式)

https://blog.csdn.net/a527603/article/details/38490583


Java模拟Web登陆(Post模式)

  1. // 连接地址(通过阅读html源代码获得,即为登陆表单提交的URL)    
  2. String surl = "http://192.168.5.40:8090/ylysystem/systemmgmt/mainmanage/login.sc";    
  3.   
  4. /**  
  5. * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using  
  6. * java.net.URL and //java.net.URLConnection  
  7. */    
  8. URL url = new URL(surl);    
  9. HttpURLConnection connection = (HttpURLConnection) url.openConnection();    
  10.   
  11. /**  
  12. * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。  
  13. * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:  
  14. */    
  15. connection.setDoOutput(true);  //打开输出,向服务器输出参数(POST方式、字节)(写参数之前应先将参数的字节长度设置到配置"Content-Length"<字节长度>)  
  16. connection.setDoInput(true);//打开输入,从服务器读取返回数据  
  17. connection.setRequestMethod("POST"); //设置登录模式为POST(字符串大写)  
  18. connection.setInstanceFollowRedirects(false);   
  19. connection.connect();  
  20. /**  
  21. * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ...  
  22. */    
  23. OutputStreamWriter out = new OutputStreamWriter(connection    
  24.       .getOutputStream(), "utf-8");    
  25. //其中的loginName和loginPassword也是阅读html代码得知的,即为表单中对应的参数名称    
  26. out.write("loginName=admin&loginPassword=*****&validateCode=autoupdate"); // post的关键所在!    
  27. //remember to clean up    
  28. out.flush();    
  29. out.close();    
  30.   
  31. //取得cookie,相当于记录了身份,供下次访问时使用    
  32. //HttpURLConnection.getHeaderFields()).get("Set-Cookie")用于迭代读取Cookie,为以后使用  
  33. //HttpURLConnection.getHeaderField("Set-Cookie")也可用于读取Cookie,但不一定能读取完全  
  34. String cookieVal = connection.getHeaderField("Set-Cookie");  //格式:JSESSIONID=541884418E77E7F07363CCEE91D4FF7E; Path=/  
  35. connection.disconnect();  
  36.   
  37. //登陆成功后,即可访问其他URL了。  
  38. String s = "http://192.168.5.40:8090/ylysystem/cms/systemsetting/u_cmsInput.sc?" +  
  39.         "buildType=date&maxResults=10&firstResult=0&buildContent=index" +  
  40.         "&beginDate=2013-03-13&endDate=2013-03-20";    
  41. //重新打开一个连接    
  42. url = new URL(s);    
  43. HttpURLConnection resumeConnection = (HttpURLConnection) url  
  44.       .openConnection();    
  45. if (cookieVal != null) {    
  46.      //发送cookie信息上去,以表明自己的身份,否则会被认为没有权限    
  47.   resumeConnection.setRequestProperty("Cookie", cookieVal);//设置登陆配置   
  48. }  
  49. resumeConnection.connect();    
  50. InputStream urlStream = resumeConnection.getInputStream();    
  51. BufferedReader bufferedReader = new BufferedReader(    
  52.       new InputStreamReader(urlStream));    
  53. String ss = null;    
  54. String total = "";    
  55. while ((ss = bufferedReader.readLine()) != null) {    
  56.     System.err.println(ss);  
  57.     total += ss;    
  58. }    
  59. IOUtils.write(total, new FileOutputStream("d:/index.html"));    
  60. bufferedReader.close();   

Java模拟Web登陆(Post模式)
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值