Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2060961
  • 博文数量: 178
  • 博客积分: 2076
  • 博客等级: 大尉
  • 技术积分: 2800
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-10 10:50
文章分类

全部博文(178)

文章存档

2010年(4)

2009年(13)

2008年(161)

我的朋友

分类: Java

2008-05-12 23:25:25

import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.UrlEncodedFormEntity;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import java.io.*;
import java.util.regex.*;
/**
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 */
public class Client163Mail {
    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(
                ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        HttpPost httpost = new HttpPost("");
        //&product=&savelogin=&outfoxer=
        NameValuePair[] nvps = new NameValuePair[] {
                new BasicNameValuePair("username", "xxx"),
                new BasicNameValuePair("password", "2xxxx"),
                new BasicNameValuePair("product", "mail163"),
                new BasicNameValuePair("selType", "jy"),
                new BasicNameValuePair("logType", "2")
        };
       
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
       
  System.out.println(new java.util.Date());
  
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();
       
        java.io.FileOutputStream htmlOutStream=new java.io.FileOutputStream("c:\\sid1.html");
    entity.writeTo(htmlOutStream);
    htmlOutStream.close();
    
        System.out.println("Login form get: " + response.getStatusLine());
      
  if (entity != null) {
            entity.consumeContent();
        }
  String sid="";
        System.out.println("Post logon cookies:");   
        List cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");   
        } else {
            for (int i = 0; i < cookies.size(); i++)
   {
                System.out.println(cookies.get(i).getDomain() + cookies.get(i).getName()+"- " + cookies.get(i).getValue());
    String temp=cookies.get(i).getValue();
    sid=temp.substring(temp.indexOf("%")+1,temp.length());
    
            }
   for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());   
            }
        }
System.out.println("第一次取得:sid------------------------------------------" + sid);
  
  httpost = new HttpPost("" );
                         
  response = httpclient.execute(httpost);
        entity = response.getEntity();
       
        htmlOutStream=new java.io.FileOutputStream("c:\\sid2.html");
    entity.writeTo(htmlOutStream);
    htmlOutStream.close();
    
        System.out.println("Login form get: " + response.getStatusLine());        
        System.out.println("Post logon cookies:");   
       
        cookies = httpclient.getCookieStore().getCookies();       
System.out.println(cookies.size());
        if (cookies.isEmpty()) {
            System.out.println("None");   
        } else {
            for (int i = 0; i < cookies.size(); i++)
   {
                System.out.println(cookies.get(i).getDomain() + cookies.get(i).getName()+"- " + cookies.get(i).getValue());
    String temp=cookies.get(i).getValue();
    sid=temp.substring(temp.indexOf("%")+1,temp.length());
    
            }
   for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());   
            }
        }
System.out.println("第二次取得:sid------------------------------------------" + sid);
  HttpGet httpget = new HttpGet("");
        response = httpclient.execute(httpget);
        entity = response.getEntity();
       
  String name="",email="";
  Pattern pLinePattern;
  Matcher pLineMatcher;
        System.out.println("Login form get: " + response.getStatusLine());
  BufferedReader bufferReader=new BufferedReader(new InputStreamReader(entity.getContent()));
  String lineString=bufferReader.readLine();
  while(lineString!=null)
  {
//System.out.println(lineString);   
   pLinePattern=Pattern.compile("(.*)|(.*)");
      pLineMatcher=pLinePattern.matcher(lineString);
      while(pLineMatcher.find())
   {
    String pMatchString=pLineMatcher.group(1);
    if(pMatchString!=null && !pMatchString.equals(""))
     name=pMatchString;
    pMatchString=pLineMatcher.group(2);
    if(pMatchString!=null && !pMatchString.equals(""))
     email=pMatchString;
      }
   if(!email.equals("") && !name.equals(""))
   {
    System.out.println("邮箱名字: " + name + "    Email地址: "+email);
    email=name="";
   }
   lineString=bufferReader.readLine();
  }
  
  System.out.println(new java.util.Date());
    }
}

 
阅读(3015) | 评论(0) | 转发(0) |
0

上一篇:JAVA相关基础知识

下一篇:Client163

给主人留下些什么吧!~~