当在浏览器中点击任何 url 时。提交了多个 get/post/delete 方法。我想捕获这些方法的状态。
尝试使用以下程序,但它给出了网页状态
String url = "http://www.google.com/";
WebClient webClient = new WebClient();
HtmlPage htmlPage = webClient.getPage(url);
try{
//verify response
Assert.assertEquals(200,htmlPage.getWebResponse().getStatusCode());
System.out.println(true);
}
最佳答案
我们有一个使用 HttpURLConnection 的实用程序类获取 GET 响应代码,但您可以使用相同的方法使用 setRequestMethod 方法获取其他方法。以下是我们如何获得 GET 响应:
private static int getResponseCode(String url) throws MalformedURLException, IOException{
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setConnectTimeout(connection_time_out);
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if(con != null){
con.disconnect();
}
return responseCode;
}
关于java - 如何使用 Webdriver 从页面捕获所有 Get/post 请求的响应状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28323305/