httpclient4.5的get和post方法

package an;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class PostForm {
	
	/*
	 * 自动填单
	 */
	public String hcPost() throws ClientProtocolException, IOException, URISyntaxException  {
		
		BasicCookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .build();
        try {
            HttpGet httpget = new HttpGet("https://passport.csdn.net/account/login");
            CloseableHttpResponse response1 = httpclient.execute(httpget);
            try {
                HttpEntity entity = response1.getEntity();

                System.out.println("Login form get: " + response1.getStatusLine());
                EntityUtils.consume(entity);
                
                System.out.println("Initial set of cookies:");
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i).toString());
                    }
                }
            } finally {
                response1.close();
            }

            HttpUriRequest login = RequestBuilder.post()
                    .setUri(new URI("https://passport.csdn.net/account/login"))
                    .addParameter("username", "my")
                    .addParameter("password", "123")
                    //.addParameter("IDToken2", "password")
                    .build();
            CloseableHttpResponse response2 = httpclient.execute(login);
            try {
                HttpEntity entity = response2.getEntity();

                System.out.println("Login form get: " + response2.getStatusLine());
                EntityUtils.consume(entity);
                
                System.out.println("Post logon cookies:");
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("- " + cookies.get(i).toString());
                    }
                }
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
        return "yes";
	}
	
	public static void main(String[]args) throws Exception{
		
		PostForm pf=new PostForm();	
        
        String ret=pf.hcPost();
        
        System.out.println("********"+ret);  
  
	}
}

httpclient提交后用htmlparser解析内容:

package an;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
import org.htmlparser.util.ParserException;

public class PostForm {
	
	public String hcPost() throws ClientProtocolException, IOException, URISyntaxException, ParserException, ParseException  {
		
        CloseableHttpClient httpclient = HttpClients.custom().build();
        HttpUriRequest post = RequestBuilder.post()
                    .setUri(new URI("http://mail.163.com/"))
                    .addParameter("username", "my")
                    .addParameter("password", "123")
                    //.addParameter("IDToken2", "password")
                    .build();
        CloseableHttpResponse response = httpclient.execute(post); 
        try {  
        	HttpEntity entity = response.getEntity();//中文乱码
            if( response.getStatusLine().getStatusCode() == 200){
            	StringBean sb = new StringBean ();
        		sb.setLinks (false);//设置不需要得到页面所包含的链接信息  
        		sb.setReplaceNonBreakingSpaces(true);//设置将不间断空格由正规空格所替代  
        		sb.setCollapse(true);//设置将一序列空格由一个单一空格所代替  
        		Parser parser = new Parser();
        		parser.setInputHTML(EntityUtils.toString(entity));
        		parser.reset ();
        		parser.visitAllNodesWith (sb);
        		String text = sb.getStrings ();
            	return text;
            }
            
        } finally { response.close();}
        return response.getStatusLine().toString();
	}
	
	public static void main(String[]args) throws Exception{
		
		PostForm pf=new PostForm();	
        
        String ret=pf.hcPost();
        
        System.out.println("********"+ret);  
  
	}
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值