title: Internal server Error 500 when I try to automate login to facebook in java
body: <p>I am largely following this :</p>
  
  <p><a href="http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/" rel="nofollow">http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/</a></p>
  
  <p>I am able to get the form details. I even constructed the params. Then when I post the request, I get internal server error 500!</p>
  
  <p>Whats wrong ? here is my code :</p>
  
  <pre><code>public class Abc {
  private List&lt;String&gt; cookies;
  private HttpsURLConnection conn;
  private final String USER_AGENT = "Mozilla/5.0";
  
    public static void main(String[] args) {
   String proxyHost="43.88.65.10";
   String proxyPort="8080";
  
      System.out.println("Setting proxy....");
      System.setProperty("http.proxyHost",proxyHost) ;  
  
      System.setProperty("https.proxyHost",proxyHost) ;  
      System.setProperty("https.proxyPort",proxyPort) ;
      System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1|43.88.102.142");
      System.setProperty("https.nonProxyHosts", "localhost|127.0.0.1|43.88.102.142");
      System.out.println("Proxy Set.");
  
      String url = "https://graph.facebook.com/oauth/authorize?client_id=XXXXXXXXXX&amp;redirect_uri=http://localhost:9091&amp;scope=read_stream";
  
      String fb = "https://www.facebook.com/login.php?login_attempt=1&amp;next=http%3A%2F%2Fwww.facebook.com%2Fdialog%2Foauth%3F&amp;redirect_uri=http://localhost:9091/test&amp;scope=publish_stream&amp;client_id=XXXXXXXXXXXX&amp;ret=login";
  
       Abc tryObject = new Abc();
  
       CookieHandler.setDefault(new CookieManager());
  
      // 1. Send a "GET" request, so that you can extract the form's data.
          String page = null;
          try {
              page = tryObject.GetPageContent(url);
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          String postParams = null;
          try {
              postParams = tryObject.getFormParams(page, "xxxxxxx@gmail.com", "xxxxx");
          } catch (UnsupportedEncodingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          System.out.println("postParams"+postParams);
  
          //postParams = "lsd=AVoao3Js&amp;api_key=XXXXXXXXXXXXX&amp;display=page&amp;enable_profile_selector=&amp;legacy_return=1&amp;profile_selector_ids=&amp;skip_api_login=1&amp;signed_next=1&amp;trynum=1&amp;timezone=&amp;lgnrnd=044127_BBYN&amp;lgnjs=n&amp;email=XXXXXXXXXXX%40gmail.com&amp;pass=XXXXXXXX&amp;persistent=1&amp;default_persistent=0&amp;login=Log+In";
  
  
          // 2. Construct above post's content and then send a POST request for
          // authentication
          //tryObject.sendPost(url, postParams);
          try {
              tryObject.sendPost(fb, postParams);
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
    }//end of main
  
  
  
  
    private String GetPageContent(String url) throws Exception {
  
          URL obj = new URL(url);
          conn = (HttpsURLConnection) 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,*/*;q=0.8");
          conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
          if (cookies != null) {
              for (String cookie : this.cookies) {
                  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
          setCookies(conn.getHeaderFields().get("Set-Cookie"));
  
          return response.toString();
  
        }
  
    public void setCookies(List&lt;String&gt; cookies) {
          this.cookies = cookies;
        }
  
  
    public String getFormParams(String html, String username, String password)
              throws UnsupportedEncodingException {
  
          System.out.println("Extracting form's data...");
  
          Document doc = Jsoup.parse(html);
          System.out.println("doc--&gt;"+doc);
  
          //FB form id
          Element loginform = doc.getElementById("loginform");
          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.equalsIgnoreCase("Email"))
                  value = username;
              else if (key.equalsIgnoreCase("Pass"))
                  value = password;
              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();
        }
  
  
    private void sendPost(String url, String postParams) throws Exception {
  
          URL obj = new URL(url);
          conn = (HttpsURLConnection) obj.openConnection();
  
          // Acts like a browser
          conn.setUseCaches(false);
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Host", "www.facebook.com");
          conn.setRequestProperty("User-Agent", USER_AGENT);
          conn.setRequestProperty("Accept",
              "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
          conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
          for (String cookie : this.cookies) {
              conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
          }
          conn.setRequestProperty("Connection", "keep-alive");
          conn.setRequestProperty("Referer", "https://www.facebook.com/");
          conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  
          System.out.println("Length--&gt;"+postParams.length());
  
          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);
  
          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());
  </code></pre>
  
  <p>}
  }</p>
  
  <p>The error I get is 
  Response Code : 500
  java.io.IOException: Server returned HTTP response code: 500 for URL: <a href="https://www.facebook.com/login.php?login_attempt=1&amp;next=http%3A%2F%2Fwww.facebook.com%2Fdialog%2Foauth%3F&amp;redirect_uri=http://localhost:9091/test&amp;scope=publish_stream&amp;client_id=XXXXXXXXXX&amp;ret=login" rel="nofollow">https://www.facebook.com/login.php?login_attempt=1&amp;next=http%3A%2F%2Fwww.facebook.com%2Fdialog%2Foauth%3F&amp;redirect_uri=http://localhost:9091/test&amp;scope=publish_stream&amp;client_id=XXXXXXXXXX&amp;ret=login</a>
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
      at facebook.Abc.sendPost(Abc.java:216)
      at facebook.Abc.main(Abc.java:78)
  Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: <a href="https://www.facebook.com/login.php?login_attempt=1&amp;next=http%3A%2F%2Fwww.facebook.com%2Fdialog%2Foauth%3F&amp;redirect_uri=http://localhost:9091/test&amp;scope=publish_stream&amp;client_id=XXXXXXXXX&amp;ret=login" rel="nofollow">https://www.facebook.com/login.php?login_attempt=1&amp;next=http%3A%2F%2Fwww.facebook.com%2Fdialog%2Foauth%3F&amp;redirect_uri=http://localhost:9091/test&amp;scope=publish_stream&amp;client_id=XXXXXXXXX&amp;ret=login</a>
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
      at facebook.Abc.sendPost(Abc.java:210)
      ... 1 more</p>