模拟站点的自动登录发帖回复
2009-09-21 00:00:00 来源:WEB开发网其中, BrowserContext 类代表浏览器上下文对象,维护 HttpClient 链接和 Cookies 。 KaixinSitePost 是负责实现开心网的具体登录发帖回复逻辑的类。
BrowserContext 的代码如下:
Java代码
/** */ /**
* 浏览器进程上下文
*/
public class BrowserContext
{
private HttpClient client; // 注意:每个站点和每个用户,对应一个单独的BrowserContext对象
private Cookie[] cookies = new Cookie[ 0 ]; // 维护Cookies
private Proxyips proxyip = null ; // 当前的代理IP
private Siteusers user = null ; // 当前的登录用户
public Cookie[] getCookies() {
return cookies;
}
public void setCookies(Cookie[] cookies) {
this .cookies = cookies;
}
public void addCookie(Cookie c) {
if (cookies != null && cookies.length > 0 ) {
Cookie[] others = new Cookie[cookies.length + 1 ];
System.arraycopy(cookies, 0 , others, 0 , cookies.length);
others[others.length - 1 ] = c;
cookies = others;
} else {
cookies = new Cookie[ 1 ];
cookies[ 0 ] = c;
}
}
public Proxyips getProxyip() {
return proxyip;
}
public void setProxyip(Proxyips proxyip) {
this .proxyip = proxyip;
if ( this .proxyip != null ) {
client.getHostConfiguration().setProxy(proxyip.getIp(),proxyip.getPort());
client.getParams().setAuthenticationPreemptive( true );
// 如果代理需要密码验证,这里设置用户名密码
// client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("",""));
}
}
public HttpClient getClient() {
return client;
}
public Siteusers getUser() {
return user;
}
public void setUser(Siteusers user) {
this .user = user;
}
private BrowserContext(Site site) {
super ();
Protocol myhttps = new Protocol( " https " , new MySecureProtocolSocketFactory(), 443 );
Protocol.registerProtocol( " https " , myhttps);
client = new HttpClient();
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
// 设置连接超时时间(单位毫秒)
// managerParams.setConnectionTimeout(50000);
// 设置读数据超时时间(单位毫秒)
// managerParams.setSoTimeout(120000);
initForSiteVisit(site.getSite(),site.getPort(),site.getCharset());
}
public BrowserContext(Site site,Proxyips proxyip) {
this (site);
this .setProxyip(proxyip);
}
private void initForSiteVisit(String siteurl, int port,String charset) {
client.getHostConfiguration().setHost(siteurl, port, " http " );
// 解决中文乱码问题,和指定网站的页面编码一致
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
}
// 查看cookie信息
public void printCookies()
{
System.out.println( " ---------------Cookie---------------- " );
if (cookies != null ) {
for (Cookie c:cookies) {
System.out.println(c.getName() + " : " + c.getValue());
}
} else {
System.out.println( " 没有设置Cookies " );
}
System.out.println( " ---------------Cookie---------------- " );
}
public void setCommonMethodRequestHeaders(HttpMethodBase method)
{
method.setRequestHeader( " Accept " , " */* " );
// method.setRequestHeader("Accept-Language", "zh-cn");
// method.setRequestHeader("Accept-Encoding", "gzip,deflate");
method.setRequestHeader( " User-Agent " , " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;) " );
// 设置非常重要
method.setRequestHeader( " Connection " , " Keep-Alive " );
}
public String redirectToURL(String url) throws IOException
{
if (url != null ) {
try {
System.out.println( " 页面重定向到: " + url);
String responseString = this .doCommonVisitWithURL(url);
// System.out.println(responseString);
return responseString;
} catch (IOException e) {
System.out.println( " 重定向: " + url + " 出错 " );
}
} else {
System.out.println( " redirect url is null " );
}
return null ;
}
public String doCommonVisitWithURL(String url) throws IOException {
GetMethod get = new GetMethod(url);
return this .doGet(get);
}
public String doPost(ExpectContinueMethod post) throws IOException
{
if (post == null )
return null ;
try
{
if (getCookies() != null ) {
// printCookies();
client.getState().addCookies(cookies);
post.addRequestHeader( " Cookie " ,getCookies().toString());
// System.out.println(post.getRequestHeader("Cookie").getValue());
}
setCommonMethodRequestHeaders(post);
int statusCode = client.executeMethod(post);
cookies = client.getState().getCookies();
System.out.println(statusCode);
// System.out.println(post.getResponseHeader("Location"));
String responseString = post.getResponseBodyAsString();
System.out.println(responseString);
printCookies();
post.releaseConnection();
if (statusCode == 301 || statusCode == 302 ) {
redirectToURL(post.getResponseHeader( " Location " ).getValue());
}
return responseString;
}
finally {
if (post != null )
post.releaseConnection();
}
}
public String doGet(GetMethod get) throws IOException
{
if (get == null )
return null ;
if (cookies != null ) {
// printCookies();
client.getState().addCookies(cookies);
get.addRequestHeader( " Cookie " ,cookies.toString());
}
try {
setCommonMethodRequestHeaders(get);
int statusCode = client.executeMethod(get);
cookies = client.getState().getCookies(); // 重新保存Cookies
printCookies();
System.out.println(statusCode);
if (statusCode == 301 || statusCode == 302 ) {
redirectToURL(get.getResponseHeader( " Location " ).getValue());
}
String responseString = get.getResponseBodyAsString();
// System.out.println(responseString);
return responseString;
}
finally {
if (get != null )
get.releaseConnection();
}
}
public String getRedirectURL(String content)
{
if (content != null && content.indexOf( " window.location=\ "" )!=-1){
int begin = content.indexOf( " window.location=\ "" );
int end = content.indexOf( " \ "" , begin+17);
return content.substring(begin + 17 ,end);
}
return null ;
}
}
更多精彩
赞助商链接