Tôi đang cố gắng tìm cách để có được các thành phần http theo một chuyển hướng nhưng không tìm thấy bất kỳ trên google vì vậy tôi đã đến đây để yêu cầu trợ giúp The func:Các thành phần HTTP cốt lõi theo hướng chuyển hướng
public String GetSite(String site, String path) throws Exception {
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[]{
// Required protocol interceptors
new RequestContent(),
new RequestTargetHost(),
// Recommended protocol interceptors
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()});
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost(site, 80);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
try {
String[] targets = {
path};
for (int i = 0; i < targets.length; i++) {
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);
if (!connStrategy.keepAlive(response, context)) {
conn.close();
} else {
}
return (EntityUtils.toString(response.getEntity()));
}
} finally {
conn.close();
}
return null;
}
Mọi trợ giúp với điều này cũng vậy? bởi vì tôi không thể tìm thấy bất cứ điều gì ...
Bạn có chắc chắn rằng bạn cần mã phức tạp này không? http://hc.apache.org/httpcomponents-client-ga/examples.html thường hoạt động. Ví dụ đầu tiên (Xử lý đáp ứng) theo mặc định chuyển hướng. – palacsint
Đã sửa lỗi bằng cách sử dụng trình kết nối url. – Cakestep
Trong trường hợp đó, bạn nên thực hiện một câu trả lời mới và chấp nhận nó. Tôi có thể giúp những người khác với cùng một vấn đề và những người khác sẽ không cố gắng giải quyết nó một lần nữa cho bạn. – palacsint