java模拟登录百度账号,施用java模拟登陆百度

使用java模拟登陆百度

public static void login百度()

{

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

httpurlconnection.disconnect();

//String cookie0 = “百度ID=8AF5EA24DBF1275CE15C02B5FF65A265:FG=1;

BDSTAT=61a1d3a7118ce8a7ce1b9d16fdfaaf51f3deb48f8e5494eef01f3a292cf5b899; BDUSE=deleted”;

url = new URL(http://passport.百度.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.百度.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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值