0

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 );
%>
<%!
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...

arsenal
  • 23,366
  • 85
  • 225
  • 331

1 Answers1

1

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));
kasdega
  • 18,396
  • 12
  • 45
  • 89
  • @kasdega, thanks for replying back.. I am not sure what does these line means in Basic Authentication for HttpClient: `client.getState().setCredentials(new AuthScope("www.verisign.com", 443, "realm"), new UsernamePasswordCredentials("username", "password") );` like what I should write in place of www.verisign.com and what I write in place of 443 and what is realm, can you help me out in this.. – arsenal Jun 29 '11 at 19:15
  • HttpClient Api is extremely well documented. I encourage you to read through those docs. I'm not sure what you're trying to do with the new AuthScope exactly. In your case you can probably get away with using the static AuthScope.ANY. – kasdega Jun 29 '11 at 19:51
  • @kasdega, thanks for replying back. But still m getting the same error. Any other suggestions. – arsenal Jun 29 '11 at 19:54
  • One thing to note, in order to use HttpClient with a site that uses basic authentication we had to explicitly switch the Authorization preferences. I added a code snippet to show what I've done in the past. – kasdega Jun 29 '11 at 19:56
  • http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect – kasdega Jun 29 '11 at 21:18
  • @kasdega, seriously telling m not able to use these code in my code.. If you can help me out in thsi than that will be of great help for me. As few of them are still giving the same error. – arsenal Jun 29 '11 at 21:26