如果登录成功的话,我访问百度首页应该在右上角出现用户面板
- Java code
private void login() { String surl = "https://passport.baidu.com/?login&tpl=mn"; // String surl = "http://localhost:8080/jkl.html"; URL url = null; try { url = new URL(surl); } catch (MalformedURLException e) { e.printStackTrace(); } HttpURLConnection connection = null; try { connection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { e.printStackTrace(); } connection.setDoOutput(true); try { connection.setRequestProperty("Content-type", "text/html"); connection.setRequestMethod("POST"); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } catch (ProtocolException e1) { e1.printStackTrace(); } OutputStreamWriter out = null; try { out = new OutputStreamWriter(connection.getOutputStream(), "gb2312"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { String params = "tpl_ok=&" + "next_target=&" + "tpl=mn&" + "skip_ok=&" + "aid=&" + "need_pay=&" + "need_coin=&" + "pay_method=&" + "u=http://www.baidu.com/&" + "return_method=get&" + "more_param=&" + "return_type=&" + "psp_tt=0&" + "safeflg=0&" + "isphone=tpl&" + "username=xxx&" + "password=xxx&" + "verifycode=&" + "mem_pass="; out.write(params); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } cookies = connection.getHeaderField("Set-Cookie"); }
- Java code
private void view(String surl) throws Exception { URL url = new URL(surl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (!"".equals(cookies)) { conn.setRequestProperty("Cookie", cookies); } conn.connect(); InputStreamReader in = new InputStreamReader(conn.getInputStream(), "gb2312"); int i = 0; while ((i = in.read()) != -1) { System.out.print((char) i); } System.out.println(); }
上面代码都是网上找来后修改的,在其他网站是可以登录的,是不是百度用了https的原因呢?
------解决方案--------------------
每个大型网站都有自己的防护 想要用一段代码登陆所有的网站 不太现实
------解决方案--------------------
设置个验证码怎么办。