java实现登陆WEB站点,并提交数据

TestLoginWeb.java
---------------------------------------------------------------------------

package com.yitai.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestLoginWeb {

    public static void main(String args[]) throws IOException {

        URL url = new URL("http://localhost:8080/backgroundH/login.jsp");
        URL url1 = new URL("http://localhost:8080/backgroundH/execute.jsp");
        HttpURLConnection huc = (HttpURLConnection) url.openConnection();

        // 设置允许output
        huc.setDoOutput(true);
        // 设置为post方式
        huc.setRequestMethod("POST");
        huc.setRequestProperty("user-agent", "mozilla/4.7 [en] (win98; i)");

        OutputStream os = huc.getOutputStream();
        os.write("name=gaolei".getBytes("gbk"));
        os.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(huc
                .getInputStream()));
        huc.connect();
        String line = br.readLine();
        while (line != null) {
            System.out.println(line);
            line = br.readLine();
        }
       
        String cookieval = huc.getHeaderField("set-cookie");
        System.out.println(cookieval);
        String sessionid = null;
        if (cookieval != null) {
            sessionid = cookieval.substring(0, cookieval.indexOf(";"));
        }

        huc.disconnect();
        huc = null;
       
       
       
        HttpURLConnection huc1 = (HttpURLConnection) url1.openConnection();

        huc1.setRequestProperty("cookie", sessionid);
        // 设置允许output
        huc1.setDoOutput(true);
        // 设置为post方式
        huc1.setRequestMethod("POST");
        huc1.setRequestProperty("user-agent", "mozilla/4.7 [en] (win98; i)");

        OutputStream os1 = huc1.getOutputStream();
        os1.write("value=1234567890".getBytes("gbk"));
        os1.close();
       
        BufferedReader br1 = new BufferedReader(new InputStreamReader(huc1
                .getInputStream()));
        huc1.connect();
        line = br1.readLine();
        while (line != null) {
            System.out.println(line);
            line = br1.readLine();
        }
        huc1.disconnect();

    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值