天天看點

JAVA核心知識點--HttpClient擷取302響應中的Location頭資訊HttpClient擷取302響應中的Location頭資訊

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

繼續閱讀