标签:
要模拟登录的post地址(url),可以先登录一边要登录的网站,用wireshark抓取信息,filter过滤条件为http.request.method==POST。
也可以在Chrome浏览器打开,按f12,点击Network,进行查看。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /* * Copyright (c) 2015 * 广州米所思信息科技有限公司(Guangzhou Misuosi Information technology co., LTD) * All rights reserved. */ /** * Description : * <p/> * <br><br>Time : 2015-11-9 下午8:02:01 * * @author ZXL * @version 1.0 * @since 1.0 */ public class ShoolEmailLogin { //portaluser.jsp static String surl = "http://www.gduf.edu.cn/portaluser.jsp"; static String responseCookie ; static String username = "账号"; static String password = "密码"; public static void main(String[] args) { // TODO Auto-generated method stub StringBuilder sb = new StringBuilder(); try { URL url = new URL(surl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("contentType", "GB2312"); sb.append("username="+username); sb.append("&password="+password); conn.setRequestProperty("Content-Length", String.valueOf(sb.toString().length())); OutputStream os = conn.getOutputStream(); os.write(sb.toString().getBytes("GB2312")); os.close();
//获取返回的信息 BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream(),"GB2312")); responseCookie = conn.getHeaderField("Set-Cookie"); System.out.println("Cookie="+responseCookie); String line = null; while ((line=bf.readLine())!=null){ //System.out.println(line); } //读取登录后的页面内容 URL newurl = new URL("http://www.gduf.edu.cn/mail/mail_list.jsp?foldertype=1"); HttpURLConnection httpURLConn = (HttpURLConnection) newurl.openConnection(); httpURLConn.setRequestProperty("Cookie",responseCookie); BufferedReader newBR = new BufferedReader(new InputStreamReader(httpURLConn.getInputStream(),"GB2312")); String newLine = null; while ((newLine = newBR.readLine())!=null){ System.out.println(newLine); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/lindaZ/p/4951357.html