自动登陆网站

来源:互联网 发布:知乎痴情叔被骂 编辑:程序博客网 时间:2024/05/21 07:07

以美国adidas官网为例,思路就是模拟用户操作的登陆请求。下面摘取部分主要代码(Java)。

登陆函数

private ExecResult<Document> signin(String email, String password) {        // sign in and get cookie        try {            Elements inputElements = doc.select(".auth-validate-form input");            List<NameValuePair> params = AdidasUtils.fillParamsWithInputElements(inputElements);            if (params == null || params.size() <= 0) {                inputElements = doc.select(".auth-pagelet-container form input");                params = AdidasUtils.fillParamsWithInputElements(inputElements);            }            params = AdidasUtils.fillLoginParams(email, password, params);            AdidasUtils.getHttpPostResponseWithDocument(AdidasConstants.SIGNIN_URL, doc.location(), params, httpsClient);        } catch (Exception e) {            LogUtils.info(AdidasConstants.ERROR_PREFIX + e.toString());        }        return createResult(true, doc, null);    }
    public static List<NameValuePair> fillParamsWithInputElements(Elements inputElements) {        List<NameValuePair> params = new ArrayList<NameValuePair>();        LogUtils.info("Params size: " + inputElements.size());        for (Element inputElement : inputElements) {            if (!StringUtils.isBlank(inputElement.attr("name"))) {                if (!StringUtils.isBlank(inputElement.attr("type")) &&                        "radio".equalsIgnoreCase(inputElement.attr("type")) &&                        !inputElement.hasAttr("checked")) {                    continue;                }                params.add(new BasicNameValuePair(inputElement.attr("name"), inputElement.val()));            }        }        return params;    }

设置登陆参数

public static List<NameValuePair> fillLoginParams(String email, String password, List<NameValuePair> params) {        List<NameValuePair> filteredList = filterOutInputValues(AdidasConstants.LOGIN_BLACK_KEYS, params);        filteredList.add(new BasicNameValuePair("username", email));        filteredList.add(new BasicNameValuePair("password", password));        filteredList.add(new BasicNameValuePair("remembermeParam", "true"));        filteredList.add(new BasicNameValuePair("signinSubmit", "Sign in"));        return filteredList;    }

发送请求

    public static Document getHttpPostResponseWithDocument(String url, String referrer, List<NameValuePair> params,DecompressingHttpClient httpClient) throws IOException {        response = getHttpPostResponse(url, referrer, params, httpClient);        Document doc = Jsoup.parse(EntityUtils.toString(response.getEntity(), "UTF-8"));        EntityUtils.consume(response.getEntity());        return doc;    }public static HttpResponse getHttpPostResponse(String url, String referrer, List<NameValuePair> params, DecompressingHttpClient httpClient) throws IOException {        HttpPost post = new HttpPost(url);        setHeaders(post);        if (!StringUtils.isBlank(referrer)) {            post.setHeader("Referer", referrer);        }        if (params != null) {            post.setEntity(new UrlEncodedFormEntity(params));            for (NameValuePair pair : params) {                print(pair.getName() + "=" + pair.getValue());            }        }        return httpClient.execute(post);    }
阅读全文
0 0
原创粉丝点击