Form login via Proxy

通常許多web資源是面向登錄用戶的,如果我們想利用程序自動獲取,則必須要通過程序登錄之後才能實現。java本身提供的標準庫也可以實現客戶端的登 錄,但是卻不能自動管理服務器返回的cookie,必須手動管理。這個是十分麻煩和繁瑣的。幸好有APACHE 的 HTTPCLIENT,我們不必在擔心如何管理cookie。所以用hc登錄,獲取資源都是十分簡單和方便的。在這裡用一段源碼記錄如何使用hc等錄一個 website,以及如何通過代理登錄和通過代理訪問頁面。

/*
 * @(#)WebVistor.java 
 * 
 *  Created on: 2010-7-25
 
*/
package zhujia.web;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * 
@author Zhujia(Mr.Zhujia@gmail.com | TopZhujia@163.com)
 * 
 
*/
public class WebVistor {
    
/**
     * Logger for this class
     
*/
    
private static final Logger logger = Logger.getLogger(WebVistor.class);
    
static {
        logger.addAppender(
new ConsoleAppender(new PatternLayout("%m%n")));
    }

    
private static void nativeGUI() {
        
if (logger.isDebugEnabled()) {
            logger.debug(
"nativeGUI() - start"); //$NON-NLS-1$
        }

        String osname 
= System.getProperty("os.name").toLowerCase();

        
try {
            
if (osname.contains("linux")) {
                UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.gtk."
                        
+ "GTKLookAndFeel");

            } 
else if (osname.contains("windows")) {
                UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows."
                        
+ "WindowsLookAndFeel");
            }
        } 
catch (ClassNotFoundException e) {
            logger.error(
"nativeGUI()", e);
        } 
catch (InstantiationException e) {
            logger.error(
"nativeGUI()", e);
        } 
catch (IllegalAccessException e) {
            logger.error(
"nativeGUI()", e);
        } 
catch (UnsupportedLookAndFeelException e) {
            logger.error(
"nativeGUI()", e);
        }

        
if (logger.isDebugEnabled()) {
            logger.debug(
"nativeGUI() - end"); //$NON-NLS-1$
        }
    }
    
private HttpClient httpclient;
    
private List<NameValuePair> nvps;
    
private String password;
    
private HttpResponse response = null;
    
private String username;

    
public WebVistor() {
        nativeGUI();
        
// httpclient = new DefaultHttpClient();
        nvps = new ArrayList<NameValuePair>();

        username 
= JOptionPane.showInputDialog(null"enter your 163 email",
                
"username", JOptionPane.QUESTION_MESSAGE);
        password 
= JOptionPane.showInputDialog(null"enter your password",
                
"password", JOptionPane.QUESTION_MESSAGE);

        
// send a request via proxy
        HttpHost proxy = new HttpHost("edu6.zzzcn.info"2012"http");
        
// general setup
        SchemeRegistry supportedSchemes = new SchemeRegistry();
        supportedSchemes.register(
new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 
2012));

        
// prepare parameters
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_0);
        HttpProtocolParams.setContentCharset(params, 
"UTF-8");
        HttpProtocolParams.setUseExpectContinue(params, 
true);

        ClientConnectionManager ccm 
= new ThreadSafeClientConnManager(params,
                supportedSchemes);
        httpclient 
= new DefaultHttpClient(ccm, params);
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                proxy);
    }

    
/**
     * 
     
*/
    
public void login() {
        
if (logger.isDebugEnabled()) {
            logger.debug(
"login() - start");
        }

        nvps.removeAll(nvps);
        nvps.add(
new BasicNameValuePair("username", username));
        nvps.add(
new BasicNameValuePair("password", password));
        post(
"http://reg.163.com/logins.jsp", nvps);

        
// nvps.removeAll(nvps);
        
// if(username.contains("@163.com")){
        
// nvps.add(new BasicNameValuePair("username", username.split("@")[0]));
        
// nvps.add(new BasicNameValuePair("url", "http://reg.163.com/"
        
// + "Main.jsp?username=" + username.split("@")[0]));
        
// }else{
        
// nvps.add(new BasicNameValuePair("username", username));
        
// nvps.add(new BasicNameValuePair("url", "http://reg.163.com/"
        
// + "Main.jsp?username=" + username));
        
// }
        
// post("http://reg.youdao.com/crossdomain.jsp", nvps);

        
if (username.contains("@163.com")) {
            post(
"http://reg.163.com/" + "Main.jsp?username="
                    
+ username.split("@")[0], null);
        } 
else {
            post(
"http://reg.163.com/" + "Main.jsp?username=" + username, null);
        }
        logger.debug(
"login() - end");
    }

    
public void post(String url, List<NameValuePair> nvps) {
        
if (logger.isDebugEnabled()) {
            logger.debug(
"post(String, List<NameValuePair>) - start"); //$NON-NLS-1$
        }

        HttpPost post 
= new HttpPost(url);
        BufferedReader br 
= null;
        OutputStreamWriter fout 
= null;
        
try {
            
if (null != nvps) {
                post.setEntity(
new UrlEncodedFormEntity(nvps, "utf-8"));
            }
            response 
= httpclient.execute(post);
            logger.info(
"post(String, List<NameValuePair>) - status: "
                    
+ response.getStatusLine());

            br 
= new BufferedReader(new InputStreamReader(response.getEntity()
                    .getContent(), EntityUtils.getContentCharSet(response
                    .getEntity())));
            String filename 
= System.currentTimeMillis() + ".html";
            fout 
= new OutputStreamWriter(
                    
new FileOutputStream(filename),
                    EntityUtils.getContentCharSet(response.getEntity()));
            logger.debug(
"write content to file " + filename);
            
char[] buf = new char[1024];
            
int length = -1;
            
while (-1 != (length = br.read(buf))) {
                fout.write(buf, 
0, length);
            }
            logger.debug(
"writen over");

        } 
catch (ClientProtocolException e) {
            logger.error(
"post(String, List<NameValuePair>)", e);
        } 
catch (IOException e) {
            logger.error(
"post(String, List<NameValuePair>)", e);
        } 
finally {
            
try {
                
if (null != fout) {
                    fout.flush();
                    fout.close();
                }
                
if (null != br) {
                    br.close();
                }
                response.getEntity().consumeContent();
            } 
catch (IOException e) {
                logger.error(
"post(String, List<NameValuePair>)", e); //$NON-NLS-1$
            }
        }

        
if (logger.isDebugEnabled()) {
            logger.debug(
"post(String, List<NameValuePair>) - end"); //$NON-NLS-1$
        }
    }

    
/**
     * 
@param password
     *            the password to set
     
*/
    
public void setPassword(String password) {
        
this.password = password;
    }

    
/**
     * 
@param username
     *            the username to set
     
*/
    
public void setUsername(String username) {
        
this.username = username;
    }

    
/**
     * release resource
     
*/
    
public void signout() {
        
this.httpclient.getConnectionManager().shutdown();
    }
}

package test;

import org.apache.log4j.Logger;

import zhujia.web.WebVistor;

/**
 * 
@author 祝嘉 (Mr.Zhujia@gmail.com | TopZhujia@163.com)
 *
 
*/
public class MainTesting {



    
/**
     * 
@param args
     * 
@throws InterruptedException 
     
*/
    
public static void main(String[] args) throws InterruptedException {
        WebVistor webVistor 
= new WebVistor();
        
        Logger logger 
= Logger.getLogger(WebVistor.class);
        logger.debug(
"main(String[]) - start");

        webVistor.login();
        String uri 
= "http://topzhujia.blog.163.com/";
//        String uri = "http://topzhujia.blog.163.com/" +
//                "blog/static/165033017201062563217283/";
        webVistor.post(uri, null);
        webVistor.signout();
        
        logger.debug(
"main(String[]) - end");

    }

}

posted @ 2010-07-29 10:54  MagicLetters  阅读(385)  评论(0编辑  收藏  举报