我们知道,网站走的是http/https等协议,通过这些协议,我们能在浏览器中完成网站的注册,登录,发表文章,上传文件等一系列操作。呵呵,讲到用浏览器,意味着我们必须用我们的鼠标,键盘进行操作,如果是你要进行大量的重复性操作的话,是不是就略显疲惫了?比如你要反复的登录,然后去给某人投票?本人以前就在寝室反复地从excel里面复制用户名密码登录,投票,注销,为一个学姐刷票...刷抽筋了。
现在通过httpclient这个apache common这个第三方api,就可以很轻松地把这些工作都交给程序来做。
我这里演示的是注册一个验证码的博客,简单地介绍下httpclient4的使用。
ps.博客是我以前做的,基于jsp/servlet
1.首页
2.查看该页面的源代码 这里只看form表单
<form name="reg" action="regist.do" method="post" onSubmit="return checkdata()"> <center> <table width=600 border=0> <tr><td> 用户名</td> <td width=60><input type="text" name="username" onBlur="checkUsername()"></td><td width=400><font color=red>*<button onclick="checkUser()">检测用户名</button><span id="usernameE"></span></font></td> </tr> <tr><td> 密码 </td> <td width=60><input type="password" name="password" onBlur="checkPassword()"></td><td width=300><font color=red>*<span id="passwordE"></span></font></p></td></tr> <tr><td> 重复密码 </td> <td width=60> <input type="password" name="repassword" onBlur="checkRePassword()" ></td><td width=300><font color=red>*<span id="repasswordE"></span></font></p></td></tr> <tr><td> 姓名</td> <td width=60> <input type="text" name="name" onBlur="checkRealName()"></td><td width=300><font color=red>*<span id="nameE"></span></font></p></td></tr> <tr><td> 性别 </td> <td width=200> <label> <input type="radio" name="sex" value="0" > 男</label> <label> <input type="radio" name="sex" value="1"> 女</label> <label> <input type="radio" name="sex" value="2" checked="checked"> 不详</label></td> </tr> <tr><td> 邮箱</td> <td width=60> <input type="text" name="email" onblur="checkMail()"></td><td width=300><font color=red>*<span id="mailE"></span></font></p></td></tr> <tr><td> 验证码</td><td width=60><input type="text" name="ccode" onblur="checkCode()"/></td> <td width=300> <img id="code" src="Checkcode?"+1335096054921 /> [url=javascript:;]看不清,换一个[/url]</p></td></tr> <tr><td> <input type="submit" name="submit" value="提交"> </label> <label> <input name="reset" type="reset" id="reset" value="重置"> </label> </td></tr> </table> </center> </form>
3.这个表单是要提交到 regist.do 这个servlet,提交的信息有如下属性
username------用户名
password------密码
name------姓名
sex------性别
email------邮箱
ccode------验证码
需要注意的是:
验证码</td><td width=60><input type="text" name="ccode" onblur="checkCode()"/></td>
<td width=300> <img id="code" src="Checkcode?"+1335096054921 />
<img>连接的是一个servlet:Checkcode,后面跟的是随机数,防止得到缓存数据
知道了如上信息后,我们就可以开始编写程序了。
首先创建一个java project
导入httpclient所需要的Jar包 我这里用的是最新版
下载地址 http://hc.apache.org/downloads.cgi
public class FormDemo { public static void main(String[] args) throws ClientProtocolException, IOException { DefaultHttpClient httpclient = new DefaultHttpClient(); //登录和发表文章先不做讨论 // login(httpclient); // postArticle(httpclient); //注册 regist(httpclient); //关闭Httpclient httpclient.getConnectionManager().shutdown(); } /** * 注册 * @param httpclient * @throws IOException * @throws ClientProtocolException */ private static void regist(DefaultHttpClient httpclient) throws ClientProtocolException, IOException { /** * 下载验证码 */ //验证码生成的大概原理就是随即生成一个验证码字符串(0-9,a-z),然 //后用io流和图片流将验证码写入图片中,最后将图片用servlet writer出 //通过get方式访问该servlet,得到验证码图片 HttpGet httpget = new HttpGet("http://localhost:8080/blog/Checkcode?"+new Random().nextInt(1000)); //执行get请求 HttpResponse response = httpclient.execute(httpget); //我们把返回的信息,保存在本地的一张图片里 File file = new File("regist.jpg"); OutputStream ops = new FileOutputStream(file); response.getEntity().writeTo(ops); ops.close(); /** * 这时我们可以弹出一个JFrame来显示刚刚保存的验证码,我现在做的是 验证码手动输入,当然还可以做程序来读验证码。 */ JFrame frame= new JFrame(); frame.setVisible(false); frame.setBounds(100, 100, 100, 100); frame.setLayout(new FlowLayout()); ImageIcon icon = new ImageIcon("regist.jpg"); frame.add(new JLabel(icon)); frame.setVisible(true); /** * 在控制台输入验证码 */ System.out.println("输入你看到的验证码"); Scanner scr = new Scanner(System.in); String code = scr.nextLine(); EntityUtils.consume(response.getEntity()); //提交注册请求到regist.do,并以post方式提交一个表单,就是刚刚我们看到的注册信息 HttpPost post = new HttpPost("http://localhost:8080/blog/regist.do"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "ydxx1")); nvps.add(new BasicNameValuePair("password", "zhao1")); nvps.add(new BasicNameValuePair("repassword", "zhao1")); nvps.add(new BasicNameValuePair("name", "zhao12")); nvps.add(new BasicNameValuePair("sex", "1")); nvps.add(new BasicNameValuePair("email", "[email protected]")); //把控制台输入的验证码填入 nvps.add(new BasicNameValuePair("ccode", code)); post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = httpclient.execute(post); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } }
5.效果
这里我们就看到了刚通过程序注册的用户
需要注意的是,在程序中必须保证是同一个HttpClient对象,因为它里面保存了jsessionid,而验证码也是保存在session里的,如果是不同的httpclient对象,那你得到的验证码和现在服务端所生成的验证码并不是同一个,就不能注册成功了。
忘记截图了,这张是后来截的
弹出的jframe