Skip to Content
author's profile photo
Former Member

URL Exception

Hi,

When i am trying to read a URL i am getting follwoing exception.

"ERRORjava.net.ProtocolException: Server redirected too many times (20)"

Code :

URL resutlsURL = new URL(file);

BufferedReader in = new BufferedReader( new InputStreamReader(resutlsURL.openStream()));

BufferedWriter out=new BufferedWriter(new FileWriter("c:/folder.xml"));

String inputLine;

while ((inputLine = in.readLine()) != null)

{

out.write(inputLine );

}

in.close();

out.close();

Where file http://dptest:3080/WebAS/AUTOTEST/630_SP_TEST_2K/index.xml

Please let me know the solution.

Thanks & Regards

Anilkumar

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Aug 20, 2004 at 01:24 PM

    Hi Anilkumar,

    the code should work; the problem seems to be on the server, <i>it</i> seems to redirect.

    Can you call the xml with your browser?

    Maybe you can test this:

    URL url = new URL(file);

    HttpURLConnection.setFollowRedirects(false);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    System.out.println("Response code = " + connection.getResponseCode());

    String header = connection.getHeaderField("location");

    if (header != null) System.out.println("Redirected to " + header);

    Hope this helps

    Detlev

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Aug 20, 2004 at 03:08 PM

    Hi Detlev ,

    Thanx for your reply.

    I have tried the code sent by you.

    Its printing Responsecode as 401 and returning Null value for header.

    I can able to open that XML though Internet Explorer.

    I am using JDK1.4.2. Is there any dependency ??

    Thanks & Regards

    Anilkumar

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Anilkumar,

      401 is "Unauthorized". I don't know how the request is handled at your server, but there must be some authorization action. The "normal" behaviour for a 401 response is that the client may repeat the request with a suitable Authorization header field. Maybe (I'm not sure) Java in this case in fact tries to start the request again (but in fact without authorization). Maybe (again) this is counted internally as a redirect. If all this is the case, it would be obvious that this happens "infinitely" - here up to 20 times, than Java says "enough is enough".

      So the next try should be:

      a) Disable the Authorization action at server side.

      OR

      b) Add an Authorization header field (this would mean to open a regular HttpURLConnection as shown in the testing example).

      Hope it helps

      Detlev

  • author's profile photo
    Former Member
    Aug 20, 2004 at 03:36 PM

    Hi,

    Please let me know how to do these setting in SAP J2EE engine.

    Regards

    Anilkumar

    Add comment
    10|10000 characters needed characters exceeded