现在有一个网址需要注册批量用户,并且每次访问注册页面返回的用户名都是动态的变量
code:
/*
* @author : TF-BJ-C064
* @creation : 2014-7-15 下午9:30:41
* @description :
*
*/
package com.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.http.Cookie;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HttpClientTest {
private Logger log = Logger.getLogger(HttpClientTest.class);
/**
* <p>Title: main</p>
* <p>Description: </p>
* @param args
* @throws ClientProtocolException
* @throws IOException
* @throws HttpException
*/
public static void main(String[] args) throws ClientProtocolException, IOException {
(new HttpClientTest()).SJZYJQ();
}
Header[] sessionID;
String cookies;
Map<String, String> postparams = new HashMap<String, String>();
public void GetCookie(String codeUrl) throws ClientProtocolException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(codeUrl);
HttpResponse httpResponse = httpclient.execute(get);
String html = EntityUtils.toString(httpResponse.getEntity());
// log.info("httpResponse==\n" + html);
log.info("--------------------- end of httpResponse-----------------------------");
sessionID = httpResponse.getHeaders("Set-Cookie");
this.setCookie(sessionID);
get.releaseConnection();
Document doc = Jsoup.parse(html);
Element form = doc.select("form ").first();
log.info("\n"+ form.html());
log.info("------------------end of form---------------------------------");
postparams.put( "regsubmit", "yes");
postparams.put( "formhash", form.select("input[name=formhash]").first().attr("value"));
postparams.put( "referer", Referer);
postparams.put( "activationauth", "");
postparams.put( "agreebbrule", "true");
String name = "karl"+ (int)(Math.random()*1000);
Element e = null;
e = form.select("ul input[tabindex=1]").first();
postparams.put( e.attr("name"), name);//username
String url2 =
"http://www.sjzyjq.com/forum.php?mod=ajax&inajax=yes&infloat=register&handlekey=register&ajaxmenu=1&action=checkusername&username="+name;
get = new HttpGet(url2);
get.addHeader("Referer", Referer);
get.setHeader("Cookie", cookies);
httpResponse = httpclient.execute(get);
html = EntityUtils.toString(httpResponse.getEntity());
sessionID = httpResponse.getHeaders("Set-Cookie");
// this.setCookie(sessionID);
log.info(html);
log.info("------------------end of check username ---------------------------------");
get.releaseConnection();
e = form.select("ul input[tabindex=2]").first();
postparams.put( e.attr("name"), name);//password
e = form.select("ul input[tabindex=3]").first();
postparams.put( e.attr("name"), name );//password
e = form.select("ul input[type=email]").first();
postparams.put( e.attr("name"), name+"@163.com");//email
String url3 = "http://www.sjzyjq.com/forum.php?mod=ajax&inajax=yes&infloat=register&handlekey=register&ajaxmenu=1&action=checkemail&" +
"email="+name+"@163.com";
get = new HttpGet(url2);
get.addHeader("Referer", Referer);
get.setHeader("Cookie", cookies);
httpResponse = httpclient.execute(get);
sessionID = httpResponse.getHeaders("Set-Cookie");
// this.setCookie(sessionID);
html = EntityUtils.toString(httpResponse.getEntity());
log.info(html);
log.info("------------------end of checkemail ---------------------------------");
get.releaseConnection();
}
public void setCookie(Header[] heasers){
cookies = "";
for(Header s : sessionID){
String str = s.toString();
cookies += str.split(":")[1].split(";")[0]+";";
}
log.info("setCookie="+cookies);
log.info("------------------end of setCookie ---------------------------------");
}
String Referer = "http://www.sjzyjq.com/member.php?mod=register";
public void SJZYJQ() throws ClientProtocolException, IOException {
String url = "http://www.sjzyjq.com/member.php?mod=register";
GetCookie(url);
HttpParams httpparams = new BasicHttpParams();
HttpClientParams.setRedirecting(httpparams , true);
HttpClient client = new DefaultHttpClient(httpparams);
HttpPost method = new HttpPost( "http://www.sjzyjq.com/member.php?mod=register&mobile=2" );
List<NameValuePair> params = new ArrayList<NameValuePair>();
Set set = postparams.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry mapentry = (Entry) it.next();
params.add(new BasicNameValuePair( (String) mapentry.getKey(), (String) mapentry.getValue()));
// method.getParams().setParameter( (String) mapentry.getKey(), mapentry.getValue() );
log.info( mapentry.getKey() + "="+ mapentry.getValue() );
}
method.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
log.info("------------------ postparams ------------------------------");
//使用系统提供的默认的恢复策略
// method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
method.addHeader("Referer", Referer);
method.setHeader("Cookie", cookies);
HttpResponse httpResponse = client.execute(method);
log.info("status="+httpResponse.getStatusLine()); //打印服务器返回的状态
String html = EntityUtils.toString(httpResponse.getEntity());
Header[] headers = httpResponse.getAllHeaders();
for(Header h : headers){
log.info("header : "+h.toString());
}
log.info("---------------------------------------------------");
sessionID = httpResponse.getHeaders("Set-Cookie");
this.setCookie(sessionID);
System.out.println( html );
log.info("---------------------------------------------------");
//释放连接
method.releaseConnection();
url = "http://www.sjzyjq.com/forum.php";
HttpGet get = new HttpGet( url );
get.addHeader("Referer", Referer);
get.setHeader("Cookie", cookies);
httpResponse = client.execute(get);
html = EntityUtils.toString(httpResponse.getEntity());
log.info("sessionID= "+sessionID);
//log.info("html= "+html);
log.info("---------------------------------------------------");
}
}