Java模拟Web登陆(Post模式)
来源:互联网 发布:澳大利亚网络攻击 编辑:程序博客网 时间:2024/04/26 15:40
// 连接地址(通过阅读html源代码获得,即为登陆表单提交的URL) String surl = "http://192.168.5.40:8090/ylysystem/systemmgmt/mainmanage/login.sc"; /** * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using * java.net.URL and //java.net.URLConnection */ URL url = new URL(surl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); /** * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: */ connection.setDoOutput(true); //打开输出,向服务器输出参数(POST方式、字节)(写参数之前应先将参数的字节长度设置到配置"Content-Length"<字节长度>)connection.setDoInput(true);//打开输入,从服务器读取返回数据connection.setRequestMethod("POST"); //设置登录模式为POST(字符串大写)connection.setInstanceFollowRedirects(false); connection.connect();/** * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... */ OutputStreamWriter out = new OutputStreamWriter(connection .getOutputStream(), "utf-8"); //其中的loginName和loginPassword也是阅读html代码得知的,即为表单中对应的参数名称 out.write("loginName=admin&loginPassword=*****&validateCode=autoupdate"); // post的关键所在! //remember to clean up out.flush(); out.close(); //取得cookie,相当于记录了身份,供下次访问时使用 //HttpURLConnection.getHeaderFields()).get("Set-Cookie")用于迭代读取Cookie,为以后使用//HttpURLConnection.getHeaderField("Set-Cookie")也可用于读取Cookie,但不一定能读取完全String cookieVal = connection.getHeaderField("Set-Cookie"); //格式:JSESSIONID=541884418E77E7F07363CCEE91D4FF7E; Path=/connection.disconnect();//登陆成功后,即可访问其他URL了。String s = "http://192.168.5.40:8090/ylysystem/cms/systemsetting/u_cmsInput.sc?" +"buildType=date&maxResults=10&firstResult=0&buildContent=index" +"&beginDate=2013-03-13&endDate=2013-03-20"; //重新打开一个连接 url = new URL(s); HttpURLConnection resumeConnection = (HttpURLConnection) url .openConnection(); if (cookieVal != null) { //发送cookie信息上去,以表明自己的身份,否则会被认为没有权限 resumeConnection.setRequestProperty("Cookie", cookieVal);//设置登陆配置}resumeConnection.connect(); InputStream urlStream = resumeConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(urlStream)); String ss = null; String total = ""; while ((ss = bufferedReader.readLine()) != null) { System.err.println(ss);total += ss; } IOUtils.write(total, new FileOutputStream("d:/index.html")); bufferedReader.close();
0 0
- Java模拟Web登陆(Post模式)
- Java模拟Web登陆(Post模式)
- PHP模拟post登陆
- 模拟post登陆
- ASP.NET POST模拟登陆
- PHP_CURL1之模拟POST登陆
- PHP_CURL之模拟POST登陆
- Cookie和Post模拟登陆
- python--模拟POST登陆请求(待优化)
- 模拟post登陆提交表单问题详解
- curl 的post请求模拟登陆
- java如何模拟 登陆
- java模拟登陆
- java模拟登陆
- java模拟登陆
- java模拟登陆
- 利用java模拟登陆
- java模拟登陆功能
- CERSAIE,即在博洛尼亚的瓷砖和浴室家具博览会
- 打造漂亮的tooltip
- cocos2dx3.2 学习笔记(1)--CppTest
- Brainguru实现高品质的专业网页设计
- SQL事务
- Java模拟Web登陆(Post模式)
- BIA体脂肪显示器和减肥 - 他们准确
- JAVA,Calendar的用法
- 基于HTML5 Canvas和jQuery 的画图工具的实现
- Brainguru - 专业网站开发和搜索引擎优化公司在印度诺伊达
- [网站] 国学资料下载
- A峰值在合作伙伴关系管理的基础知识
- Unity3d之游戏分析一
- IOS动态方法决议