JAVA核心知识点--HttpClient获取302响应中的Location头信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pengjunlee/article/details/89043987

HttpClient获取302响应中的Location头信息

	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 == 302) {
				Header locationHeader = response.getFirstHeader("Location");
				location = locationHeader.getValue();
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return location;
	}

猜你喜欢

转载自blog.csdn.net/pengjunlee/article/details/89043987