httpclient cookie

本文提供了一个使用Java实现的小米登录示例程序。该程序通过Apache HttpClient库发送HTTP请求,并设置了必要的SSL信任策略来处理HTTPS连接。它展示了如何构造带有特定参数的URL、设置HTTP头信息并获取响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
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.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

public class XiaomiLogin {
    public static void main(String[] args) throws URISyntaxException,
            ClientProtocolException, IOException, NoSuchAlgorithmException,
            KeyStoreException, KeyManagementException {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        CookieStore cookieStore = new BasicCookieStore();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                builder.build());

        RequestConfig globalConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.DEFAULT).build();

        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .setDefaultRequestConfig(globalConfig)
                .setDefaultCookieStore(cookieStore).build();

        URI uri = new URIBuilder()
                .setScheme("https")
                .setHost("uac.10010.com")
                .setPath("/portal/Service/MallLogin")
                .setParameter(
                        "callback",
                        "jQuery172018417995200171355_"
                                + Long.toString(System.currentTimeMillis()))
                .setParameter("redirectURL", "http://www.10010.com")
                .setParameter("_", Long.toString(System.currentTimeMillis()))
                .setParameter("password", "XXXXXX")
                .setParameter("productType", "01")
                .setParameter("pwdType", "01")
                .setParameter("redirectType", "01")
                .setParameter("rememberMe", "1")
                .setParameter("userName", "186*****56").build();
        System.out.println(uri);

        HttpGet get = new HttpGet(uri);

        get.addHeader("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        get.addHeader("Accept-Language", "en-US,en;q=0.5");
        get.addHeader("Accept-Encoding", "gzip, deflate");
        get.addHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.4) Gecko/20150509 Firefox/31.9 PaleMoon/25.4.1");
        get.addHeader("Connection", "Keep-Alive");

        CloseableHttpResponse response = httpclient.execute(get);
        try {
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));

            // Header[] headers = response.getAllHeaders();
            // for (Header header : headers) {
            // System.out
            // .println(header.getName() + " : " + header.getValue());
            // }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            response.close();
        }
        System.out
                .println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        System.out.println();
        URI uri2 = new URIBuilder().setScheme("http").setHost("iservice.10010.com")
                .setPath("/e3/query/call_dan.html")
                .setParameter("menuId", "000100030001").build();
        System.out.println(uri2);
        HttpGet get1 = new HttpGet(uri2);
        get1.addHeader(
                "Accept",
                "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
        get1.addHeader("Accept-Language", "en-US");
        get1.addHeader("Accept-Encoding", "gzip, deflate");
        get1.addHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.4) Gecko/20150509 Firefox/31.9 PaleMoon/25.4.1");
        get1.addHeader("Connection", "Keep-Alive");
        CloseableHttpResponse response1 = httpclient.execute(get1);
        System.out.println(response1.getStatusLine());
        HttpEntity entity = response1.getEntity();
        System.out.println(EntityUtils.toString(entity));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值