302 Found Error when Trying to get the contents of url using jsp
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/rest-services/services/ag/get?id=';
var on_show_info = function() {
var outOfDomainCall = search_agile_metadata + current_doc_info.id;//An XML document
request_meta_info = $.ajax({
url: "proxy.jsp?url=" + outOfDomainCall ,
type: 'GET',
success: on_get_metadata,
error: on_get_metadata_agile
});
};
var on_get_metadata = function(data, text_status, XMLHttpRequest) {
console.log(data);
}
Any my proxy.jsp file is:-
<%@ page language="java" import="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"%>
<%
final String login ="user";
final String password ="pass";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (login, password.toCharArray());
}
});
String a_Url = request.getParameter( "url" ) ;
String l_Content = "" ;
if( a_Url!=null && a_Url.length()>0 )
l_Content = GetContent( a_Url ).toString() ;
out.println( l_Content );
%>
&开发者_如何学编程lt;%!
StringBuffer GetContent( String a_Url ) throws Exception
{
URL l_URL = new URL(a_Url);
BufferedReader l_Reader = new BufferedReader( new InputStreamReader( l_URL.openStream()));
StringBuffer l_Result = new StringBuffer("") ;
String l_InputLine = null ;
while ((l_InputLine = l_Reader.readLine()) != null)
//System.out.println("Print3" +l_InputLine );
l_Result.append( l_InputLine );
l_Reader.close();
return( l_Result ) ;
}
%>
And When I get the response back from this proxy.jsp file, I get this error:-
<!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="HTTP://search.xyz.com/rest-services/se/agile/get?id=CD90">here</a>.</p></body></html>
And if I check the status code in that above jQuery AJAX code then it is 200. So that means something is happening in the proxy.jsp page. Why it is not able to get the contents of url. Any suggestions will be appreciated...
A status code of 302 means the file you're trying to pull information from has moved. 302 is a temporary redirect you can see the definition here: HTTP status code definitions
Your ajax call is fine but your JSP code doesn't follow the 302 redirect. I'm not sure if it will fix the problem but it would be worth looking into using apache's HttpClient code in your JSP.
Hope this helps.
Code snippet of what I've done with HttpClient
httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, AuthPolicy.BASIC);
Credentials creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, creds);
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, credsProvider.getCredentials(AuthScope.ANY));
精彩评论