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


%>
4

2 回答 2

0

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

至于这是否是正确的做法,它是什么?你能多谈谈你的申请吗?

于 2011-06-30T02:12:46.160 回答
0

我认为你可以使用

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

如果您只想覆盖 302

于 2020-04-19T14:16:03.613 回答