Idunk
Idunk

Reputation: 90

Redirect URL type 301 in Java

I learn to know where actually link redirect from an URL. After testinf on redirect URL web site, it give url redirect type 301. So, I test based on link below to get real link. Get hold of redirect url with Java org.apache.http.client

Code looks like below:

HttpGet httpget = new HttpGet(filename);
HttpContext context = new BasicHttpContext(); 
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute((HttpUriRequest) httpget, context); 
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
    throw new IOException(response.getStatusLine().toString());
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
HttpHost currentHost = (HttpHost)  context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
String currentUrl = currentHost.toURI() + currentReq.getURI();
System.out.println(currentUrl);

but I got this message:

The method execute(HttpUriRequest, HttpContext) in the type AbstractHttpClient is not >applicable for the arguments (HttpGet, HttpContext)

Would some body help me, what's wrong in this code?

Upvotes: 1

Views: 1008

Answers (1)

palacsint
palacsint

Reputation: 28845

Your code works well for me with this httpclient dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.1.2</version>
</dependency>

and with these imports:

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

Check that you are using the correct dependencies.

Upvotes: 1

Related Questions