开发学院软件开发Java 利用httpclient模拟站点的登录发帖回复 阅读

利用httpclient模拟站点的登录发帖回复

 2009-09-22 00:00:00 来源:WEB开发网   
核心提示: 封装登录的类SiteLogin:1/***//**2*Copyright(C):20093*@author陈新汉4*Aug24,20093:03:00PM5*/67/***//**8*站点登录9*/10publicclassSiteLoginextendsAbstractMethodAdapte

封装登录的类SiteLogin:

1/** *//**
2 *  Copyright (C): 2009
3 *  @author 陈新汉
4 *  Aug 24, 2009 3:03:00 PM
5 */
6
7/** *//**
8 * 站点登录
9 */
10public class SiteLogin extends AbstractMethodAdapter
11{
12    private HttpMethodBase method;
13    private boolean ispost=true;
14    protected BrowserContext context; //当前的浏览器进程上下文
15   
16    public BrowserContext getContext() {
17        return context;
18    }
19   
20    /** *//**
21     * 构造函数
22     * @param context
23     * @param url
24     * @param ispost 设置是否POST方式提交,默认为POST
25     */
26    public SiteLogin(BrowserContext context,String url,boolean ispost) {
27        super();
28        this.context = context;
29        this.ispost=ispost;
30        method = this.ispost?new PostMethod(url):new GetMethod(url);
31    }
32   
33    public SiteLogin(BrowserContext context,String url) {
34        this(context,url,true);
35    }
36
37    public String login(Siteusers user)
38    {
39        int statusCode=0;
40        if(this.ispost && this.hasRequestParameters()){
41            ((PostMethod)method).setRequestBody(this.getRequestParams());
42        }
43       
44        if(this.hasExtraRequestHeaders()){
45            this.addExtraRequestHeaders(method,this.getExtraRequestHeaders());
46        }
47        context.setCommonMethodRequestHeaders(method);
48        try
49        {
50            if(context.getCookies()!=null){
51                //printCookies();
52                context.getClient().getState().addCookies(context.getCookies());
53                method.addRequestHeader("Cookie", context.getCookies().toString());
54            }
55            statusCode = context.getClient().executeMethod(method);
56            context.setCookies(context.getClient().getState().getCookies());
57            String responseString = method.getResponseBodyAsString();
58            //System.out.println(responseString);
59            method.releaseConnection();
60            if(statusCode==HttpStatus.SC_OK){
61                System.out.println("登录成功");
62                return responseString;
63            }
64            else if(statusCode==302 ||statusCode==301){
65                System.out.println("登录成功,页面重定向");
66                String url=method.getResponseHeader("Location").getValue();
67                return context.redirectToURL(url);
68            }
69            else{
70                System.out.println("登录失败,状态码:"+statusCode);
71            }
72        }catch(Exception e){
73            e.printStackTrace();
74        }finally{
75            if(method!=null)
76                method.releaseConnection();
77        }
78        return null;
79    }
80}
81

上一页  1 2 3 4 5  下一页

Tags:利用 httpclient 模拟

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接