该文件已移动错误

时间:2011-06-28 18:51:36

标签: java jquery httpclient httpwebresponse

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

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

var search_agile_metadata = 'https://doc.xyz.com/rest-services/services/get?id=';

var on_show_info = function() {
var outOfDomainCall = search_agile_metadata + current_doc.id;//An XML document

request_meta_info = $.ajax({
url: "proxy.jsp?url=" + outOfDomainCall,
                type: 'GET',
                success: on_get_metadata,
                error: on_get_metadata_error

        });

我的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://login.xyz.com/siteminder/cert/13890/smgetcred.scc?TYPE=16777217&amp;REALM=-SM-amp;SMAUTHREASON=0&amp;METHOD=GET&amp;SMAGENTNAME=-SM-3FfhzbcpbcT6grIIICnrPzvFIbogCZlcnVHG8&amp;TARGET</p>
<hr>
<address>Apache/2.0.58 (Unix) DAV/2 mod_jk/1.2.28 Server at doc.xyz.com Port 80</address>
</body></html>

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:2)

我不认为HTTPS会让你如此烦恼,但401 Unauthorized是关键。检查协议说的内容:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2。您的请求以某种方式需要考虑授权标头。我以前使用HttpClient库完成了这项工作:http://hc.apache.org/httpclient-3.x/authentication.html