作者:手机用户2502852037 | 来源:互联网 | 2023-05-16 13:42
Edited:-UpdatedOneWhatswrongwiththiscode.NowIamgettingerroronsetRedirectStrategyas
Edited:- Updated One
What's wrong with this code. Now I am getting error on setRedirectStrategy as
这段代码出了什么问题。现在我在setRedirectStrategy上遇到错误
The method setRedirectStrategy(new DefaultRedirectStrategy(){}) is undefined for the type DefaultHttpClient
对于DefaultHttpClient类型,方法setRedirectStrategy(new DefaultRedirectStrategy(){})是未定义的
and error on DefaultRedirectStrategy as
和DefaultRedirectStrategy上的错误
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..
而我正在尝试重定向,因为我总是得到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;
}
});
%>
1 个解决方案