302 发现文档已移动错误

发布于 2024-11-18 02:10:15 字数 2975 浏览 2 评论 0原文

在阅读了 stackoverflow 上的各种帖子以及其他人的一些帮助后,我在代码中进行了身份验证,并试图避免之前发生的重定向,因为我对所有这些事情都是新手。我开始收到相同的错误 302 Found,文档已移动。

我试图通过在 jsp 中编写一些代理代码来从我的计算机本地对其他域进行 ajax 调用。这是我调用 proxy.jsp 页面的 jQuery AJAX 代码。

    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

        });

我的 proxy.jsp 文件是:-

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

当我从服务器收到响应时。我收到这个错误。知道为什么我会收到此错误。

<!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>

任何帮助将不胜感激。

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&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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2024-11-25 02:10:15

这不是一个错误。这是一条非正式消息。该请求刚刚被重定向。这是 3nn 响应。只有 HTTP 4nn 和 5nn 响应是错误(4nn = 客户端错误,5nn = 服务器错误)

您的具体问题是您已告诉 HttpURLConnection 通过以下方式遵循重定向设置:

connection.setFollowRedirects(false);

因此您看到的是非正式消息,而不是自动重定向到新请求。您应该告诉它遵循重定向:

connection.setFollowRedirects(true);

与问题无关:请注意,您在这里没有充分利用 Apache HttpClient API。您只需使用标准 Java SE URLConnection API 来建立 HTTP 连接。该行

HttpClientParams.setRedirecting(params, false);

URLConnection 行为完全没有影响。您应该选择使用其中之一。标准 URLConnection API Apache HttpClient。

另请记住,在 JSP 文件而不是 Java 类中编写 Java 代码是一种不好的做法。您应该使用 servlet 来完成这项工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文