Selenium Check download link is active or not
Using Java and Selenium I want to check if my download link is live or not. I just want to show the http status code.
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(baseUrl);
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().getStatusCode());
I tried this but didn't have time.
+3
source to share
3 answers
I tried and it works great for checking link status. My code is here.
public static void verifyURLStatus(String URL) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(URL);
try {
HttpResponse response = client.execute(request);
// verifying response code and The HttpStatus should be 200 if not,
// increment invalid link count
// We can also check for 404 status code like response.getStatusLine().getStatusCode() == 404
int invalidLinksCount=0;
if (response.getStatusLine().getStatusCode() != 200)
invalidLinksCount++;
System.out.println(response.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
String URL="http://cdn.rentbyowner.com/sitemap/gz/site-map-0.xml.gz";
verifyURLStatus(URL);
+1
source to share
Yes. We can use a httpurl connection to test the download link. Below is the reusable code.
public static void verifyLinkActive(String linkUrl)
{
try
{
URL url = new URL(linkUrl);
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
httpURLConnect.setConnectTimeout(3000);
httpURLConnect.connect();
if(httpURLConnect.getResponseCode()==200)
{
System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage());
}
else
{
System.out.println(linkUrl+" - is Dowm");
}
} catch (Exception e)
{
}
}
Thank.
0
source to share