读书人

施用HttpClient实现126邮箱的登录

发布时间: 2012-12-20 09:53:21 作者: rapoo

使用HttpClient实现126邮箱的登录
但是目前不知道该如何将进入126后的页面下载下来,希望大家多多指教.

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;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.PostMethod;import org.junit.Test;public class LoginMail {private String USERNAME = "raindreams";private String PASSWORD = "password";private String LOGINURL = "http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26language%3D0%26style%3D-1";private InputStream response = null;private FileOutputStream fileoutput = null;private BufferedInputStream bufferinput = null;private BufferedOutputStream bufferoutput=null;@Testpublic void login() throws HttpException, IOException  {HttpClient httpClient = new HttpClient();// PostMethod authpost = new PostMethod("http://www.126.com");PostMethod authpost = new PostMethod(LOGINURL);// 注意上面的地址!authpost.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");NameValuePair user = new NameValuePair("user", USERNAME);NameValuePair pwd = new NameValuePair("password", PASSWORD);NameValuePair username = new NameValuePair("username", USERNAME+ "@126.com");authpost.setRequestBody(new NameValuePair[] { user, pwd, username });int status = httpClient.executeMethod(authpost);//System.out.println(authpost.getResponseBodyAsString());//System.out.println(status);try {if(status==HttpStatus.SC_OK){System.out.println("登录成功!");}//缓冲输入输出.response = authpost.getResponseBodyAsStream();bufferinput = new BufferedInputStream(response);fileoutput = new FileOutputStream(new File("D:\\index.html"));            bufferoutput=new BufferedOutputStream(fileoutput);byte[] temp = new byte[1024];int len=0;while ((len = bufferinput.read(temp)) > 0) {bufferoutput.write(temp, 0, len);}} finally {//关闭输入输出流.if (response != null)response.close();if (fileoutput != null)fileoutput.close();}}}

读书人网 >编程

热点推荐