首页 > 编程知识 正文

httpclient官网,httpclient jar包

时间:2023-05-03 23:40:03 阅读:223258 作者:1382


package com.demo;
import java.io.File;import org.apache.commons.httpclient.Cookie;    
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;    
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;    
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;    
import org.apache.commons.httpclient.methods.PostMethod; 
public class LoginRenRen {    
private static final String LOGON_SITE = "www.renren.com";    
private static final int LOGON_PORT = 80;    
public static void main(Stringxsdlt args)throws Exception {       client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); //配置服务器参量 
   client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); //配置Cookie策略为浏览器默认策略   
   //登录页面    
   PostMethod post = new PostMethod("http://www.renren.com/PLogin.do");    
   NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");       
   NameValuePair url = new NameValuePair("origdzddy", "http://www.renren.com/Home.do"); 
   //NameValuePair domain = new NameValuePair("domain", "renren.com"); 
   NameValuePair username = new NameValuePair("email","帐号");    
   NameValuePair password = new NameValuePair("password", "密码");
   NameValuePair isplogin = new NameValuePair("isplogin", "true");
   NameValuePair formName = new NameValuePair("formName", "");
   NameValuePair method = new NameValuePair("method", "");
   post.setRequestBody(new NameValuePairxsdlt {ie,url,username, password,isplogin,formName,method});    
   int statusCode = client.executeMethod(post); 
   if (statusCode == HttpStatus.活泼的犀牛MOVED_PERMANENTLY || 
     statusCode == HttpStatus.活泼的犀牛MOVED_TEMPORARILY) { 
    // 从头中取出转向的地址 
    Header locationHeader = post.getResponseHeader("location"); 
    String location = null; 
    if (locationHeader != null) { 
     location = locationHeader.getValue(); 
     System.out.println("页面重定向:" + location); 
    } else { 
     System.err.println("Location field value is null."); 
     //return;
    }     
   } 
   System.out.println("******************************登录******************************");    
   Cookiexsdlt cookies = client.getState().getCookies();    
   client.getState().addCookies(cookies); 
   post.releaseConnection();    
   System.out.println("******************************页面转向******************************");    
   String newUrl="http://www.renren.com/home";    
   System.out.println("=================================Cookies==============================");    
   int i=0;    
   for(Cookie c:cookies){       }    
   client.getState().addCookies(cookies);    
   post.releaseConnection();    
   GetMethod get = new GetMethod(newUrl);    
   get.setRequestHeader("Cookie", cookies.toString());    
   client.executeMethod(get);    
   String responseString = get.getResponseBodyAsString();    
   //登录后首页的内容      
   //System.out.println(responseString);    
   get.releaseConnection();    
//   System.out.println("******************************组件功能******************************"); 
//   System.out.println("******************************(1)进入首页******************************");    
//   String slave="http://www.renren.com/home";    
//   get = new GetMethod(slave);    
//   get.setRequestHeader("Cookie", cookies.toString());    
//   client.executeMethod(get);    
//   responseString = get.getResponseBodyAsString();    
//   System.out.println(responseString);    
//   get.releaseConnection();   
}   }  转自:http://blog.csdn.net/yodlove/archive/2010/10/13/5938022.aspx

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。