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

* 注冊原理實現(xiàn)是,封裝一些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 ...{

//執(zhí)行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) ...{

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

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

e.printStackTrace();


} catch (IOException e) ...{

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

e.printStackTrace();


} finally ...{

//釋放連接

postMethod.releaseConnection();

}


}

}
