如何使用 java 来模拟登录



我想使用httpurlconnection类来模拟登录网站。我尝试捕获数据包以获取请求标头,我将其全部放入我的程序中。但是此代码无法从服务器获取cookie。

    String username = "。。。。。。;
    String password = ".。。。。";
    String param = "username="+username+"&pwd="+password+"&usertype=xuesheng&Submit=+%B5%C7%C2%BD+";
    HttpURLConnection connection = (HttpURLConnection)urlx.openConnection();
    connection.setRequestProperty("Host","jwc.ecjtu.jx.cn:8080");
    connection.setRequestProperty("Connection","keep-alive");
    connection.setRequestProperty("Content-Length","74");
    connection.setRequestProperty("Cache-Control","max-age=0");
    connection.setRequestProperty("Content-Length","http://jwc.ecjtu.jx.cn:8080");
    connection.setRequestProperty("Upgrade-Insecure-Requests","1");
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36");
    connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    connection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    connection.setRequestProperty("DNT","1");
    connection.setRequestProperty("Referer","http://jwc.ecjtu.jx.cn:8080/jwcmis/assess/");
    connection.setRequestProperty("Accept-Encoding","gzip, deflate");
    connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST"); 

这是我发送到服务器的密钥代码。我应该怎么做才能成功模拟登录?我还应该添加哪些代码?

我认为你应该阅读有关CookieHandler的信息

有一个

在java中模拟登录的解决方案示例 http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/

我没有收到cookie的原因是我发布的网站会重定向链接,我没有设置setInstanceFollowRedirects(false)。我得到错误的回答。所以我没有得到饼干

最新更新