如果有一个网站的url是http://xxx.xxx.xxx,登陆的动作是login.do其中userId表示变量名 username表示内容,密码同理
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--> 1 public static String getHttpContent(String loginForm, String url, String userId, String passwordId,
2 String username, String password) throws Exception {
3 String responseString = "";
4 HttpClient conn = new HttpClient();
5 if (!loginForm.equals("")) {
6 UTF8PostMethod signinPost = new UTF8PostMethod(loginForm);
7 signinPost.addParameter(userId, username);
8 signinPost.addParameter(passwordId, password);
9 conn.executeMethod(signinPost);
10 }
11 UTF8PostMethod postMethod = new UTF8PostMethod(url);
12 int statusCode = conn.executeMethod(postMethod);
13 if (statusCode > 300) {
14 Header header = postMethod.getResponseHeader("location");
15 String location = null;
16 if (header != null) {
17 location = header.getValue();
18 System.out.println("The page was redirected to:" + location);
19 postMethod = new UTF8PostMethod(location);
20 }
21 }
22 String serverPath = HttpUtils.getServerContext(url);
23 String host = HttpUtils.gethost(url);
24 String[] item = url.split("/");
25 responseString = postMethod.getResponseBodyAsString();
26 if (responseString == null)
27 responseString = "此网址内容为空";
28 if (item.length > 4) {
29 if (responseString.replace("/" + item[3], item[0] + "//" + item[2] + "/" + item[3]) != null) {
30 responseString = responseString.replace("/" + item[3], item[0] + "//" + item[2] + "/" + item[3]);
31 }
32 } else {
33 if (responseString.replace("/" + item[3], item[0] + "//" + item[2]) != null) {
34 responseString = responseString.replace("/" + item[3], item[0] + "//" + item[2]);
35 }
36 }
37 return responseString;
38 }
2 String username, String password) throws Exception {
3 String responseString = "";
4 HttpClient conn = new HttpClient();
5 if (!loginForm.equals("")) {
6 UTF8PostMethod signinPost = new UTF8PostMethod(loginForm);
7 signinPost.addParameter(userId, username);
8 signinPost.addParameter(passwordId, password);
9 conn.executeMethod(signinPost);
10 }
11 UTF8PostMethod postMethod = new UTF8PostMethod(url);
12 int statusCode = conn.executeMethod(postMethod);
13 if (statusCode > 300) {
14 Header header = postMethod.getResponseHeader("location");
15 String location = null;
16 if (header != null) {
17 location = header.getValue();
18 System.out.println("The page was redirected to:" + location);
19 postMethod = new UTF8PostMethod(location);
20 }
21 }
22 String serverPath = HttpUtils.getServerContext(url);
23 String host = HttpUtils.gethost(url);
24 String[] item = url.split("/");
25 responseString = postMethod.getResponseBodyAsString();
26 if (responseString == null)
27 responseString = "此网址内容为空";
28 if (item.length > 4) {
29 if (responseString.replace("/" + item[3], item[0] + "//" + item[2] + "/" + item[3]) != null) {
30 responseString = responseString.replace("/" + item[3], item[0] + "//" + item[2] + "/" + item[3]);
31 }
32 } else {
33 if (responseString.replace("/" + item[3], item[0] + "//" + item[2]) != null) {
34 responseString = responseString.replace("/" + item[3], item[0] + "//" + item[2]);
35 }
36 }
37 return responseString;
38 }
发表评论
-
如何把Https网站中的安全证书导入到java中的cacerts证书库?
2010-08-06 18:22 913其实很简单,方法如下: 每一步:进入某个https://www ... -
Java引用多个jar包的写法
2010-02-21 11:35 1148假设有个程序的启动方法在test.java里运行Java程序是 ... -
关于在配置spring的时候我犯的错误总结
2008-04-15 13:22 551由于第一次弄spring , ... -
关于.hashcode()和equals()的问题
2008-04-17 23:22 374今天看代码的时候忽然发现有些类会重写 ... -
关于SSH的琐事
2008-04-21 23:00 393今天终于把一个最简化的框架跑起来了。然后小小的 ... -
今天非常郁闷
2008-04-29 23:33 500今天做了下小测试,传参数也传到 ... -
自身一对多关联模型的JSON转换
2008-06-20 22:59 2104最近又遇到了一个问题, 这里有一个一对多自身关联 ... -
[转载]Spring AOP编程笔记
2008-06-24 17:09 479AOP正在成为软件开发的下一个圣杯。使用AOP,你可以将处理a ... -
Flex Json文档
2008-06-25 10:46 856<style> <!-- /* Font D ... -
Spring Security 2.0学习笔记
2008-07-05 02:15 693spring 2.5也发布了,Acegi 2.0也出来 ... -
IP转向技术------自动识别IP,并跳转到来访问者所在的城市
2009-02-02 17:10 1759先去下一个最新的纯真IP数据库,然后按如下操作: 1.运行纯真 ... -
关于解析CSV文件
2010-01-19 17:06 1279做开发的时候需要对一个CSV文件进行解析,并把解 ... -
[转载]quartz的配置
2010-01-22 10:58 549首先我们来写一个被调度的类: package com.kay. ...
相关推荐
HttpClient4.3模拟登陆新版12306
此资源为新浪微博和腾讯微博的HttpClient4模拟登录的代码Sina.java为新浪的,TencentClient.java为腾讯的。
用HttpClient来模拟浏览器GET_POST,技术简单学!!
httpclient4之百度模拟登陆,回复与58同城自动登陆
httpclient模拟登陆,请大家快过来赶紧下载哦。
利用某些站点提供的页面来完成某种功能,例如说我们想知道某个手机号码的归属地而我们自己又没有这样的数据,因此只好借助其他公司已有的网站来完成这个功能,这个时候我们需要向网页提交手机号码并从返回的页面中...
JAVA-用HttpClient来模拟浏览器GET,POST.docx
httpclient4 微信公众账号模拟登陆,网络上看到的,觉得不错上传出来,供大家一起共同分享学习。
Jsoup+httpclient模拟登陆和抓取页面.pdf
httpclient模拟登陆代码以及相关的jar包
模拟登陆方正系统,在此基础上可以做开发类似课程格子之类的软件供个人或者多人之用
HttpClient模拟登录实例,模拟登录网站,获取用户信息,登录后数据,带验证码登录,带解析HTML实例。
模拟登录 不提供用户名密码
利用HttpClient发送HTTP请求 利用HttpClient发送HTTP请求
NULL 博文链接:https://lxzqz.iteye.com/blog/2051318
工程里的入口类为:mnLogin.java ,由于模拟登陆的网站,使用了自签名的SSL证书,所以新增两个方法,创建HttpClient不校验服务端证书,后台通过调用登陆接口,获得返回的cookie信息,在使用登陆成功获得cookie,调取...
JAVA利用HttpClient进行HTTPS接口调用
NULL 博文链接:https://shaozhen.iteye.com/blog/950472
Jsoup+httpclient 模拟登陆和抓取页面 package com.app.html; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader...