使用httpclient开源包模拟post注册用户

//闲的蛋疼,浏览网页时发现某网站不需要验验证码就可以注册用户,所以随便写了点代码
//帮它提高点人气,顺便把源代码给大家分享下
package org.apache.httpCilent;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * Simulation form submit Post
 * 
 * @author Hewei
 * 
 */
public class DoPost {
	/**
	 * 
	 * @param url
	 *            actionUrl
	 * @param data
	 *            you want to post data
	 * @return
	 */
	public synchronized String methodPost(String url, List<NameValuePair> data)
			throws Exception {
		String returnString = "";// 服务器响应
		HttpClient client = new DefaultHttpClient(); // 实例化httpClient对象
		HttpPost post = new HttpPost(url);// 实例化httpPost对象
		post.setEntity(new UrlEncodedFormEntity(data, "UTF-8")); //设置post提价的data
		HttpResponse response = client.execute(post);// 执行提交 获得httpResponse对象
//		HttpEntity httpEntity = response.getEntity();
//		System.out.println("服务器响应文本类型" + httpEntity.getContentType());
//
//		InputStream input = httpEntity.getContent(); // 获得输入流
//		BufferedReader bf = new BufferedReader(new InputStreamReader(input)); //读入输入流
//
//		String temp;
//		while (null != (temp = bf.readLine())) {
//			returnString = returnString + temp;
//		}
//		bf.close();
//		input.close();
		System.out.println("已提交注册用户:"+data.get(0).getValue());
		return "suceess";
	}
	/*封装提交数据*/
	public synchronized List<NameValuePair> createPostData(int nowInt, int sumTimes) {
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		String temp = "";
		if (nowInt < sumTimes) {
			for (int j = 0; j < String.valueOf(sumTimes).length()
					- String.valueOf(nowInt).length(); j++) {
				temp = temp + "0";
			}
			temp = temp + nowInt;
		} else {
			temp = String.valueOf(sumTimes);
		}
		NameValuePair UserName = new BasicNameValuePair("UserName", "xx"+ temp);
		NameValuePair NickName = new BasicNameValuePair("NickName", "oo"+ temp);
		NameValuePair sex = new BasicNameValuePair("sex", "1");
		NameValuePair Password = new BasicNameValuePair("password", "adj" + temp);
		NameValuePair PwdConfirm = new BasicNameValuePair("PwdConfirm", "adj"
				+ temp);
		NameValuePair Question = new BasicNameValuePair("Question", "helloWord");
		NameValuePair Answer = new BasicNameValuePair("Answer", "yeal");
		NameValuePair Email = new BasicNameValuePair("Email", temp+"@Emainxx.com");
		postData.add(UserName);
		postData.add(NickName);
		postData.add(sex);
		postData.add(Password);
		postData.add(PwdConfirm);
		postData.add(Question);
		postData.add(Answer);
		postData.add(Email);
		return postData;
	}

}

package org.apache.httpCilent;

public class PostNewUser implements Runnable {
	private DoPost post = new DoPost();
	private String url = "http://www.xxxx.com/Reg/User_RegPost.asp";
	@Override
	public synchronized void run() {
		try{
		for(int i=1;i<100000;i++){
			Thread.sleep(10);
			System.out.println(post.methodPost(url, post.createPostData(i, 100000)));
		}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args){//写到这里想了下,用线程
   //提交效果应该好点
		for(int i=0;i<5;i++){		
			Thread t = new Thread(new PostNewUser());
			t.start();
		}
	}
}



你可能感兴趣的:(java httpClient)