当前位置: 代码迷 >> 综合 >> http请求获取301重定向的Location值
  详细解决方案

http请求获取301重定向的Location值

热度:39   发布时间:2024-02-02 00:06:48.0
public static String getLocationUrl(String url) {RequestConfig config = RequestConfig.custom().setConnectTimeout(50000).setConnectionRequestTimeout(10000).setSocketTimeout(50000).setRedirectsEnabled(false).build();//不允许重定向   CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build(); String location = null;int responseCode = 0;HttpResponse response;try {response = httpClient.execute(new HttpGet(url));responseCode = response.getStatusLine().getStatusCode();if (responseCode == 301) {org.apache.http.Header locationHeader = response.getFirstHeader("Location");location = locationHeader.getValue();}} catch (Exception e) {e.printStackTrace();}return location;}

 

  相关解决方案