关于“使用java模拟登陆百度”遇到的问题。

tuchangle 2009-12-06 09:35:20
在网上看到一个篇“使用java模拟登陆百度”试了一下,打开IE进入百度后还是没有登陆的状态?请问如何实现程序运行后,打开IE进入百度就已经登陆?
网址为:http://hi.baidu.com/murphylau/blog/item/68fc5b58b732f1d69c820492.html
代码为:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mylogin;

/**
*
* @author TralaTu
*/
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class LoginBaidu {

/**
* @param args the command line arguments
*/
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");
httpurlconnection.disconnect();
//String cookie0 = "BAIDUID=8AF5EA24DBF1275CE15C02B5FF65A265:FG=1;BDSTAT = 61a1d3a7118ce8a7ce1b9d16fdfaaf51f3deb48f8e5494eef01f3a292cf5b899;BDUSE = deleted";
url = new URL("http://passport.baidu.com/?login");
String strPost = "username=tuchangle&password=19870221&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 + "rn");
}
System.out.print(strHtml.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null) {
}
httpurlconnection.disconnect();
}
}

public static void main(String[] args) {
LoginBaidu.LoginBaidu();
}
}

...全文
571 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
whut0802 2009-12-07
  • 打赏
  • 举报
回复
帮顶,学习
苍蝇①号 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tuchangle 的回复:]
找的原创作者了:http://blog.csdn.net/dongle2001/archive/2008/06/17/2557850.aspx
Java codepublicstaticvoid 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");

httpurlconnection.disconnect();

url=new URL("http://passport.baidu.com/?login");

String strPost="username=xxxxxx&password=xxxxxx&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();

}

}

不知道这里cookie的作用是什么?谁知道吗?
[/Quote]
做过爬虫的人就知道了,这是为了抓取需要登陆后才能显示的页面是用的
  • 打赏
  • 举报
回复
tuchangle 加油
magicelfzz 2009-12-07
  • 打赏
  • 举报
回复
菜鸟学习~~~~~~~~~~~
tuchangle 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 cangyingzhijia 的回复:]
引用 5 楼 tuchangle 的回复:
找的原创作者了:http://blog.csdn.net/dongle2001/archive/2008/06/17/2557850.aspx
Java codepublicstaticvoid 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");

            httpurlconnection.disconnect();

            url=new URL("http://passport.baidu.com/?login");

            String strPost="username=xxxxxx&password=xxxxxx&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();

        }

    }

不知道这里cookie的作用是什么?谁知道吗?

做过爬虫的人就知道了,这是为了抓取需要登陆后才能显示的页面是用的
[/Quote]

谢谢,了解了。最近在学习HttpClien相关的知识。有兴趣的朋友可以加我一起学习。
QQ:273628
tuchangle 2009-12-06
  • 打赏
  • 举报
回复
找的原创作者了:http://blog.csdn.net/dongle2001/archive/2008/06/17/2557850.aspx

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");

httpurlconnection.disconnect();

url = new URL("http://passport.baidu.com/?login");

String strPost = "username=xxxxxx&password=xxxxxx&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();

}

}


不知道这里cookie的作用是什么?谁知道吗?
tuchangle 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shine333 的回复:]
假设Baidu网站就是网游,你贴的程序就是内挂,你用IE上去就是正常登录游戏。你现在的问题就是,我刚才用外挂登录上去了,怎么不开外挂进游戏,还要登录啊?
[/Quote]

谢谢,很受用。程序里用到cookie,我以为就可以记录登陆状态,打开IE时就不用登陆了。
shine333 2009-12-06
  • 打赏
  • 举报
回复
假设Baidu网站就是网游,你贴的程序就是内挂,你用IE上去就是正常登录游戏。你现在的问题就是,我刚才用外挂登录上去了,怎么不开外挂进游戏,还要登录啊?
tuchangle 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shine333 的回复:]
...
LZ,看样子您根本不知道那个帖子是干嘛的
[/Quote]

确实,还请多指教。
shine333 2009-12-06
  • 打赏
  • 举报
回复
...
LZ,看样子您根本不知道那个帖子是干嘛的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧