302 Found Document has Moved error

≯℡__Kan透↙ 提交于 2021-02-05 11:29:51

问题


After reading various post from stackoverflow and some help from other guys I did authentication in my code and also trying to avoid the redirect that was occuring previously as I am new to all these things. I started getting the same error back of 302 Found, document has moved.

I am trying to make a ajax call to other domain locally from my computer by writing some proxy code in jsp. And this is my jQuery AJAX code that is calling proxy.jsp page.

    var search_agile_metadata = 'https://search.xyz.com/ag/se/ag/get?id=';

var on_show_info = function() {
                //alert("aa");
    var outOfDomainAjaxCall = search_agile + current_doc_info.id;//An XML document
    alert(outOfDomainAjaxCall);
                request_meta_info = $.ajax({
                url: "proxy.jsp?url=" + outOfDomainAjaxCall,
                type: 'GET',
                success: on_get_metadata,
                error: on_get_metadata_agile

        });

And my proxy.jsp file is:-

    <%@ page language="java" import="org.apache.http.impl.client.AbstractHttpClient,
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.net.HttpURLConnection,
java.net.URL,
java.util.Collection,
org.apache.commons.httpclient.HttpClient,
org.w3c.dom.*,
javax.xml.parsers.DocumentBuilder,
javax.xml.parsers.DocumentBuilderFactory,
java.net.*,
java.io.*,
org.apache.http.protocol.BasicHttpContext,
org.apache.http.params.BasicHttpParams,
org.apache.http.params.HttpParams,
org.apache.http.Header,
org.apache.http.client.params.HttpClientParams"
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.setFollowRedirects(false);
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);
}

%>

<%
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
%>

And when I get the response back from the server. I get this error. Any idea why I am getting this error back..

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://ln.xyz.com/site/n/13000/smscc?TYPE=16777217&amp;R">here</a>.</p>
<hr>
<address>Apache/2.0.58 (Unix) DAV/2 mod_jk/1.2.28 Server at search.xyz.com Port 80</address>
</body></html>

Any help will be appreciated..


回答1:


That is not an error. This is an informal message. The request has just been redirected. This is a 3nn response. Only HTTP 4nn and 5nn responses are errors (4nn = client error and 5nn = server error)

Your concrete problem is that you've told HttpURLConnection to not follow redirects by the following setting:

connection.setFollowRedirects(false);

So you are seeing the informal message instead of getting automatically redirected to the new request. You should rather have told it to follow redirects:

connection.setFollowRedirects(true);

Unrelated to the problem: please note that you are not fully utilizing the Apache HttpClient API here. You are just using the standard Java SE URLConnection API to make the HTTP connection. The line

HttpClientParams.setRedirecting(params, false);

has totally no influence on URLConnection behaviour. You should choose to use the one or the other. Standard URLConnection API or the Apache HttpClient.

Also please keep in mind that writing Java code in a JSP file instead of a Java class is a poor practice. You should rather be using a servlet for this job.



来源:https://stackoverflow.com/questions/6530006/302-found-document-has-moved-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!