selenium把html转化为json,java - Selenium get HTML (or JSON) response from WebDriver - Stack Overflow...

The above code didn't wind up working for me in the long run. I don't recall why it didn't work (since its been a while now) but here is my new answer:

/**

* CURLs the page passed in for the HTML response. Uses AJAX so base domain must match the url.

*

* Special exception for MkItem requests

*

* @param url - URL to CURL

* @param debug - Pass in true for debug output.

* @return HTML result as a string.

* @throws IOException

*/

public static String getHTMLResponse(String url, boolean debug) throws IOException {

if(debug){System.out.println("Making request to:\n\n" + url);}

String body = "";

try{

// Create a trust manager that does not validate certificate chains

TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {

public java.security.cert.X509Certificate[] getAcceptedIssuers() {

return null;

}

public void checkClientTrusted(X509Certificate[] certs, String authType) {

}

public void checkServerTrusted(X509Certificate[] certs, String authType) {

}

}

};

// Install the all-trusting trust manager

SSLContext sc = SSLContext.getInstance("SSL");

sc.init(null, trustAllCerts, new java.security.SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier

HostnameVerifier allHostsValid = new HostnameVerifier() {

public boolean verify(String hostname, SSLSession session) {

return true;

}

};

// Install the all-trusting host verifier

HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

}catch (Exception e){

Assert.fail("Exception thrown trying to perform GET request. " + e.getMessage());

}

URLConnection con;

//If not a secure page, use cURL

/*if(!url.contains("https://")){*/

try{

URL temp = new URL(url);

con = temp.openConnection();

InputStream in = con.getInputStream();

String encoding = con.getContentEncoding();

encoding = encoding == null ? "UTF-8" : encoding;

body = IOUtils.toString(in, encoding);

} catch (MalformedURLException e){

Assert.fail("URL wasn't passed in correctly - " + e.getMessage());

} catch (IOException e){

throw e;

}

if(debug){ System.out.println("Body:\n"+ body); }

Assert.assertNotNull("Body returned null for " + url, body);

return body;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值