Form login via Proxy

          Posted on 2010-07-29 10:54 祝嘉 閱讀(448) 評論(0)  編輯  收藏 所屬分類: Code_HttpClient
          /*
           * @(#)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");

              }

          }


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 黄石市| 正镶白旗| 济源市| 大悟县| 黄平县| 广水市| 浮梁县| 长武县| 洪泽县| 兴国县| 湟源县| 镇巴县| 甘孜| 横峰县| 鲁甸县| 扎囊县| 宁河县| 博乐市| 娱乐| 布尔津县| 班玛县| 泸西县| 阳信县| 双辽市| 库车县| 邓州市| 缙云县| 外汇| 信阳市| 河北区| 虞城县| 台北县| 齐齐哈尔市| 双牌县| 莫力| 娱乐| 连云港市| 潍坊市| 横峰县| 临武县| 德州市|