title: Java HttpURLConnection post method not working
body: <p>I am using HttpURLConnection to send data to a server via POST. I set the headers then get the output stream and write some bytes then close the output stream.</p>
  
  <p>I am trying to get the next page for schedule details from given url. But some how i am not getting the result. Please help anybody if you know any issue in this code.</p>
  
  <p>I am getting first page with error instead of second page.
  <strong>"The station combination you have chosen is invalid. Please call the LIRR Travel Information Center at (718) 217-5477  and ask for a representative for more information."</strong></p>
  
  <pre><code>     public static void main(String[] args) throws Exception {
          String url = "http://lirr42.mta.info";
          String cookie = retrieveCookies(url);
          String urlParameters = "FromStation=56&amp;ToStation=8&amp;RequestDate=08/24/2013&amp;RequestTime=01:00&amp;RequestAMPM=PM&amp;sortBy=1&amp;schedules=schedules";
          String page = postHttpPage(url + "/index.php", urlParameters, cookie);
          System.out.println(page);
          System.out.println();
      }
  
      public static String postHttpPage(String url, String urlParameters, String cookie) throws Exception {
          System.out.println("\nSending 'POST' request to URL : " + url);
          URL obj = new URL(url);
          HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
          return postPage(conn, urlParameters, cookie);
      }
  
      private static String postPage(HttpURLConnection conn, String urlParameters, String cookie) throws Exception {
          conn.setRequestMethod("POST");
          conn.setRequestProperty("User-Agent", "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36");
          conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          conn.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes().length));
          conn.setRequestProperty("Content-Language", "en-US");
          conn.setRequestProperty("Cookie", cookie);
          //conn.setInstanceFollowRedirects(true);
          // Send post request
          //conn.setDoInput(true);
          conn.setDoOutput(true);
          DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
          wr.writeBytes(urlParameters);
          //System.out.println("wr : " + wr.size());
          wr.flush();
          wr.close();
  
          StringBuffer response = new StringBuffer();
          int responseCode = conn.getResponseCode();
          System.out.println("Post parameters : " + urlParameters);
          System.out.println("Response Code : " + responseCode);
  
          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          String inputLine;
  
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
          }
          in.close();
  
          return response.toString();
      }
  
      public static String retrieveCookies(String url) throws IOException{  
          URL obj = new URL(url);
          HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
          String cookies=conn.getHeaderField("Set-Cookie");  
          System.out.print("cookies: " + cookies);
          conn.disconnect();
          return cookies; 
      }  
  </code></pre>