Socket发起post请求,并保持session的状态

直接上代码

private static void login() {
        try {
            // while (true) {
            String cookie = "";

            // 登录
            String path = "/onePortalC03L00103/usermanage/loginUser.action";
            String params = "";
            params += URLEncoder.encode("loginUser.pwd", "UTF-8") + "="
                    + URLEncoder.encode("Y2RzZjExOTExOQ==", "UTF-8");
            params += "&" + URLEncoder.encode("loginUser.msisdn", "UTF-8")
                    + "=" + URLEncoder.encode("119", "UTF-8");
            params += "&" + URLEncoder.encode("loginUser.authCode", "UTF-8")
                    + "=" + URLEncoder.encode("u6h5", "UTF-8");
            cookie = sendPost(path, params, cookie);

            // 查询
            path = "/onePortalC03L00103/lss/queryMMsSetting.action";
            params = "";
            params += URLEncoder.encode("timestamp", "UTF-8") + "="
                    + URLEncoder.encode("1437379696855.951", "UTF-8");
            cookie = sendPost(path, params, cookie);
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

private static String sendPost(String path, String params, String Cookie)
            throws IOException {
        Socket socket = new Socket("10.0.2.88", 8080);
        boolean autoflush = true;
        PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
        StringBuffer sb = new StringBuffer(8096);
        StringBuffer head = new StringBuffer();
        head.append("POST " + path + " HTTP/1.0\r\n");
        head.append("Content-Length: " + (params.length()) + "\r\n");
        head.append("Host: 10.0.2.88:8080\r\n");
        if (null != Cookie && !"".equals(Cookie)) {
            head.append("Cookie: " + Cookie + "; i18n=en-US\r\n");
        }
        head.append("Content-Type: application/x-www-form-urlencoded\r\n");
        System.out.println(head);
        out.write(head.toString());
        out.write("\r\n");
        out.write(params);
        out.write("\r\n");
        out.flush();
        boolean loop = true;
        String str;
        while (loop) {
            str = in.readLine();
            if (str != null) {
                if (str.contains("Set-Cookie")) {
                    Cookie = str.split(";")[0].split(":")[1].trim();
                }
                sb.append(str + "\r\n");
            } else {
                loop = false;

            }
        }

        System.out.println(sb.toString());
        return Cookie;
    }

至于为什么这么发请爬其他的贴,这里就不再详细描述了.
那么在看看返回的结果

POST /onePortalC03L00103/usermanage/loginUser.action HTTP/1.0
Content-Length: 79
Host: 10.0.2.88:8080
Content-Type: application/x-www-form-urlencoded

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=A29316FD8CC81BCBCB4FC1BD87B76546; Path=/onePortalC03L00103/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Content-Length: 696
Date: Thu, 30 Jul 2015 01:42:57 GMT
Connection: close

POST /onePortalC03L00103/lss/queryMMsSetting.action HTTP/1.0
Content-Length: 27
Host: 10.0.2.88:8080
Cookie: JSESSIONID=A29316FD8CC81BCBCB4FC1BD87B76546; i18n=en-US
Content-Type: application/x-www-form-urlencoded

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Content-Length: 219
Date: Thu, 30 Jul 2015 01:42:57 GMT
Connection: close

戴着钛合金眼镜的同学可能已经发现了,在第一次发送请求(登录)后返回的响应头中包含一个”Set-Cookie”的键值对和一个”Connection: close”.
Connection: close“:表示当前连接已经断开了,已经不能再用这个这个socket建立的通道向服务端发送消息(脾气倔的同学可以自己再试一试),这是为了降低服务端的压力,具体的要涉及到tcpip协议的握手心跳保持等等,请好奇的同学向度娘撒娇.
Set-Cookie“:这个才是本篇的重点,这个值是由服务端生成,然后发送请求方(如浏览器等),简单的来说这就是一个鸡毛令箭,假如第一个socket的名字叫”李某”位高权重,然后到天上人间开了个房间拿了一把钥匙,”某刚”潇洒完后就把钥匙给”赵某”,”赵某”一样可以进去潇洒,,完全不会管是谁在里面乱来.
达到的效果:
1.可以对自己的web服务器进行压力测试
2.假如有机会获取别人的浏览记录,并且在服务器没有判断session超时,而你又恰好能获取到访问的cookie,,那么就可以对别人的服务器做一些羞羞的事情了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值