Java模拟WEB登陆

 package com.jbit.test;
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;
public class LoginTest2 {
    private static CookieStore cs = new BasicCookieStore();
    public static void main(String[] args) {
     try {
   test1();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
    public static 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/ItBlog/servlet/Login");
        System.out.println("请求: " + httppost.getRequestLine());
        // 构造最简单的字符串数据
        StringEntity reqEntity = new StringEntity("userName=pengliu&userPwd=asd113399");
        // 设置类型
        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 static void opera() throws Exception {
        System.out.println("----------------------------------------");
        System.out.println("----------------------------------------");
        String logPath = "http://localhost:8080/testLogin/";
        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);
        }
    }
}

转载于:https://my.oschina.net/xlyjx/blog/511219

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值