I want to have my server check if amazon affiliate links still exist. I came across this SO thread that explains how to do it. How do i check if a webpage exists with java?
But i'm wondering if Spring has a module that allows me to achieve the same. I've already looked at Spring web-services but that doesent seem to be what i'm looking for. Does anyone know if Spring can do this?
Regards,
You could use Spring's RestTemplate to get the headers of a website.
Something like this:
RestTemplate restTemplate = new RestTemplate();
boolean pageExists = true;
HttpHeaders headers = null;
try {
headers = restTemplate
.headForHeaders("http://www.youtube.com");
} catch (RestClientException e) {
pageExists = false;
}
System.out.println(headers);
// {Location=[https://www.youtube.com/], Content-Length=[0], Expires=[Tue, 27 Apr 1971 19:44:06 EST], X-Content-Type-Options=[nosniff], P3P=[CP="This is not a P3P policy! See http://support.google.com/accounts/answer/151657?hl=en for more info."], Content-Type=[text/html; charset=utf-8], Cache-Control=[no-cache], X-XSS-Protection=[1; mode=block; report=https://www.google.com/appserve/security-bugs/log/youtube], Date=[Sat, 04 Nov 2017 10:21:53 GMT], Server=[YouTube Frontend Proxy], Set-Cookie=[VISITOR_INFO1_LIVE=rbqBq4s5uxo; path=/; domain=.youtube.com; expires=Thu, 05-Jul-2018 22:14:53 GMT; httponly, YSC=2VLECOABq0U; path=/; domain=.youtube.com; httponly]}
If the website doesn't exist it will throw a the RuntimeException with the 404 error.