Java使用HTTPS登录网站代码实现

步骤一,获得网站的登录URL,及Form页元数据

这点可以通过Chrome的F12按键,检查登录时发送的数据包获得(【Preserve Log】建议打勾)。以登录京东网站为例,示范如下:

1,【Network】标签页,【Headers】项,可以查看登录URL

鼠标右键单击【Request URL】参数,选择【Copy Value】即可复制值。

 2,【Network】标签页,【Payload】项,可以获取Form页元数据

同样地,鼠标右键单击各参数,选择【Copy Value】即可复制值。

 

 步骤二,编写登录代码

package com.clzhang.sample.net;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
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.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class Login2JD {

    // 登录URL
    public static final String LOGIN_URL = "https://passport.jd.com/uc/loginService?uuid=a4bc68e0-2163-45e7-a60e-2bad2d90bb17&ltype=logout&ReturnUrl=https://order.jd.com/center/list.action&r=0.5708092007940808&version=2015";

    @Test
    public void login() throws IOException {
        // 获取连接客户端工具
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 参数封装对象
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            params.add(new BasicNameValuePair("uuid", "a4bc68e0-2163-45e7-a60e-2bad2d90bb17"));
            params.add(new BasicNameValuePair("eid",
                    "UGLYRKJMGDSAYCBIQEFQP4QZ3AKIUDCPLHPCY5BEFAL55RMV5XZ3S7RCPNXSWKDD6IEWOMV4EYTCGOFIKTBWIHD5BM"));
            params.add(new BasicNameValuePair("fp", "fb63ee37f64f02acb247dbbd015c066a"));
            params.add(new BasicNameValuePair("_t", "_t"));
            params.add(new BasicNameValuePair("loginType", "c"));
            params.add(new BasicNameValuePair("loginname", "13801022222"));
            params.add(new BasicNameValuePair("nloginpwd",
                    "fh/NyoAVnxRwqLRyZOdyehdjaldHfWmpPwioppQMhP2Z4AoBYhUklddJJuhLndlaMAvYF0Ijq6pI95r/JuqiKloPOfBnmA+Hu7kKW6zPv71MocU8sxI0syN+YhujdkNkl9kahpZjZw="));
            params.add(new BasicNameValuePair("authcode", "fcd912b70d024852a8ecb98e8db02019"));
            params.add(new BasicNameValuePair("pubKey",
                    "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB"));
            params.add(new BasicNameValuePair("sa_token",
                    "B68C442BE645754F33277E701208059080DD726A94A73F76DEC3053A838549C06EB7D3797CE1C5BBE7C2B2EF9CA7D467C3C76FF0A28885EE64B432120BA9B13D348C69B7D2A54084AD0AF9F604987E3FF4F05CFA833594DEAA638A1460132F8E4FC41F9984A0550F77FF3A51047D9FFA6937B2323ADE6CDB3A98776094AD46AFC0D104BE5A33FD4B2D219E65930F424CA62E9A76B0553DAFABE006ED3256B130266A76FAF5CCE3ADAB479A1B91EBE72C1A4BF98524844B1E83C710FCA76C98445A859CE2250B09F1F46964521C4584F34AA325E2CCB21063230D888A26FF50EA53E8585AC059B0167F5C76E28EC36A30964D34551EDD3242C4BDF09CB4B5A3CCBBF039E1998C8FC1525EF9FEDC38B0F16718EA67EA49BF24BF2FDDA53EEC8CCCB135E6BAF4D8445ECC4313595E9D5AC1E67CADD48C6334B8300DB8E65232CD1D04D9FDFE6B8416AB0E96556FC9C5128E6A83968900DFAD1DC70F8BE27D222C5AC6F5008B02D00D3A91E1F684EA8F8795CA3F94A60E91608C790FCC52A0DE0EBB4FDB7E3EE2611370D8A13471CC91D3740F35D400D7028ABFE18C1A1D918568CE6130E2830997AA1B8BEDAA754CC33ECABA5C41BB1340E6ABD924E215E2BFC0F19EC35C5B656DFE5E55B7B3858A3819151521D320A569E8945F10348A454AD34044EEE8E3E2FD7F7415F83F0187923BA39E51007B5E4180D131FA3F1CA307A0C2E6FAEAF56178E1147FC6DAD1B6770A616032701B391E17A308EDA2AE8CD969E8"));
            params.add(new BasicNameValuePair("seqSid", "215692539242879832"));
            params.add(new BasicNameValuePair("useSlideAuthCode", "1"));

            // 创建POST请求对象
            HttpPost httpPost = new HttpPost(LOGIN_URL);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.addHeader("User-Agent:",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            // 执行请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            int code = response.getStatusLine().getStatusCode();
            System.out.println("StatusCode: " + code);
            String entityStr = EntityUtils.toString(entity, "UTF-8");
            System.out.println("返回结果: " + entityStr);

            response.close();
        } finally {
            // 释放资源
            if (httpClient != null) {
                httpClient.close();
            }
        }
    }
}

步骤三,引用相关包

修改pom.xml,加入如下内容:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

         <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>

补充,如果是简单的HPPS网站登录,可以用如下方式

    
    /**
     * 登录到网站的子程序,返回CloseableHttpClient
     * @return 登录成功,返回CloseableHttpClient对象
     * @throws Exception
     */
    private CloseableHttpClient login2JHL() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建POST请求对象
        HttpPost httpPost = new HttpPost(LOGIN_URL);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpPost.addHeader("User-Agent:",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("username", "myuser1"));
        params.add(new BasicNameValuePair("password", "mypassword1"));

        // 使用URL实体转换工具
        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(params, "UTF-8");
        httpPost.setEntity(entityParam);

        // 执行请求
        CloseableHttpResponse response = httpClient.execute(httpPost);

        // 获得响应的实体对象
//        HttpEntity entity = response.getEntity();
//        int code = response.getStatusLine().getStatusCode();
//        String entityStr = EntityUtils.toString(entity, "UTF-8");
//        System.out.println("StatusCode: " + code);
//        System.out.println("返回结果: " + entityStr);

        response.close();

        return httpClient;
    }

 

posted @ 2022-05-14 08:20  那些年的事儿  阅读(2168)  评论(0编辑  收藏  举报