处理 HttpClient 重定向
2022-08-31 20:43:45
我正在将一些数据POS到正在应答302临时移动的服务器。
我希望HttpClient遵循重定向并自动获取新位置,因为我相信这是HttpClient的默认行为。但是,我收到异常,并且没有遵循重定向:(
以下是相关的代码段,任何想法都将不胜感激:
HttpParams httpParams = new BasicHttpParams();
HttpClientParams.setRedirecting(httpParams, true);
SchemeRegistry schemeRegistry = registerFactories();
ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(clientConnectionManager, httpParams)
HttpPost postRequest = new HttpPost(url);
postRequest.setHeader(HTTP.CONTENT_TYPE, contentType);
postRequest.setHeader(ACCEPT, contentType);
if (requestBodyString != null) {
postRequest.setEntity(new StringEntity(requestBodyString));
}
return httpClient.execute(postRequest, responseHandler);