Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有一种spring方法来检查网页是否仍然存在?_Java_Spring - Fatal编程技术网

Java 是否有一种spring方法来检查网页是否仍然存在?

Java 是否有一种spring方法来检查网页是否仍然存在?,java,spring,Java,Spring,我想让我的服务器检查,如果亚马逊附属链接仍然存在。 我遇到了这样一个线程,它解释了如何做 但我想知道Spring是否有一个模块可以让我实现同样的功能。我已经看过SpringWeb服务,但这似乎就是我想要的。有人知道Spring是否能做到这一点吗 问候,,您可以做的一件事是向页面发送一个简单的get请求,并根据响应状态定义该请求是否在线。您可以做的一件事是向页面发送一个简单的get请求,并根据响应状态定义该请求是否在线。您可以使用Spring的RestTemplate获取网站的标题。 大概是这样的

我想让我的服务器检查,如果亚马逊附属链接仍然存在。 我遇到了这样一个线程,它解释了如何做

但我想知道Spring是否有一个模块可以让我实现同样的功能。我已经看过SpringWeb服务,但这似乎就是我想要的。有人知道Spring是否能做到这一点吗


问候,,

您可以做的一件事是向页面发送一个简单的get请求,并根据响应状态定义该请求是否在线。

您可以做的一件事是向页面发送一个简单的get请求,并根据响应状态定义该请求是否在线。

您可以使用Spring的
RestTemplate
获取网站的标题。
大概是这样的:

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]}

如果网站不存在,它将抛出运行时异常,并出现404错误。

您可以使用Spring的
RestTemplate
获取网站的标题。
大概是这样的:

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]}

如果该网站不存在,它将抛出运行时异常,出现404错误。

我刚才也在想同样的事情。谢谢你的回答。我刚才也在想同样的事情。谢谢你的回答。这个网页;它有rest端点吗?在计时器上使用
restemplate
轮询一些轻量级请求。我建议您调查是否存在类似于健康检查端点的内容;它有rest端点吗?在计时器上使用
restemplate
轮询一些轻量级请求。我建议您调查是否存在类似于运行状况检查终结点的内容。这种方法更适合restTemplate.exchange(url、HttpMethod.GET、entity、String.class)。getStatusCodeValue();如果网站返回403 Unauthorized,它将抛出一个错误并设置pageExists=False。这样做更好的方法是restTemplate.exchange(url,HttpMethod.GET,entity,String.class)。getStatusCodeValue();如果网站返回403 Unauthorized,它将抛出一个错误并将pageExists设置为false