首页 > 技术文章 > 模拟登录校内邮箱

lindaZ 2015-11-11 22:55 原文

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 
 * 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();
        }
        
    }

}

 

推荐阅读