登陆人人网的代码,测试OK

Java代码    收藏代码
  1. package renren;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.http.Header;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.ResponseHandler;  
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  11. import org.apache.http.client.methods.HttpGet;  
  12. import org.apache.http.client.methods.HttpPost;  
  13. import org.apache.http.impl.client.BasicResponseHandler;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15. import org.apache.http.message.BasicNameValuePair;  
  16. import org.apache.http.protocol.HTTP;  
  17.   
  18. public class Login {  
  19.       
  20.      private static String renRenLoginURL = "http://www.renren.com/PLogin.do";    
  21.        
  22.        private static String redirectURL = "http://www.renren.com/SysHome.do";  
  23.        private static String userName = "**";  
  24.        private static String password = "***";  
  25.        private HttpResponse response;    
  26.         private DefaultHttpClient httpclient = new DefaultHttpClient();  
  27.       
  28.       
  29.       
  30.       
  31.       
  32.       public static void main(String[] args) {    
  33.           Login login = new Login();    
  34.             login.printText();    
  35.         }    
  36.   
  37.         
  38.       private boolean login() {    
  39.             HttpPost httpost = new HttpPost(renRenLoginURL);    
  40.             // All the parameters post to the web site    
  41.             List<NameValuePair> nvps = new ArrayList<NameValuePair>();    
  42.             nvps.add(new BasicNameValuePair("origURL", redirectURL));    
  43.             nvps.add(new BasicNameValuePair("domain""renren.com"));    
  44.             nvps.add(new BasicNameValuePair("isplogin""true"));    
  45.             nvps.add(new BasicNameValuePair("formName"""));    
  46.             nvps.add(new BasicNameValuePair("method"""));    
  47.             nvps.add(new BasicNameValuePair("submit""登录"));    
  48.             nvps.add(new BasicNameValuePair("email", userName));    
  49.             nvps.add(new BasicNameValuePair("password", password));    
  50.             try {    
  51.                 httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));    
  52.                 response = httpclient.execute(httpost);    
  53.             } catch (Exception e) {    
  54.                 e.printStackTrace();    
  55.                 return false;    
  56.             } finally {    
  57.                 httpost.abort();    
  58.             }    
  59.             return true;    
  60.         }    
  61.         private String getRedirectLocation() {    
  62.             Header locationHeader = response.getFirstHeader("Location");    
  63.             if (locationHeader == null) {    
  64.                 return null;    
  65.             }    
  66.             return locationHeader.getValue();    
  67.         }    
  68.           
  69.         private String getText(String redirectLocation) {    
  70.             HttpGet httpget = new HttpGet(redirectLocation);    
  71.             // Create a response handler    
  72.             ResponseHandler<String> responseHandler = new BasicResponseHandler();    
  73.             String responseBody = "";    
  74.             try {    
  75.                 responseBody = httpclient.execute(httpget, responseHandler);    
  76.             } catch (Exception e) {    
  77.                 e.printStackTrace();    
  78.                 responseBody = null;    
  79.             } finally {    
  80.                 httpget.abort();    
  81.                 httpclient.getConnectionManager().shutdown();    
  82.             }    
  83.             return responseBody;    
  84.         }    
  85.         
  86.         public void printText() {    
  87.             if (login()) {    
  88.                 String redirectLocation = getRedirectLocation();    
  89.                 if (redirectLocation != null) {    
  90.                     System.out.println(getText(redirectLocation));    
  91.                 }    
  92.             }    
  93.         }    
  94.         
  95.         
  96.         
  97. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值