java 模擬web登錄


參考了不少文章,終於測試成功了。

 

package http.login;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

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.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.junit.Test;

public class LoginTest2 {

    private static CookieStore cs = new BasicCookieStore();

    @Test
    public void test1() throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();

        // 創建一個本地上下文信息
        HttpContext localContext = new BasicHttpContext();
        // 在本地上下問中綁定一個本地存儲
        localContext.setAttribute(ClientContext.COOKIE_STORE, cs);

        // 目標地址
        HttpPost httppost = new HttpPost(
                "http://localhost:8080/app/login");

        System.out.println("請求: " + httppost.getRequestLine());
        // 構造最簡單的字符串數據
        StringEntity reqEntity = new StringEntity(
                "username=admin&password=admin");
        // 設置類型
        reqEntity.setContentType("application/x-www-form-urlencoded");
        // 設置請求的數據
        httppost.setEntity(reqEntity);
        // 執行
        HttpResponse response = httpclient.execute(httppost);

        Header[] headers = response.getAllHeaders();
        for (Header h : headers) {
            String name = h.getName();
            String value = h.getValue();
            System.out.println("header : " + h.getName() + ":" + h.getValue());

            if ("Set-Cookie".equalsIgnoreCase(name)) {
                String[] strs = value.split(";");
                for (String str : strs) {
                    String[] cookies = str.split("=");
                    System.out.println("=============== : " + cookies[0] + ":"
                            + cookies[1]);
                    cs.addCookie(new BasicClientCookie(cookies[0], cookies[1]));
                }
            }

        }

        HttpEntity entity = response.getEntity();
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: "
                    + entity.getContentLength());

        }

        // 顯示結果
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        opera();
    }

    private void opera() throws Exception {

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

        String logPath = http://localhost:8080/app/log;

        DefaultHttpClient httpclient = new DefaultHttpClient();

        String cookieStr = "";
        List<Cookie> list = cs.getCookies();
        for (Cookie cookie : list) {
            cookieStr += cookie.getName() + "=" + cookie.getValue() + ";";
        }

        // 目標地址
        HttpGet httpget = new HttpGet(logPath);
        httpget.setHeader("Cookie", cookieStr);

        System.out.println("請求: " + httpget.getRequestLine());
        // 設置類型
        // 執行
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

    }
}

工程配置包參考:
http://files.cnblogs.com/alex-blog/pom.xml
 

本文參考:

http://topmanopensource.iteye.com/blog/821954(還有一些沒有記錄下來)

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM