
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/>

* 程序功能描述:對(duì)Gami自動(dòng)注冊(cè)的啟動(dòng)類 <br/>

* 注冊(cè)原理實(shí)現(xiàn)是,封裝一些gmail比填的字段,然后再通過HttpClient向Gmail請(qǐng)求,

* 請(qǐng)求完畢后分析結(jié)果

* 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("開始自動(dòng)注冊(cè)Gmail.....");

// 構(gòu)造HttpClient的實(shí)例

HttpClient httpClient = new HttpClient();

PostMethod postMethod = new PostMethod(postRegistUrl);

Member member = Member.getDefaultMember();

// 填入各個(gè)表單域的值


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 ...{

//執(zhí)行g(shù)etMethod

int statusCode = httpClient.executeMethod(postMethod);


if (statusCode != HttpStatus.SC_OK) ...{

System.err.println("Method failed: "

+ postMethod.getStatusLine());

}

//讀取內(nèi)容

byte[] responseBody = postMethod.getResponseBody();

//處理內(nèi)容

System.out.println(new String(responseBody, "UTF-8"));


} catch (HttpException e) ...{

//發(fā)生致命的異常,可能是協(xié)議不對(duì)或者返回的內(nèi)容有問題

System.out.println("Please check your provided http address!");

e.printStackTrace();


} catch (IOException e) ...{

//發(fā)生網(wǎng)絡(luò)異常

e.printStackTrace();


} finally ...{

//釋放連接

postMethod.releaseConnection();

}


}

}
