0
Edited:- Updated One

What's wrong with this code. Now I am getting error on setRedirectStrategy as

The method setRedirectStrategy(new DefaultRedirectStrategy(){}) is undefined for the type DefaultHttpClient

and error on DefaultRedirectStrategy as

DefaultRedirectStrategy cannot be resolved to a type

and error on super as the same as above one

DefaultRedirectStrategy cannot be resolved to a type

And I am trying to redirect as I am always getting 302 error. So is this the right way to do it. Any example will be appreciated..

<%@ page language="java" import="org.apache.http.client.methods.HttpUriRequest,org.apache.http.client.methods.HttpGet,org.apache.http.protocol.HttpContext,org.apache.http.impl.client.DefaultHttpClient,org.apache.http.HttpResponse,org.apache.http.HttpRequest,java.io.OutputStream,java.net.HttpURLConnection,java.net.URL,java.util.Collection,org.apache.commons.httpclient.Credentials,org.apache.commons.httpclient.auth.AuthenticationException,org.apache.commons.httpclient.auth.MalformedChallengeException,org.apache.commons.httpclient.params.DefaultHttpParams,org.apache.commons.httpclient.params.HttpParams,org.apache.commons.httpclient.auth.AuthScheme,org.apache.commons.httpclient.auth.AuthPolicy,org.apache.commons.httpclient.HttpClient,org.apache.commons.httpclient.UsernamePasswordCredentials,org.apache.commons.httpclient.auth.AuthScope,org.apache.commons.httpclient.methods.GetMethod,org.w3c.dom.*,javax.xml.parsers.DocumentBuilder,javax.xml.parsers.DocumentBuilderFactory,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%
String a_Url = request.getParameter( "url" ) ;

URL url = new URL (a_Url);
String encoding = new String(
         org.apache.commons.codec.binary.Base64.encodeBase64   
            (org.apache.commons.codec.binary.StringUtils.getBytesUtf8("test:test"))
          );


HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty  ("Authorization", "Basic " + encoding);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in   = 
    new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
   out.println(line);
}

%>
<%
DefaultHttpClient  httpclient = new DefaultHttpClient();
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {                
    public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
        boolean isRedirect=false;
        try {
            isRedirect = super.isRedirected(request, response, context);
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (!isRedirect) {
            int responseCode = response.getStatusLine().getStatusCode();
            if (responseCode == 301 || responseCode == 302) {
                return true;
            }
        }
        return false;
    }
});


%>
arsenal
  • 23,366
  • 85
  • 225
  • 331
  • It looks like you copy and pasted this code. What is the original problem that you're trying to solve? – jtoberon Jun 30 '11 at 03:03
  • @jtoberon, I copied this code from here [link](http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect/5046466#5046466) only some part from DefaultHttpClient.. . I am getting `302 error The document has moved` as response back from the sever from the proxy code. So that's why I am using this DefaultHttpClient code from that post. Let me know If I am doing anything wrong.. – arsenal Jun 30 '11 at 03:07

2 Answers2

0

I think you can use

final DefaultRedirectStrategy instance =new LaxRedirectStrategy();
httpClientBuilder.setRedirectStrategy(instance);

if you just want to override 302

David Buck
  • 3,752
  • 35
  • 31
  • 35
emaayan
  • 31
  • 3
0

I think it's quite simple: you're missing HttpContext in your import statement at the top of this code block. Add org.apache.http.protocol.HttpContext somewhere in the comma-separated import list.

As for whether this is the right way to do it, what's it? Can you say more about your application?

jtoberon
  • 8,706
  • 1
  • 35
  • 48
  • that got resolved... thanks.. but I am getting error on setRedirectStrategy as `Syntax error on token "setRedirectStrategy", = expected after this token` and error on isRedirected as `The method isRedirected(HttpRequest, HttpResponse, HttpContext) is undefined for the type HttpServlet`. I am always getting 302 error as The document has moved.. so thought to redirect to that url. so that's why I am trying to check the response code. – arsenal Jun 30 '11 at 02:16
  • You have an extra ! at the beginning of your scriptlet. – jtoberon Jun 30 '11 at 02:20
  • thanks for replying back, I have updated the question as the answer provided by you, now I am getting those error as I updated in the question.. – arsenal Jun 30 '11 at 02:27
  • Add `org.apache.http.impl.client.DefaultRedirectStrategy` somehwere in the comma-separated `import` list. – jtoberon Jun 30 '11 at 02:30
  • If I am adding this import than I am getting red line on this import as `The import org.apache.http.impl.client.DefaultRedirectStrategy cannot be resolved` – arsenal Jun 30 '11 at 02:32