登录微博的样例

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");






}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值