简体   繁体   中英

Trying to Redirect. 302 Error. HTTP CLIENT 4.1.1

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


%>

I think you can use

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

if you just want to override 302

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM