`

使用httpclient4登录百度

阅读更多

使用httpclient4.0.1登录百度,具体步骤如下:

  1. 访问百度首页获取首页HMTL内容
  2. 使用百度帐号登陆百度
  3. 重复第一步,这时可以比较两次获取的HTML的差别

如果感兴趣想测试的同学,可以去网站上下载需要的Jar包,然后执行一下看看!!

下面附上具体测试的代码:


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.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * 使用httpclient4登陆百度
 * @author gohands
 *
 */
public class BaiduLogin {
   
    private static String URL_CHARACTER = "gb2312"; // 统一字符集
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //初始化
        DefaultHttpClient httpclient = new DefaultHttpClient();
        // 这一行必须要加,否则服务器无法获取登陆状态
        HttpClientParams.setCookiePolicy(httpclient.getParams(),CookiePolicy.BROWSER_COMPATIBILITY);

        // 第一次访问
        String url = "http://www.baidu.com";
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        System.out.println("Length1::" + response.getEntity().getContentLength());
        HttpEntity entity = response.getEntity();
        BaiduLogin.printEntity(entity);

        // 登陆【使用POST方式登录】

        // 如果要直接执行,麻烦去申请个百度的帐号

        // 不好意思,给百度做广告了
        HttpPost httpost = new HttpPost("http://passport.baidu.com/?login");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "gohands"));
        nvps.add(new BasicNameValuePair("password", "*******"));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, BaiduLogin.URL_CHARACTER));
        response = httpclient.execute(httpost);

        // 第二次访问
        System.out.println("\n----------------------------------------");
        System.out.println(response.getStatusLine());
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        entity = response.getEntity();
        BaiduLogin.printEntity(entity);

        System.out.println("\n----------------------------------------");

        cookies = httpclient.getCookieStore().getCookies();

        System.out.println("cookies" + cookies.size());
        httpget = new HttpGet(url);
        // httpget.setr
        // httpget.setHeader(name, value)
        response = httpclient.execute(httpget);
        System.out.println("Length2::"
                + response.getEntity().getContentLength());
        entity = response.getEntity();
        BaiduLogin.printEntity(entity);
    }

    /**
     * 输出entity内容,获取和输出返回的HTML文
     * @param entity
     * @throws IllegalStateException
     * @throws IOException
     */
    private static void printEntity(HttpEntity entity)
            throws IllegalStateException, IOException {

        if (entity == null) {
            return;
        }
        System.out.println("HttpEntity start >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println("Response content length: " + entity.getContentLength());
        InputStream is = entity.getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        List<Byte> li = new ArrayList();
        int i;

        //之所以写的如此复杂是因为了解决中文问题
        while ((i = is.read()) != -1) {
            li.add((byte) i);
        }
        byte a[] = new byte[li.size()];
        for (i = 0; i < a.length; i++) {
            a[i] = (byte) li.get(i);
        }
        System.out.println(new String(a, BaiduLogin.URL_CHARACTER)); // 打印HTML内容
        entity.consumeContent(); // entity销毁
        System.out.println("HttpEntity END >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }
}

如果有疑问或好的想法随时可以跟我沟通,谢谢阅读!

依赖的Jar包

apache-mime4j-0.6.jar
commons-codec-1.3.jar
commons-logging-1.1.1.jar
httpclient-4.0.1.jar
httpcore-4.0.1.jar
httpmime-4.0.1.jar

 

-----------------------------------------------------------

2015/02/02:

好几年前写的代码了,

现在前端渲染开发比较多,画面上全是JS封装的,

这段代码已经不能正常使用了。

 

 

4
0
分享到:
评论
8 楼 holleyangyanges 2014-05-06  
name327 写道
LZ说句打击你的话, 首先不说Https的事情, 先说说你提交的参数, 最重要的token你这都没有。你自己我估计都没登陆成功过。


应该怎么写,你有没有写过的例子
7 楼 holleyangyanges 2014-05-06  
,没有登陆成功啊!
6 楼 name327 2012-06-13  
LZ说句打击你的话, 首先不说Https的事情, 先说说你提交的参数, 最重要的token你这都没有。你自己我估计都没登陆成功过。
5 楼 gohands 2011-11-17  
java1997 写道
博主,百度现在登录网址跳转到了https://passport.baidu.com/?login,你的代码用不了了,有没有新的可用代码可以共享下,谢谢。

这是当时学习用的,下次有时间试试!
可以使用浏览器禁用JScript看看HTML文。
4 楼 java1997 2011-11-10  
博主,百度现在登录网址跳转到了https://passport.baidu.com/?login,你的代码用不了了,有没有新的可用代码可以共享下,谢谢。
3 楼 tomfish88 2011-07-29  
再有个问题请教

我用的4.1.1 返回 HTTP/1.1 302 的时候页面打印的内容是转向内容,
<html><body>You are being <a href="http://tomfish88.iteye.com/login">redirected</a>.</body></html>

然后如何进一步到登陆后的页面呢,然后我继续用get打印内容里面有
<a href="/login" class="welcome" title="登录">您还未登录 !</a>
    <a href="http://www.iteye.com/all" style="color:red">我的应用</a>
    <a href="/login">登录</a>
    <a href="/signup" class="nobg">注册</a>

我知道我没有登陆成功

HttpResponse rsp= httpclient.execute(httpget,localContext);

我又这样了一次 ,有什么问题么?是因为没带cookie么?


ps,你这里的回复我那里没反映,方便到我的空间回复一下么,非常感谢http://tomfish88.iteye.com
2 楼 gohands 2011-07-21  
tomfish88 写道
为啥要先get,后post呢,不能直接post么? 请解答?

虽然没有测试,但是我想应该可以,其实前后并没有直接关联。
只是为了描述完成过程而已。
1 楼 tomfish88 2011-06-28  
为啥要先get,后post呢,不能直接post么? 请解答?

相关推荐

Global site tag (gtag.js) - Google Analytics