我想使用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");
这是我发送给服务器的密钥代码。 我该怎么办才能成功模拟登录?我还应该添加什么代码?
答案 0 :(得分:1)
我认为您应该阅读CookieHandler
在java http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/
中有一个模拟登录解决方案的例子答案 1 :(得分:0)
我没有获得cookie的原因是我发布的网站将重定向链接,我没有设置setInstanceFollowRedirects(false)。我的回答错了。所以我没有得到cookie