Edited:- Updated One
このコードで何が問題なのですか。今、私は1つリダイレクトしようとしています。 302エラー。 HTTPクライアント4.1.1
DefaultRedirectStrategy cannot be resolved to a type
そして、私がしようとしています上記と同様に
DefaultRedirectStrategy cannot be resolved to a type
やスーパーでのエラーなど
The method setRedirectStrategy(new DefaultRedirectStrategy(){}) is undefined for the type DefaultHttpClient
とDefaultRedirectStrategy上のエラーとしてsetRedirectStrategy上のエラーを取得しています私は常に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;
}
});
%>
このコードをコピーして貼り付けたようです。あなたが解決しようとしている元の問題は何ですか? – jtoberon
@jtoberon、私はここからこのコードをコピーしました。[リンク](http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect/5046466#5046466)DefaultHttpClientの一部だけです。 。 。私は '302エラーを取得しています。ドキュメントは、プロキシコードからサーバーからの応答として戻ってきました。そのため、私はこの投稿からこのDefaultHttpClientコードを使用しています。私が何か間違っていたら... – ferhan