Results 1 to 2 of 2
  1. #1
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Getting java.net.ProtocolException: Server redirected too many times (20)

    Hi Im trying to connect a url and get the content of a json file . but im getting below exception


    I/O Error: Server redirected too many times (20)
    java.net.ProtocolException: Server redirected too many times (20)
    at sun.net.http://www.protocol.http.HttpURLConn...Stream(Unknown Source)
    at com.carefirst.convertXml.connectUrl.main(connectUr l.java:50)


    Any idea why this is happenning? Below is my code .

    Java Code:
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.Authenticator;
    import java.net.HttpURLConnection;
    import java.net.InetAddress;
    import java.net.MalformedURLException;
    import java.net.PasswordAuthentication;
    import java.net.URL;
    import java.net.URLConnection;
     
     
     
    public class connectUrl {
    	public static void main(String[] args) {
     
    		try {
     
    			// Sets the authenticator that will be used by the networking code
     
    			// when a proxy or an HTTP server asks for authentication.
    			InputStream is = null;
    			StringBuilder parsedContentFromUrl = new StringBuilder();
    			Authenticator.setDefault(new CustomAuthenticator());
    			URLConnection uc;
    			HttpURLConnection urlConnection;
    			String urlString="http://TestServer:8081/tasks/admin/agent/AgentTasks/configuredAgentList?sort=Status&dir=asc&startIndex=0&results=25";
    			//URLConnection conn = new URL("http://sv-udeploy-d1:8081/tasks/admin/agent/AgentTasks/configuredAgentList?sort=Status&dir=asc&startIndex=0&results=25").openConnection();
    			//URL url = new URL("http://sv-udeploy-d1:8081/tasks/admin/agent/AgentTasks/configuredAgentList?sort=Status&dir=asc&startIndex=0&results=25");
    			URL url = new URL(urlString);
    			//uc = url.openConnection();
    			/*uc.addRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    			uc.setRequestProperty("User-Agent", "Mozilla/5.0");
    			uc.connect();
    			uc.getInputStream();*/
     
    			urlConnection = (HttpURLConnection) url.openConnection();
    			urlConnection.setRequestMethod("POST");
    			urlConnection.setDoInput(true);
    			urlConnection.setDoOutput(true);
    			urlConnection.setUseCaches(false);
    			urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
     
     
     
    			BufferedInputStream in1 = new BufferedInputStream(urlConnection.getInputStream());
    			int ch;
    			while ((ch = in1.read()) != -1) {
    			    parsedContentFromUrl.append((char) ch);
    			}
    			System.out.println(parsedContentFromUrl);
     
    			// read text returned by server
    			System.out.println("connected successully");
    			System.out.println(url);
     
    			System.setProperty("javax.net.debug","all");
    			url.openStream();
    			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
     
    			String line;
    			while ((line = in.readLine()) != null) {
    				System.out.println(line);
    			}
    			in.close();
    		} catch (MalformedURLException e) {
    			System.out.println("Malformed URL: " + e.getMessage());
    		} catch (IOException i) {
    			System.out.println("I/O Error: " + i.getMessage());
    			i.printStackTrace();
    		}
    	}
     
    	public static class CustomAuthenticator extends Authenticator {
    		// Called when password authorization is needed
    		protected PasswordAuthentication getPasswordAuthentication() {
    			// Get information about the request
    			String prompt = getRequestingPrompt();
    			String hostname = getRequestingHost();
    			InetAddress ipaddr = getRequestingSite();
    			int port = getRequestingPort();
    			String username = "admin";
    			String password = "admin";
    			// Return the information (a data holder that is used by
    			// Authenticator)
    			return new PasswordAuthentication(username, password.toCharArray());
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Getting java.net.ProtocolException: Server redirected too many times (20)

    Moved from New to Java to Networking.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 10
    Last Post: 05-17-2014, 07:50 PM
  2. I keep getting redirected to MyFilestore.com
    By kjkrum in forum Suggestions & Feedback
    Replies: 3
    Last Post: 04-16-2013, 06:14 PM
  3. Replies: 6
    Last Post: 12-25-2011, 11:33 PM
  4. Replies: 8
    Last Post: 03-12-2010, 04:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •