java 脚本ssl语言,在给定的Java代码中禁用SSL连接

我在下面的代码中收到安全证书错误(source) . 例外是:

javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径引起:sun.security.provider . certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径 .

如何禁用安全证书检查?

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

import com.gargoylesoftware.htmlunit.Page;

import com.gargoylesoftware.htmlunit.WebClient;

import com.gargoylesoftware.htmlunit.html.DomNode;

import com.gargoylesoftware.htmlunit.html.HtmlElement;

import com.gargoylesoftware.htmlunit.html.HtmlFileInput;

import com.gargoylesoftware.htmlunit.html.HtmlForm;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;

import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class WebRobot {

public static void login(String username, String password) {

String loginUrl = "http://example.com";

int loginFormNum = 1;

String usernameInputName = "nameinput";

String passwordInputName = "passinput";

String submitLoginButtonValue = "Sign In";

// create the HTMLUnit WebClient instance

WebClient wclient = new WebClient();

// configure WebClient based on your desired

wclient.getOptions().setPrintContentOnFailingStatusCode(false);

wclient.getOptions().setCssEnabled(false);

wclient.getOptions().setThrowExceptionOnFailingStatusCode(false);

wclient.getOptions().setThrowExceptionOnScriptError(false);

try {

// get the login page by connect to the URL

final HtmlPage loginPage = (HtmlPage)wclient.getPage(loginUrl);

// get the login form by its form number. mine is 1 (form[1])

final HtmlForm loginForm = loginPage.getForms().get(loginFormNum);

// get the text input field by the name and set the value

final HtmlTextInput txtUser = loginForm.getInputByName(usernameInputName);

txtUser.setValueAttribute(username);

// get the password input field by the name and set the value

final HtmlPasswordInput txtpass = loginForm.getInputByName(passwordInputName);

txtpass.setValueAttribute(password);

// get the submit button by the text value

final HtmlSubmitInput submitLogin = loginForm.getInputByValue(submitLoginButtonValue);

// after we set the name & password then we click the submit button

// it will return a page (redirect or message alert or somethin, different sites different behaviour)

// or it could throws an exception

final HtmlPage returnPage = submitLogin.click();

// we can analyze the return page based on the body

// ex: mine is check if its body contains less than 5 elements

final HtmlElement returnBody = returnPage.getBody();

if (returnBody==null || returnBody.getChildElementCount()<5) {

// bla.. bla.. bla..

}

} catch(FailingHttpStatusCodeException e) {

e.printStackTrace();

} catch(Exception e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值