The Document has moved error
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://doc.xyz.com/rest-se开发者_运维知识库rvices/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
});
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://login.xyz.com/siteminder/cert/13890/smgetcred.scc?TYPE=16777217&REALM=-SM-amp;SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-3FfhzbcpbcT6grIIICnrPzvFIbogCZlcnVHG8&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>
Any help will be appreciated..
I don't think it is HTTPS that is messing you up so much, but the 401 Unauthorized is the key. Check what the protocol says: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 . Your request somehow needs to account for an Authorization header. I've done this in the past with the HttpClient library: http://hc.apache.org/httpclient-3.x/authentication.html
精彩评论