HttpClient模拟登录手机版微博

昨天查询了关于使用httpclient模拟登录网站的信息,今天的附上代码

再有一个就是,split方法返回的是数组,将已知的html文件通过参数分为两部分,分别为下标[0]和[1],将已知的html经过两次split就可以截取到想要的消息.

package loginweibo;

import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import sun.rmi.runtime.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pl on 15-7-19.
 */
public class weibo {
    public static void main(String[] args) throws NullPointerException{

        //登录页面的URL
        String Loginurl ="http://login.weibo.cn/login/";
        String firstpage = "http://weibo.cn/?vt=4";    //微博首页的网址
        String loginnum = "18829289882";
        String loginpwd = "pl19710504";

        CloseableHttpClient httpclient = HttpClients.createDefault();   //建立客户端的链接
        HttpGet httpget = new HttpGet(Loginurl);

        try{
            CloseableHttpResponse response = httpclient.execute(httpget);

            String responhtml = null;
            try {
                responhtml = EntityUtils.toString(response.getEntity());
                //System.out.println(responhtml);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

                 //将vk的值截出来,split将整个的html分为两个部分,截取下面的部分
                String vk = responhtml.split("<input type=\"hidden\" name=\"vk\" value=\"")[1].split("\" /><input")[0];
                System.out.println(vk);

                String pass = vk.split("_")[0];
                String finalpass = "password_"+pass;
                System.out.println(finalpass);
                response.close();

                List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
                pairs.add(new BasicNameValuePair("mobile",loginnum));
                pairs.add(new BasicNameValuePair(finalpass,"pl19710504"));
                pairs.add(new BasicNameValuePair("remember","on"));
                pairs.add(new BasicNameValuePair("vk",vk));

                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, Consts.UTF_8);   //返回的实体
                HttpPost httppost = new HttpPost(Loginurl);
                httppost.setEntity(entity);
                //检测以下响应状态是否正确
                CloseableHttpResponse  response2 = httpclient.execute(httppost);
                System.out.println(response2.getStatusLine().toString());
                httpclient.execute(httppost);  //登录操作
                System.out.println("success");

                HttpGet getinfo= new HttpGet("http://m.weibo.cn/p/100803?vt=4");
                CloseableHttpResponse res;
                res = httpclient.execute(getinfo);
                System.out.println("进入热门话题的页面:");
                //输出该页面的html文件
                System.out.println(EntityUtils.toString(res.getEntity()));
                res.close();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值