package org.job.four;
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.job.util.Logger;
/** *//**
* <b>类名:AutoRegistGmail.java</b> </br>
* 编写日期: 2007-3-27 <br/>
* 程序功能描述:对Gami自动注册的启动类 <br/>
* 注册原理实现是,封装一些gmail比填的字段,然后再通过HttpClient向Gmail请求,
* 请求完毕后分析结果
* Demo: <br/>
* Bug: <br/>
*
* 程序变更日期 :<br/>
* 变更作者 :<br/>
* 变更说明 :<br/>
*
* @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
*/
public class AutoRegistGmail ...{
private static String postRegistUrl = "https://www.google.com/accounts/NewAccount";
private static Logger logger = Logger.getLogger(AutoRegistGmail.class);
public static void main(String[] args) ...{
autoRegist();
}
public final static void autoRegist() ...{
logger.debug("开始自动注册Gmail.....");
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(postRegistUrl);
Member member = Member.getDefaultMember();
// 填入各个表单域的值
NameValuePair[] data = ...{
new NameValuePair("Email", member.getEmail()),
new NameValuePair("Passwd", member.getPasswd()),
new NameValuePair("PasswdAgain", member.getPasswdAgain()),
new NameValuePair("FirstName", member.getFirstName()),
new NameValuePair("Loc", member.getLoc()),
new NameValuePair("newaccountcaptcha", member
.getIdentityAnswer()),
new NameValuePair("IdentityAnswer", member
.getNewaccountcaptcha()),
new NameValuePair("Selection", member.getSelection()),
new NameValuePair("SecondaryEmail", member.getSecondaryEmail()),
new NameValuePair("continue", member.getContinue()),
new NameValuePair("dsh", member.getDsh()),
new NameValuePair("service", member.getService()) };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
try ...{
//执行getMethod
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) ...{
System.err.println("Method failed: "
+ postMethod.getStatusLine());
}
//读取内容
byte[] responseBody = postMethod.getResponseBody();
//处理内容
System.out.println(new String(responseBody, "UTF-8"));
} catch (HttpException e) ...{
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) ...{
//发生网络异常
e.printStackTrace();
} finally ...{
//释放连接
postMethod.releaseConnection();
}
}
}