利用httpclient模拟站点的登录发帖回复
2009-09-22 00:00:00 来源:WEB开发网其中, BrowserContext 类代表浏览器上下文对象,维护 HttpClient 链接和 Cookies 。 KaixinSitePost 是负责实现开心网的具体登录发帖回复逻辑的类。
BrowserContext 的代码如下:
1 /** */ /**
2 * Copyright (C): 2009
3 * @author 陈新汉
4 * Aug 24, 2009 3:09:00 PM
5 */
6
7 /** */ /**
8 * 浏览器进程上下文
9 */
10 public class BrowserContext
11 {
12 private HttpClient client; // 注意:每个站点和每个用户,对应一个单独的BrowserContext对象
13 private Cookie[] cookies = new Cookie[ 0 ]; // 维护Cookies
14 private Proxyips proxyip = null ; // 当前的代理IP
15 private Siteusers user = null ; // 当前的登录用户
16
17 public Cookie[] getCookies() {
18 return cookies;
19 }
20
21 public void setCookies(Cookie[] cookies) {
22 this .cookies = cookies;
23 }
24
25 public void addCookie(Cookie c) {
26 if (cookies != null && cookies.length > 0 ) {
27 Cookie[] others = new Cookie[cookies.length + 1 ];
28 System.arraycopy(cookies, 0 , others, 0 , cookies.length);
29 others[others.length - 1 ] = c;
30 cookies = others;
31 } else {
32 cookies = new Cookie[ 1 ];
33 cookies[ 0 ] = c;
34 }
35 }
36
37 public Proxyips getProxyip() {
38 return proxyip;
39 }
40
41 public void setProxyip(Proxyips proxyip) {
42 this .proxyip = proxyip;
43 if ( this .proxyip != null ) {
44 client.getHostConfiguration().setProxy(proxyip.getIp(),proxyip.getPort());
45 client.getParams().setAuthenticationPreemptive( true );
46 // 如果代理需要密码验证,这里设置用户名密码
47 // client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("",""));
48 }
49 }
50
51 public HttpClient getClient() {
52 return client;
53 }
54
55 public Siteusers getUser() {
56 return user;
57 }
58
59 public void setUser(Siteusers user) {
60 this .user = user;
61 }
62
63 private BrowserContext(Site site) {
64 super ();
65 Protocol myhttps = new Protocol( " https " , new MySecureProtocolSocketFactory(), 443 );
66 Protocol.registerProtocol( " https " , myhttps);
67 client = new HttpClient();
68 client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
69 HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
70 // 设置连接超时时间(单位毫秒)
71 // managerParams.setConnectionTimeout(50000);
72 // 设置读数据超时时间(单位毫秒)
73 // managerParams.setSoTimeout(120000);
74 initForSiteVisit(site.getSite(),site.getPort(),site.getCharset());
75 }
76
77 public BrowserContext(Site site,Proxyips proxyip) {
78 this (site);
79 this .setProxyip(proxyip);
80 }
81
82 private void initForSiteVisit(String siteurl, int port,String charset) {
83 client.getHostConfiguration().setHost(siteurl, port, " http " );
84 // 解决中文乱码问题,和指定网站的页面编码一致
85 client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
86 }
87
88 // 查看cookie信息
89 public void printCookies()
90 {
91 System.out.println( " ---------------Cookie---------------- " );
92 if (cookies != null ) {
93 for (Cookie c:cookies) {
94 System.out.println(c.getName() + " : " + c.getValue());
95 }
96 } else {
97 System.out.println( " 没有设置Cookies " );
98 }
99 System.out.println( " ---------------Cookie---------------- " );
100 }
101
102 public void setCommonMethodRequestHeaders(HttpMethodBase method)
103 {
104 method.setRequestHeader( " Accept " , " */* " );
105 // method.setRequestHeader("Accept-Language", "zh-cn");
106 // method.setRequestHeader("Accept-Encoding", "gzip,deflate");
107 method.setRequestHeader( " User-Agent " , " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;) " );
108 // 设置非常重要
109 method.setRequestHeader( " Connection " , " Keep-Alive " );
110 }
111
112 public String redirectToURL(String url) throws IOException
113 {
114 if (url != null ) {
115 try {
116 System.out.println( " 页面重定向到: " + url);
117 String responseString = this .doCommonVisitWithURL(url);
118 // System.out.println(responseString);
119 return responseString;
120 } catch (IOException e) {
121 System.out.println( " 重定向: " + url + " 出错 " );
122 }
123 } else {
124 System.out.println( " redirect url is null " );
125 }
126 return null ;
127 }
128
129 public String doCommonVisitWithURL(String url) throws IOException {
130 GetMethod get = new GetMethod(url);
131 return this .doGet(get);
132 }
133
134 public String doPost(ExpectContinueMethod post) throws IOException
135 {
136 if (post == null )
137 return null ;
138 try
139 {
140 if (getCookies() != null ) {
141 // printCookies();
142 client.getState().addCookies(cookies);
143 post.addRequestHeader( " Cookie " ,getCookies().toString());
144 // System.out.println(post.getRequestHeader("Cookie").getValue());
145 }
146 setCommonMethodRequestHeaders(post);
147 int statusCode = client.executeMethod(post);
148 cookies = client.getState().getCookies();
149 System.out.println(statusCode);
150 // System.out.println(post.getResponseHeader("Location"));
151 String responseString = post.getResponseBodyAsString();
152 System.out.println(responseString);
153 printCookies();
154 post.releaseConnection();
155 if (statusCode == 301 || statusCode == 302 ) {
156 redirectToURL(post.getResponseHeader( " Location " ).getValue());
157 }
158 return responseString;
159 }
160 finally {
161 if (post != null )
162 post.releaseConnection();
163 }
164 }
165
166 public String doGet(GetMethod get) throws IOException
167 {
168 if (get == null )
169 return null ;
170 if (cookies != null ) {
171 // printCookies();
172 client.getState().addCookies(cookies);
173 get.addRequestHeader( " Cookie " ,cookies.toString());
174 }
175 try {
176 setCommonMethodRequestHeaders(get);
177 int statusCode = client.executeMethod(get);
178 cookies = client.getState().getCookies(); // 重新保存Cookies
179 printCookies();
180 System.out.println(statusCode);
181 if (statusCode == 301 || statusCode == 302 ) {
182 redirectToURL(get.getResponseHeader( " Location " ).getValue());
183 }
184 String responseString = get.getResponseBodyAsString();
185 // System.out.println(responseString);
186 return responseString;
187 }
188 finally {
189 if (get != null )
190 get.releaseConnection();
191 }
192 }
193
194 public String getRedirectURL(String content)
195 {
196 if (content != null && content.indexOf( " window.location=\ "" )!=-1){
197 int begin = content.indexOf( " window.location=\ "" );
198 int end = content.indexOf( " \ "" , begin+17);
199 return content.substring(begin + 17 ,end);
200 }
201 return null ;
202 }
203 }
Tags:利用 httpclient 模拟
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接