httpclient with login cookies

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
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.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LoginUtil {
    public static CloseableHttpClient Login() {
        BasicCookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpPost post = new HttpPost("http://test.com/login.action");
        try {
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("name", "name"));
            nvps.add(new BasicNameValuePair("pwd", "pwd"));
            post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            post.setHeader("Content-type", "application/x-www-form-urlencoded");
            post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36");
            HttpResponse response = httpclient.execute(post);
            String body =  EntityUtils.toString(response.getEntity(), Charset.forName("utf-8"));
            System.out.println(body);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return httpclient;
    }

    public static String sendGet(String url, CloseableHttpClient closeableHttpClient){
        HttpGet httpGet =new HttpGet(url);

        try {
            CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
            String body =  EntityUtils.toString(response.getEntity(), Charset.forName("utf-8"));
            return body;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendPost(String url,Map<String, String> param,CloseableHttpClient closeableHttpClient){
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> params=new ArrayList<NameValuePair>();

        if(param != null && param.size() > 0) {
            for(Map.Entry<String, String> entry : param.entrySet()) {
                params.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
            }
        }
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
            String body =  EntityUtils.toString(response.getEntity(), Charset.forName("utf-8"));
            return body;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值