httpclient4初探_完成有验证码的注册

我们知道,网站走的是http/https等协议,通过这些协议,我们能在浏览器中完成网站的注册,登录,发表文章,上传文件等一系列操作。呵呵,讲到用浏览器,意味着我们必须用我们的鼠标,键盘进行操作,如果是你要进行大量的重复性操作的话,是不是就略显疲惫了?比如你要反复的登录,然后去给某人投票?本人以前就在寝室反复地从excel里面复制用户名密码登录,投票,注销,为一个学姐刷票...刷抽筋了。
现在通过httpclient这个apache common这个第三方api,就可以很轻松地把这些工作都交给程序来做。

我这里演示的是注册一个验证码的博客,简单地介绍下httpclient4的使用。
ps.博客是我以前做的,基于jsp/servlet

看下首页,界面也是自己写的,也是我最后一次写界面。




查看该页面的源代码 这里只看form表单

Java代码   收藏代码
  1. <form name="reg" action="regist.do" method="post" onSubmit="return checkdata()">  
  2.         <center>  
  3.                 <table width=600 border=0>  
  4.                 <tr><td>  
  5.                 用户名</td>  
  6.  <td width=60><input type="text" name="username" onBlur="checkUsername()"></td><td width=400><font color=red>*<button οnclick="checkUser()">检测用户名</button><span id="usernameE"></span></font></td>  
  7.             </tr>  
  8.             <tr><td>  
  9.              密码 </td>  
  10.                 <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>  
  11.             <tr><td>  
  12.              重复密码 </td>  
  13.              <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>  
  14.              <tr><td>  
  15.             姓名</td>  
  16.              <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>  
  17.             <tr><td>  
  18.             性别      </td>  
  19.             <td width=200>   
  20.               <label>  
  21.               <input type="radio" name="sex" value="0" >  
  22. 男</label>  
  23.               <label>  
  24.                 <input type="radio" name="sex" value="1">  
  25.   
  26. 女</label>  
  27.   
  28.               <label>  
  29.   
  30.               <input type="radio" name="sex" value="2" checked="checked">  
  31.   
  32. 不详</label></td> </tr>  
  33.   
  34.             <tr><td>  
  35.   
  36.             邮箱</td>  
  37.   
  38.                <td width=60> <input type="text" name="email" οnblur="checkMail()"></td><td width=300><font color=red>*<span id="mailE"></span></font></p></td></tr>  
  39.   
  40.             <tr><td>  
  41.   
  42.                验证码</td><td width=60><input type="text" name="ccode" οnblur="checkCode()"/></td>  
  43.   
  44.             <td width=300> <img id="code" src="Checkcode?"+1335096054921 />  
  45.   
  46.     [url=javascript:;]看不清,换一个[/url]</p></td></tr>  
  47.   
  48.               <tr><td>  
  49.   
  50.                 <input type="submit" name="submit" value="提交">  
  51.   
  52.                 </label>    
  53.   
  54.                 <label>  
  55.   
  56.                 <input name="reset" type="reset" id="reset" value="重置">  
  57.   
  58.                 </label>  
  59.   
  60.                 </td></tr>  
  61.   
  62.                 </table>  
  63.   
  64.                 </center>  
  65.   
  66.               </form>  


@这个表单是要提交到 regist.do 这个servlet,提交的信息有如下属性:
username------用户名
password------密码
name------姓名
sex------性别
email------邮箱
ccode------验证码

需要注意的是:
验证码</td><td width=60><input type="text" name="ccode" οnblur="checkCode()"/></td>

<td width=300> <img id="code" src="Checkcode?"+1335096054921 />
<img>连接的是一个servlet:Checkcode,后面跟的是随机数,防止得到缓存数据

知道了如上信息后,我们就可以开始编写程序了。
首先创建一个java project
导入httpclient所需要的Jar包 我这里用的是最新版
下载地址 http://hc.apache.org/downloads.cgi


上代码了

Java代码   收藏代码
  1. public class FormDemo {  
  2.     public static void main(String[] args) throws ClientProtocolException,  
  3.             IOException {  
  4.         DefaultHttpClient httpclient = new DefaultHttpClient();  
  5.         //登录和发表文章先不做讨论  
  6. //      login(httpclient);  
  7. //      postArticle(httpclient);  
  8.                 //注册  
  9.         regist(httpclient);  
  10.                 //关闭Httpclient  
  11.         httpclient.getConnectionManager().shutdown();  
  12.   
  13.     }  
  14.     /** 
  15.      * 注册 
  16.      * @param httpclient 
  17.      * @throws IOException  
  18.      * @throws ClientProtocolException  
  19.      */  
  20.     private static void regist(DefaultHttpClient httpclient) throws ClientProtocolException, IOException {  
  21.         /** 
  22.          * 下载验证码 
  23.          */  
  24.                 //验证码生成的大概原理就是随即生成一个验证码字符串(0-9,a-z),然  
  25.                 //后用io流和图片流将验证码写入图片中,最后将图片用servlet writer出                                                                                                                                       
  26.                   //通过get方式访问该servlet,得到验证码图片         
  27. HttpGet httpget = new HttpGet("http://localhost:8080/blog/Checkcode?"+new Random().nextInt(1000));  
  28.                  //执行get请求  
  29.         HttpResponse response =  httpclient.execute(httpget);  
  30.                 //我们把返回的信息,保存在本地的一张图片里  
  31.         File file = new File("regist.jpg");  
  32.         OutputStream ops = new FileOutputStream(file);  
  33.         response.getEntity().writeTo(ops);  
  34.         ops.close();  
  35.         /** 
  36.          * 这时我们可以弹出一个JFrame来显示刚刚保存的验证码,我现在做的是                   验证码手动输入,当然还可以做程序来读验证码。 
  37.          */  
  38.         JFrame frame= new JFrame();  
  39.         frame.setVisible(false);  
  40.         frame.setBounds(100100100100);  
  41.         frame.setLayout(new FlowLayout());  
  42.         ImageIcon icon  = new ImageIcon("regist.jpg");  
  43.         frame.add(new JLabel(icon));  
  44.         frame.setVisible(true);  
  45.           
  46.         /** 
  47.          * 在控制台输入验证码 
  48.          */  
  49.         System.out.println("输入你看到的验证码");  
  50.         Scanner scr = new Scanner(System.in);  
  51.         String code = scr.nextLine();  
  52.           
  53.         EntityUtils.consume(response.getEntity());  
  54.         //提交注册请求到regist.do,并以post方式提交一个表单,就是刚刚我们看到的注册信息  
  55.         HttpPost post = new HttpPost("http://localhost:8080/blog/regist.do");  
  56.         List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
  57.         nvps.add(new BasicNameValuePair("username""ydxx1"));  
  58.         nvps.add(new BasicNameValuePair("password""zhao1"));  
  59.         nvps.add(new BasicNameValuePair("repassword""zhao1"));  
  60.         nvps.add(new BasicNameValuePair("name""zhao12"));  
  61.         nvps.add(new BasicNameValuePair("sex""1"));  
  62.         nvps.add(new BasicNameValuePair("email""dsfsd@126.com"));  
  63.                 //把控制台输入的验证码填入  
  64.         nvps.add(new BasicNameValuePair("ccode", code));  
  65.         post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
  66.           
  67.         response = httpclient.execute(post);  
  68.         System.out.println(response.getStatusLine());  
  69.         EntityUtils.consume(response.getEntity());  
  70.     }  
  71. }  




这里我们就看到了刚通过程序注册的用户


需要注意的是,在程序中必须保证是同一个HttpClient对象,因为它里面保存了jsessionid,而验证码也是保存在session里的,如果是不同的httpclient对象,那你得到的验证码和现在服务端所生成的验证码并不是同一个,就不能注册成功了。

忘记截图了,这张是后来截的

弹出的jframe


转载:http://zjfhw.iteye.com/blog/1493988

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值