用Java程序模拟登陆网站平台

由于想测试性能,想模拟多个用户同时登陆系统进行访问,于是写了一个例子。

代码如下:

URL url = null;

        HttpURLConnection httpurlconnection = null;

        try {

            url = new URL("http://www.****.com");            

            httpurlconnection = (HttpURLConnection) url.openConnection();

            httpurlconnection.setRequestProperty("User-Agent", "Internet Explorer");

            httpurlconnection.setRequestProperty("Host", "www.baidu.com");

            httpurlconnection.connect();

            String cookie0 = httpurlconnection.getHeaderField("Set-Cookie");
            /**
             * write out the cookie
             */
            System.out.println(cookie0);
            System.out.println(cookie0.split(";")[0]);

            httpurlconnection.disconnect();
            //http://www.360itsm.com/desktop;
            url = new URL("http://www.****.com/desktop;"+cookie0.split(";")[0]);

            String strPost = "j_username=****&j_password=****";

            httpurlconnection = (HttpURLConnection) url.openConnection();

            HttpURLConnection.setFollowRedirects(true);

            httpurlconnection.setInstanceFollowRedirects(true);

            httpurlconnection.setDoOutput(true); // 需要向服务器写数据

            httpurlconnection.setDoInput(true); //

            httpurlconnection.setUseCaches(false); // 获得服务器最新的信息

            httpurlconnection.setAllowUserInteraction(false);

            httpurlconnection.setRequestMethod("POST");

            httpurlconnection

                    .addRequestProperty(

                            "Accept",

                            "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*");

            httpurlconnection

                    .setRequestProperty("Referer",

                            "http://www.360itsm.com");

            httpurlconnection.setRequestProperty("Accept-Language", "zh-cn");

            httpurlconnection.setRequestProperty("Content-Type",

                    "application/x-www-form-urlencoded");

            httpurlconnection.setRequestProperty("Accept-Encoding",

                    "gzip, deflate");

            httpurlconnection

                    .setRequestProperty(

                            "User-Agent",

                            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727; MEGAUPLOAD 1.0)");

            httpurlconnection.setRequestProperty("Host", "****.com");

            httpurlconnection.setRequestProperty("Content-Length", strPost

                    .length()

                    + "");

            httpurlconnection.setRequestProperty("Connection", "Keep-Alive");

            httpurlconnection.setRequestProperty("Cache-Control", "no-cache");

            httpurlconnection.setRequestProperty("Cookie", cookie0);

            httpurlconnection.connect();

            httpurlconnection.getOutputStream().write(strPost.getBytes());

            httpurlconnection.getOutputStream().flush();

            httpurlconnection.getOutputStream().close();

            int code = httpurlconnection.getResponseCode();

            System.out.println("code   " + code);



            String cookie1 = httpurlconnection.getHeaderField("Set-Cookie");

            System.out.print(cookie0 + "; " + cookie1);
            byte[] bytes = new byte[10240];
            httpurlconnection.getInputStream().read(bytes);
            System.out.println(new String(bytes,"GBK"));

            httpurlconnection.disconnect();

原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100450.html