试图重定向。 302错误。 HTTP CLIENT 4.1.1

时间:2011-06-29 23:00:38

标签: java jsp httpclient http-status-code-302

Edited:- Updated One

此代码有什么问题。现在我在setRedirectStrategy上收到错误

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

和DefaultRedirectStrategy上的错误

DefaultRedirectStrategy cannot be resolved to a type

和super上的错误与上面的错误相同

DefaultRedirectStrategy cannot be resolved to a type

我正在尝试重定向,因为我总是遇到302错误。这是正确的做法。任何一个例子将不胜感激..

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


%>

2 个答案:

答案 0 :(得分:0)

我认为这很简单:你在这个代码块顶部的import语句中缺少HttpContext。在逗号分隔的org.apache.http.protocol.HttpContext列表中的某处添加import

至于这是否是正确的方法,它是什么?你能说一下你的申请吗?

答案 1 :(得分:0)

我认为您可以使用

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

如果您只想覆盖302