title: After login using java code to website server sends response code 500
body: <p>I want to make java program to download a file from website, but i'm stuck in following situation. When I run the following code logging to page gets done successfully, but after it is done, when I send request for the next page, it shows response code 500.</p>
  
  <pre><code>public class Ss2mydb
  {
      private List&lt;String&gt; cookies;
      private HttpURLConnection conn;
  
      private final String USER_AGENT = "Mozilla/5.0";
  
      public static void main(String[] args) throws Exception
      {
          String indexPage = "http://10.100.100.142/index.asp";
          String validatePage = "http://10.100.100.142/validate.asp";
          String ccmenuPage = "http://10.100.100.142/callcentre/ccmenu.asp";
      String reportPage = "http://10.100.100.142/topmgmt/reports/PROJECTVIJAY/get_download_cafs.asp";
          String reportPageDownload = "http://10.100.100.142/topmgmt/reports/PROJECTVIJAY/get_download_cafs.asp?view=N";
  
      Ss2mydb http = new Ss2mydb();
  
      // make sure cookies is turn on
      CookieHandler.setDefault(new CookieManager());
  
      // 1. Send a "GET" request, so that you can extract the form's data.
      String page = http.GetPageContent(indexPage);
      String postParams = http.getFormParams(page, "username", "password");
  
  
      // 2. Construct above post's content and then send a POST request for
      // authentication
      http.sendPost(validatePage, postParams);
  //        System.exit(0);
  
      // 3. success then go to gmail.
      http.GetPageContent2(ccmenuPage);
          String result = http.GetPageContent2(reportPage);
      System.out.println(result);
      }
  
      private void sendPost(String url, String postParams) throws Exception {
  
      URL obj = new URL(url);
      conn = (HttpURLConnection) obj.openConnection();
  
      // Acts like a browser
      conn.setUseCaches(false);
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Host", "10.100.100.142");
      conn.setRequestProperty("User-Agent", USER_AGENT);
      conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
      conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
      for (String cookie : this.cookies) {
              System.out.println(" sendPost : "+cookie.split(";", 1)[0]);
          conn.addRequestProperty("Cookie",cookie.split(";", 1)[0]);
      }
      conn.setRequestProperty("Connection", "keep-alive");
      conn.setRequestProperty("Referer", "http://10.100.100.142/index.asp");
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
  
      conn.setDoOutput(true);
      conn.setDoInput(true);
  
      // Send post request
      DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
      wr.writeBytes(postParams);
      wr.flush();
      wr.close();
  
      int responseCode = conn.getResponseCode();
      System.out.println("\nSending 'POST' request to URL : " + url);
      System.out.println("Post parameters : " + postParams);
      System.out.println("Response Code : " + responseCode);
  
  //        setCookies(conn.getHeaderFields().get("Set-Cookie"));
  
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
      // System.out.println(response.toString());
  
    }
  
      private String GetPageContent2(String url) throws Exception {
  
      URL obj = new URL(url);
      conn = (HttpURLConnection) obj.openConnection();
  
      // default is GET
      conn.setRequestMethod("GET");
  
      conn.setUseCaches(false);
  
      // act like a browser
      conn.setRequestProperty("User-Agent", USER_AGENT);
      conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
      conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
          System.out.println(" L : "+cookies);
      if (cookies != null) {
          for (String cookie : this.cookies) {
                      System.out.println(" GetPageContent : "+cookie.split(";", 1)[0]);
              conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
          }
      }
      int responseCode = conn.getResponseCode();
      System.out.println("\nSending 'GET' request to URL : " + url);
      System.out.println("Response Code : " + responseCode);
  
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
  
      // Get the response cookies
  //        System.out.println(" Get : "+conn.getHeaderFields().get("Set-Cookie"));
  //  setCookies(conn.getHeaderFields().get("Set-Cookie"));
  
      return response.toString();
  
    }
  
    private String GetPageContent(String url) throws Exception {
  
      URL obj = new URL(url);
      conn = (HttpURLConnection) obj.openConnection();
  
      // default is GET
      conn.setRequestMethod("GET");
  
      conn.setUseCaches(false);
  
      // act like a browser
      conn.setRequestProperty("User-Agent", USER_AGENT);
      conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
      conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
          System.out.println(" L : "+cookies);
      if (cookies != null) {
          for (String cookie : this.cookies) {
                      System.out.println(" GetPageContent : "+cookie.split(";", 1)[0]);
              conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
          }
      }
      int responseCode = conn.getResponseCode();
      System.out.println("\nSending 'GET' request to URL : " + url);
      System.out.println("Response Code : " + responseCode);
  
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
  
      // Get the response cookies
          System.out.println(" Get : "+conn.getHeaderFields().get("Set-Cookie"));
      setCookies(conn.getHeaderFields().get("Set-Cookie"));
  
      return response.toString();
  
    }
  
    public String getFormParams(String html, String username, String password)
          throws UnsupportedEncodingException {
  
      System.out.println("Extracting form's data...");
  
      Document doc = Jsoup.parse(html);
  
      // Google form id
      Element loginform = doc.getElementById("right");
      Elements inputElements = loginform.getElementsByTag("input");
      List&lt;String&gt; paramList = new ArrayList&lt;String&gt;();
      for (Element inputElement : inputElements) {
          String key = inputElement.attr("name");
          String value = inputElement.attr("value");
  
          if (key.equals("USERname"))
              value = username;
          else if (key.equals("password"))
              value = password;
                  if(!key.equals(""))
                      paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
      }
  
      // build parameters list
      StringBuilder result = new StringBuilder();
      for (String param : paramList) {
          if (result.length() == 0) {
              result.append(param);
          } else {
              result.append("&amp;" + param);
          }
      }
      return result.toString();
    }
  
    public List&lt;String&gt; getCookies() {
      return cookies;
    }
  
    public void setCookies(List&lt;String&gt; cookies) {
      this.cookies = cookies;
    }
  }
  </code></pre>