Reputation: 875
When hit any url in browser. Multiple get/post/delete method is submitted. I want to capture a status of these methods.
Tried with below program but it gives a webpage status
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);
}
Upvotes: 1
Views: 919
Reputation: 300
We have a utility class using HttpURLConnection to get GET response codes, but you could use that same approach to get other methods using the setRequestMethod method. Here's how we get the GET responses:
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;
}
Upvotes: 1