我的httpclient,登录校内获取内部的代码

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

 

public static void main(String args[]){
   HttpClient client = new HttpClient();//相当于客户端浏览器  
         client.getHostConfiguration().setHost("www.renren.com", 80);//比如www.oschina.net 80是端口  
         try {  
           
           PostMethod post1 = new PostMethod("http://www.renren.com/PLogin.do");
                 NameValuePair ie = new NameValuePair("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
           NameValuePair[] data =
             {
               new NameValuePair("email", "[email protected]"),
               new NameValuePair("password", "XXXXXX"),
              //这里应该添加的是用户名和密码
               new NameValuePair("origURL","http://www.renren.com/Home.do"),
               new NameValuePair("domain","renren.com"),
               new NameValuePair("domain","renren.com"),
               ie
             };
           post1.setRequestBody(data);
                
                 //登陆或注册的提交路径  
                int  statusCode = client.executeMethod(post1);    
                 if(HttpStatus.SC_OK==statusCode){//状态码200表示请求成功,先发送去请求验证码  
                     System.out.println(post1.getResponseBodyAsString());

               //简单查看一下打印信息有没有成功的信息打印出来的是首页的相关信息的内容。  
                 } 
              
                 post1.releaseConnection();//释放连接 
             }  
        catch (HttpException e) {  
             e.printStackTrace();  
         } catch (IOException e) {  
             e.printStackTrace();  
         }  

 }

 

上面打印出来的是首页的相关的用户名和密码,但是在这里我们不仅需要打印首页的用户名和密码,在下面的代码中是继续的打印相关的别的位置的用户名和密码。

其他的页面可能用get的方法就可以解决了。

 

Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);

 

 

GetMethod get = new GetMethod(newUrl);
  get.setRequestHeader("Cookie", cookies.toString());
  client.executeMethod(get);
  String responseString = get.getResponseBodyAsString();
  //登录后首页的内容   
  System.out.println(responseString);
  get.releaseConnection();

 

 

你可能感兴趣的:(apache,windows,.net,浏览器,IE)