import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import sun.util.logging.resources.logging;
public class TestLogin {
public static String getHtmlSource(String url,String charset) throws IOException {
//登陆处理
HttpClient httpClient = new HttpClient();
HttpClientParams httparams = new HttpClientParams();
httpClient.setParams(httparams);
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; TheWorld)");
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
// httpClient.getHostConfiguration().setHost("ifeng.com", 80);
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
System.setProperty("apache.commons.httpclient.cookiespec","COMPATIBILITY");
//读取用户名和密码
PostMethod postMethod = new PostMethod("https://reg.163.com/logins.jsp");
NameValuePair username2 = new NameValuePair("username","xxxxx");
NameValuePair password2 = new NameValuePair("password","xxxxx");
NameValuePair savelogin = new NameValuePair("savelogin","1");
NameValuePair product = new NameValuePair("product","t");
NameValuePair type = new NameValuePair("type","1");
NameValuePair url2 = new NameValuePair("url","http://t.163.com/session/first");
NameValuePair[] data = {username2, password2, url2 ,savelogin,product,type};
postMethod.setRequestBody(data);
httpClient.executeMethod(postMethod);
//获取目标url的源代码
// HttpClient client1 = new HttpClient();
//
for(int i=0;i<httpClient.getState().getCookies().length;i++){
System.out.println("--------"+httpClient.getState().getCookies()[i]);
}
GetMethod getMethod = new GetMethod(url);
httpClient.executeMethod(getMethod);
InputStream response = getMethod.getResponseBodyAsStream();
String result = StreamOut(response, charset);
response.close();
System.out.println(result);
return result;
}
public static String StreamOut(InputStream stream, String charset) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream,charset));
String tempbf;
StringBuffer html = new StringBuffer(100);
while ((tempbf = br.readLine()) != null) {
html.append(tempbf + "\n");
}
return html.toString();
}
public static void main(String[] args) throws IOException {
getHtmlSource("http://t.163.com/2699910068","utf-8");
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import sun.util.logging.resources.logging;
public class TestLogin {
public static String getHtmlSource(String url,String charset) throws IOException {
//登陆处理
HttpClient httpClient = new HttpClient();
HttpClientParams httparams = new HttpClientParams();
httpClient.setParams(httparams);
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; TheWorld)");
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
// httpClient.getHostConfiguration().setHost("ifeng.com", 80);
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
System.setProperty("apache.commons.httpclient.cookiespec","COMPATIBILITY");
//读取用户名和密码
PostMethod postMethod = new PostMethod("https://reg.163.com/logins.jsp");
NameValuePair username2 = new NameValuePair("username","xxxxx");
NameValuePair password2 = new NameValuePair("password","xxxxx");
NameValuePair savelogin = new NameValuePair("savelogin","1");
NameValuePair product = new NameValuePair("product","t");
NameValuePair type = new NameValuePair("type","1");
NameValuePair url2 = new NameValuePair("url","http://t.163.com/session/first");
NameValuePair[] data = {username2, password2, url2 ,savelogin,product,type};
postMethod.setRequestBody(data);
httpClient.executeMethod(postMethod);
//获取目标url的源代码
// HttpClient client1 = new HttpClient();
//
for(int i=0;i<httpClient.getState().getCookies().length;i++){
System.out.println("--------"+httpClient.getState().getCookies()[i]);
}
GetMethod getMethod = new GetMethod(url);
httpClient.executeMethod(getMethod);
InputStream response = getMethod.getResponseBodyAsStream();
String result = StreamOut(response, charset);
response.close();
System.out.println(result);
return result;
}
public static String StreamOut(InputStream stream, String charset) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream,charset));
String tempbf;
StringBuffer html = new StringBuffer(100);
while ((tempbf = br.readLine()) != null) {
html.append(tempbf + "\n");
}
return html.toString();
}
public static void main(String[] args) throws IOException {
getHtmlSource("http://t.163.com/2699910068","utf-8");
}
}