导航:首页 > 编程语言 > java自动登录网站

java自动登录网站

发布时间:2024-11-10 20:04:35

A. 如何用java实现登陆网站--不需打开浏览器

public static void loginBai() {
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..com");
httpurlconnection.connect();

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

System.out.println(cookie0);//打印出cookie
httpurlconnection.disconnect();
// String cookie0 =
// "BAIDUID=:FG=1;BDSTAT=;
// BDUSE=deleted";
url = new URL("http://passport..com/?login");
String strPost = "username=xxxxxx&password=yyyyyyy&mem_pass=on";
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://passport..com/?login&tpl=mn&u=http%3A//www..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", "passport..com");
httpurlconnection.setRequestProperty("Content-Length", strPost
.length()
+ "");
httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
httpurlconnection.setRequestProperty("Cookie", cookie0);
httpurlconnection.getOutputStream().write(strPost.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
httpurlconnection.connect();
int code = httpurlconnection.getResponseCode();
System.out.println("code " + code);
String cookie1 = httpurlconnection.getHeaderField("Set-Cookie");
System.out.print(cookie0 + "; " + cookie1);
httpurlconnection.disconnect();
url = new URL("http://www..com/");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestProperty("User-Agent",
"Internet Explorer");
httpurlconnection.setRequestProperty("Host", "www..com");
httpurlconnection.setRequestProperty("Cookie", cookie0 + "; "
+ cookie1);
httpurlconnection.connect();
InputStream urlStream = httpurlconnection.getInputStream();
BufferedInputStream buff = new BufferedInputStream(urlStream);
Reader r = new InputStreamReader(buff, "gbk");
BufferedReader br = new BufferedReader(r);
StringBuffer strHtml = new StringBuffer("");
String strLine = null;
while ((strLine = br.readLine()) != null) {
strHtml.append(strLine + "\r\n");
}
System.out.print(strHtml.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
}

呵呵,改了下程序中错误的地方。学习了,原来这样也可以。
以前只知道可以这样做,还从来没有花过心思去做过。
阅读了些代码,真是获益匪浅啊。

程序可以运行了,用户名和密码改为你的帐户就可以直接运行了。可以使用,但不能一直使用啊,如果用多了网络给你个验证码,就是神仙也不行了。

B. java程序实现登陆。用户没有登录但访问非登录的页面,使用过滤器跳转到登录页,登录后怎么回到访问页

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
if(req.getSession().getAttribute("user")==null){
req.setAttribute("error", "请你先登录");
//非法请求才会进到这里面,在这里保存请求的url地址,在成功登录后再进行跳转
req.getSession().setAttribute("goUrl", req.getRequestURL()+"?"+ req.getQueryString());
request.getRequestDispatcher("/ulogin.jsp").forward(request, response);
}

else{
chain.doFilter(request, response);
}
}
上面的代码是过滤器中的代码
下面的是servlet中的代码
if(request.getSession().getAttribute("goUrl")!=null){
String url = (String)request.getSession().getAttribute("goUrl");
response.sendRedirect(url);
}
else{
response.sendRedirect("/webshopping/index.jsp");
}

C. javaweb如何实现用户在一个页面长时间不操作自动跳转到登录页面

设置Session超时时间,超时时长内,如果用户没有任何操作,Session就会自动过期.你在代码里先取Session,如果没有取到,就跳转到登录页面即可.

D. java实现注册成功自动登录

注册页面-->验证注册成功后跳到action,把存储数据库的那条数据取出来,放到session里,就已经成功登陆了,哪个页面需要用到用户信息,直接从session里取值

阅读全文

与java自动登录网站相关的资料

热点内容
js组合快捷键 浏览:174
linux系统盘默认挂在的文件夹 浏览:667
淘宝数据包如何操作上架 浏览:567
vb编程中输入cls是什么意思 浏览:81
linuxtime服务 浏览:184
疯狂安卓讲义第二版代码 浏览:420
老炮儿三小时版本下载 浏览:313
matlab怎么调试程序 浏览:2
winxp升级win7的危害 浏览:496
网络没连上却不可用是怎么回事 浏览:752
社区版本 浏览:738
怎么查微信公众号什么时候开通的 浏览:717
安装三菱编程闪退怎么回事 浏览:488
手机怎么创建word文件格式 浏览:694
c语言连接数据库 浏览:887
数据线粉色和白色哪个是正 浏览:775
vb编程应注意什么 浏览:855
js循环添加控件 浏览:615
学习计算机网络的作用 浏览:235
access数据库最新内容怎么调 浏览:203

友情链接