htmlunitは小さな例を使用します:Analog Sina-メールログイン、パスワードの変更、アカウントのニックネームの変更



Htmlunit Use Small Example



技術的なコンテンツではなく、htmlunitに公開されたJavaで大量メール送信ソフトウェアを作成するには、htmlunitエクスペリエンスを使用するだけです。

import java.io.IOException import java.net.MalformedURLException import java.net.URL import java.util.Arrays import java.util.regex.Matcher import java.util.regex.Pattern import com.gargoylesoftware.htmlunit.BrowserVersion import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException import com.gargoylesoftware.htmlunit.HttpMethod import com.gargoylesoftware.htmlunit.WebClient import com.gargoylesoftware.htmlunit.WebRequest import com.gargoylesoftware.htmlunit.html.HtmlForm import com.gargoylesoftware.htmlunit.html.HtmlInput import com.gargoylesoftware.htmlunit.html.HtmlPage import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput import com.gargoylesoftware.htmlunit.util.NameValuePair public class MySina { private WebClient client private WebRequest request private String sinaLoginUrl = 'http://mail.sina.com.cn/cgi-bin/login.php' private String hostSinaUrl = '' public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { String username = '***' String password = '***' String newpassword = '***' String nickname = '***' MySina mySina = new MySina() if (mySina.mailLoginBySina (username, password)) {// Login mySina.updatePwdBySina (password, newpassword) // Change Password mySina.updateNickName (nickname) // modify account nickname }else{ System.out.println ( '! Login failed please check your username and password are correct!') } } public MySina() { client = new WebClient(BrowserVersion.INTERNET_EXPLORER_8) client.setJavaScriptEnabled(false) } /** * * Change the account nickname * * @param nickname * Nickname * @return boolean * @throws FailingHttpStatusCodeException * @throws IOException */ public boolean updateNickName(String nickname) throws FailingHttpStatusCodeException, IOException { String sinaSetUrl = hostSinaUrl + 'basic/setting_account' request = new WebRequest(new URL(sinaSetUrl),HttpMethod.POST) request.setCharset('utf-8') request.setRequestParameters(Arrays.asList( new NameValuePair('nickname', nickname), new NameValuePair('pop3', 'on'), new NameValuePair('imap', 'on'))) client.getPage(request) HtmlPage p = client.getPage(hostSinaUrl + 'classic/index.php') if (p.getBody().getTextContent() .indexOf(''NickName':'' + nickname + ''') > 0) { return true } else { return false } } /** * * change Password * * @param oldpassword * old password * @param newpassword * new password * @return boolean * @throws FailingHttpStatusCodeException * @throws IOException */ public boolean updatePwdBySina(String oldpassword, String newpassword) throws FailingHttpStatusCodeException, IOException { String sinaSetUrl = 'http://login.sina.com.cn/member/security/password.php' request = new WebRequest(new URL(sinaSetUrl),HttpMethod.POST) request.setCharset('gbk') request.setRequestParameters(Arrays.asList( new NameValuePair('pass',oldpassword), new NameValuePair('pass1', newpassword), new NameValuePair('pass2', newpassword))) HtmlPage p = client.getPage(request) if (p.getBody (). getTextContent (). indexOf ( 'Your password changed successfully')> 0) { return true } else { return false } } /** * log in * * @param username * username * @param password * Password * @return boolean * @throws FailingHttpStatusCodeException * @throws MalformedURLException * @throws IOException */ public boolean mailLoginBySina(String username, String password) throws FailingHttpStatusCodeException, MalformedURLException, IOException { HtmlPage loginPage = client.getPage(sinaLoginUrl) HtmlForm loginForm = loginPage.getFormByName('free') HtmlInput u = loginForm.getInputByName('u') HtmlInput psw = loginForm.getInputByName('psw') HtmlSubmitInput loginButton = loginForm.getInputByName ( 'login') u.setValueAttribute(username) psw.setValueAttribute(password) HtmlPage result = loginButton.click() String resultUrl = result.getUrl().toString() if (resultUrl.indexOf('classic/index.php') > 0) { String regex = 'http://(.*?)/' hostSinaUrl = myRegex(resultUrl, regex, null) if (hostSinaUrl.length() > 0) { return true } else { return false } } else { return false } } /** * * Replace regular match * * @param str * @param reg * @param replace * @return */ public String myRegex(String str, String reg, String[] replace) { String result = null Matcher m = Pattern.compile(reg).matcher(str) while (m.find()) { result = m.group() if (replace != null && replace.length > 0) { for (String s : replace) { result = result.replace(s, '') } } } return result } }

技術的なコンテンツはそれほど多くありません。htmlunitエクスペリエンスを使用してください。



htmlunitjarファイルとAPIを添付します http://files.cnblogs.com/skillCoding/htmlunit-2.9-bin.zip

複製:https://www.cnblogs.com/skillCoding/archive/2011/09/07/2169982.html