利用HttpClient自动登陆ITEYE

          我利用的是4.5.2的jar包,可以在官网上下载,下载地址为http://hc.apache.org/downloads.cgi,我这里主要实现了登陆的主页的代码,如果需要登陆到博客之类的就自己更换连接的地址。我这里使用的是SpringBoot的进行测试的。

   SpringBoot的实现代码为

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>testImport</groupId>
    <artifactId>testImport</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

 Java的代码部分为

package com;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
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.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * Created by Administrator on 2016/5/11.
 */
@RestController
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class TestImport {

    @RequestMapping
    public String getHello(){
        return "hello";
    }

    @RequestMapping(value = "/testLogin")
    public String testLogin(){
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String html = "";
        try {
            //可以使用谷歌浏览器的F12查看登陆的时候需要那些信息
            //首先到登陆的界面进行登陆
            HttpPost httpPost = new HttpPost("http://www.iteye.com/login");
            httpPost.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            httpPost.addHeader("Accept-Encoding","gzip, deflate, sdch");
            httpPost.addHeader("Accept-Language","h-CN,zh;q=0.8");
            httpPost.addHeader("Host","www.iteye.com");
            httpPost.addHeader("Proxy-Connection","keep-alive");
            httpPost.addHeader("Upgrade-Insecure-Requests","1");
            httpPost.addHeader("Referer","http://www.iteye.com/login");
            //使用的是谷歌浏览器登陆
            httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36");
            httpPost.addHeader("Cookie","_javaeye_cookie_id_=1466393515330269; __utmt=1; _javaeye3_session_=BAh7CDoQX2NzcmZfdG9rZW4iMVo1SlVaOEhrTTdia0w3Tm40MDJqY3BtbHhVd25lU1lPTDI5MGZVdDFLMVk9OhFvcmlnaW5hbF91cmkiGmh0dHA6Ly93d3cuaXRleWUuY29tLzoPc2Vzc2lvbl9pZCIlMjU0YzJiMDgwYjRiZjg4YzI0YmJlNmFhYWFhYzdmNjA%3D--6e802374f064e10c59fccc0af2965c29472e77e9; __utma=191637234.388365526.1466393674.1466393674.1466393674.1; __utmb=191637234.3.10.1466393674; __utmc=191637234; __utmz=191637234.1466393674.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); dc_tos=o91wpp; dc_session_id=1466393821804");
            httpPost.addHeader("Cache-Control","max-age=0");
            //httpPost.addHeader("Content-Length","143");
            httpPost.addHeader("Origin","http://www.iteye.com");
            httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
            List formParams = new ArrayList();
            formParams.add(new BasicNameValuePair("name","*****"));
            formParams.add(new BasicNameValuePair("password","*******"));
            formParams.add(new BasicNameValuePair("authenticity_token","Z5JUZ8HkM7bkL7Nn402jcpmlxUwneSYOL290fUt1K1Y="));
            formParams.add(new BasicNameValuePair("button","登 录"));
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formParams,"UTF-8");
            httpPost.setEntity(urlEncodedFormEntity);
            CookieStore cookieStore = new BasicCookieStore();
            ResponseHandler responseHandler = new BasicResponseHandler();
            //String res = client.execute(httpPost,responseHandler,httpClientContext).toString();
            response = client.execute(httpPost);
            //进行缓存
            if(response != null){
                for(Header header :response.getAllHeaders()){
                    //if("Set-Cookie".equals(header.getName())){
                    Cookie cookie = new BasicClientCookie(header.getName(),header.getValue());
                    cookieStore.addCookie(cookie);
                    //}
                }
            }
            HttpClientContext httpClientContext = HttpClientContext.create();
            httpClientContext.setCookieStore(cookieStore);
            // 设置需要登陆的到那个页面
            String loginPage = "http://www.iteye.com";
            HttpGet httpget = new HttpGet(loginPage);
            if(response != null){
                //缓存
                for(Header header :response.getAllHeaders()){
                    if("Set-Cookie".equals(header.getName())){
                        httpget.setHeader(header);
                    }
                }
            }
            HttpResponse httpResponse = client.execute(httpget);
            // 必须是同一个HttpClient
            HttpEntity entity = httpResponse.getEntity();
            html = EntityUtils.toString(entity, "GBK");
            System.out.println(html);
            httpget.releaseConnection();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                response.close();
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return html;
    }



   public static void main(String [] args){
       SpringApplication.run(TestImport.class);
   }
}

猜你喜欢

转载自357029540.iteye.com/blog/2306338
今日推荐