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

时间:2024-10-18 14:32:28

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

	public static String getLocationUrl(String url) {
		RequestConfig config = ().setConnectTimeout(50000).setConnectionRequestTimeout(10000).setSocketTimeout(50000)
                .setRedirectsEnabled(false).build();//不允许重定向   
		CloseableHttpClient httpClient = ().setDefaultRequestConfig(config).build(); 
		String location = null;
		int responseCode = 0;
 
		HttpResponse response;
		try {
			response = (new HttpGet(url));
			responseCode = ().getStatusCode();
			if (responseCode == 302) {
				Header locationHeader = ("Location");
				location = ();
			}
		} catch (Exception e) {
			
			();
		}
		
		return location;
	}