日期:2014-05-20 浏览次数:20829 次
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"); }
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(); }