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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用java实现登陆网站--不需打开浏览器相关的知识,希望对你有一定的参考价值。

用java application 实现网站的登录,仅需在本地的程序上输入用户名和密码,就自动向该网站发出登陆请求(暂不考虑验证码问题),要求能运行!

这里有一个百度登陆的模拟,可惜不能运行,如果改对了也可以。
http://hi.baidu.com/murphylau/blog/item/68fc5b58b732f1d69c820492.html

参考技术A public static void loginBaidu()
URL url = null;
HttpURLConnection httpurlconnection = null;
try
url = new URL("http://www.baidu.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");

System.out.println(cookie0);//打印出cookie
httpurlconnection.disconnect();
// String cookie0 =
// "BAIDUID=8AF5EA24DBF1275CE15C02B5FF65A265:FG=1;BDSTAT=61a1d3a7118ce8a7ce1b9d16fdfaaf51f3deb48f8e5494eef01f3a292cf5b899;
// BDUSE=deleted";
url = new URL("http://passport.baidu.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.baidu.com/?login&tpl=mn&u=http%3A//www.baidu.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.baidu.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.baidu.com/");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestProperty("User-Agent",
"Internet Explorer");
httpurlconnection.setRequestProperty("Host", "www.baidu.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();



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

程序可以运行了,用户名和密码改为你的帐户就可以直接运行了。可以使用,但不能一直使用啊,如果用多了百度给你个验证码,就是神仙也不行了。本回答被提问者采纳

以上是关于如何用java实现登陆网站--不需打开浏览器的主要内容,如果未能解决你的问题,请参考以下文章

如何用java或js或jQuery将打开的网页保存成一个图片

如何用手机登陆ftp服务器

如何用WordPress建立网站

如何用window.scrollTo实现打开网页跳转到DIV?

如何用 Java 实现 wordexcel 等文档在线预览?

保存壁纸太麻烦?教你如何用Java快速获取网站图片