title: How can I login to a website using Java and stay logged in?
body: <p>So what i am trying to do is log into a a web app that is used by our company, I need to download multiple graphs(45 to be exact) I have a program that will do this exactly how i want it to, However the way my code works it has to log in each time. I'm not sure if this is a problem( Might look suspicious to the admin of the web app) or not but it seems a little inefficient. Ideally I would like to log into the site once and them move to whatever Urls I need to download the images. Any help you guys could offer would be great.</p>
  
  <pre><code>for (int i = 1; i &lt;= 45; i++) {
                  URL url;
                  if(i&lt;10) {
                      url = new URL("http://127.0.0.1:3333/website/image0"+i);
  
                  }
  
                  else{
                      url = new URL("http://127.0.0.1:3333/website/image"+i);
  
                  }
                  String loginPassword = "usrName" + ":" + "PassWrd";
                  String encoded = new sun.misc.BASE64Encoder().encode(loginPassword.getBytes());
                  URLConnection conn = url.openConnection();
                  conn.setRequestProperty("Authorization", "Basic " + encoded);
  
  
                  String destName = "C:\\Users\\Name\\Documents\\Report\\p"+i+".png";
                  InputStream in = new BufferedInputStream(conn.getInputStream());
                  OutputStream os = new FileOutputStream(destName);
  
                  byte[] b = new byte[2048];
                  int length;
  
                  while ((length = in.read(b)) != -1) {
                      os.write(b, 0, length);
                  }
  
                  in.close();
                  os.close();
              }
  </code></pre>