title: Post and redirect to external url java servlets
body: <p>I am making post request to URL that is not in my application- external URL. I would like to do it using java servlet. Here is the code I am using</p>
  
  <pre><code>            String urlParameters =
              "PaymentOKURL=" + URLEncoder.encode("http://test.com", "UTF-8")
              + "&amp;PaymentFailURL=" + URLEncoder.encode("https://test.com", "UTF-8")
              + "&amp;AmountToPay=" + URLEncoder.encode("170000", "UTF-8")
              + "&amp;AmountCurrency=" + URLEncoder.encode("EUR", "UTF-8")
              + "&amp;PayToMerchant=" + URLEncoder.encode("1000000", "UTF-8")
              + "&amp;Details1=" + URLEncoder.encode("Invoice 1000", "UTF-8")
              + "&amp;Details2=" + URLEncoder.encode("123456789", "UTF-8")
              + "&amp;MerchantName=" + URLEncoder.encode("TEST", "UTF-8")
              + "&amp;CheckSumHeader=" +URLEncoder.encode("08PaymentOKURL,PaymentFailURL,AmountToPay,AmountCurrency,PayToMerchant,Details1,Details2,MerchantName,0303030303003", "UTF-8")
              + "&amp;CheckSum=" + URLEncoder.encode("7d4192f39c50d8d87863005fbc44515d", "UTF-8");
  
  
      URL url = null;
      String testUrl="https://redirectApp.com";
      try {
          url = new URL(testUrl);
      } catch (MalformedURLException ex) {
          Logger.getLogger(TextServlet.class.getName()).log(Level.SEVERE, null, ex);
      }
      HttpsURLConnection httpsCon = null;
      InputStream is = null;
      try {
          httpsCon = (HttpsURLConnection) url.openConnection();
          httpsCon.setHostnameVerifier(new HostnameVerifier() {
  
              public boolean verify(String hostname, SSLSession session) {
                  return true;
              }
          });
          httpsCon.setRequestMethod("POST");
          System.out.println("execute  httpsCon.setRequestMethod(\"POST\");");
          httpsCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          System.out.println("execute  httpsCon.setRequestProperty(\"Content-Type\", \"application/x-www-form-urlencoded\");");
          httpsCon.setRequestProperty("Content-Length", String.valueOf(urlParametersEtax.getBytes().length));
          System.out.println("execute  Content-Length");
          httpsCon.setRequestProperty("Accept-Charset", "UTF-8");
          System.out.println("execute Accept-Charset");
          httpsCon.setDoOutput(true);
          System.out.println("execute  httpsCon.setDoOutput(true);");
          httpsCon.setInstanceFollowRedirects(true);  //you still need to handle redirect manully.
          HttpsURLConnection.setFollowRedirects(true);
          // 
  
          //add reuqest header
  
          OutputStream os = httpsCon.getOutputStream();
          System.out.println("execute   OutputStream os = con.getOutputStream();");
          os.write(urlParameters.getBytes());
          System.out.println("execute   os.write(urlParameters.getBytes());");
          httpsCon.connect();
          System.out.println("execute  httpsCon.connect();");
          int code = httpsCon.getResponseCode();
          System.out.println("RESPONSE CODE " + code);
      } catch (IOException ex) {
          Logger.getLogger(TextServlet.class.getName()).log(Level.SEVERE, null, ex);
      }
  </code></pre>
  
  <p>I got http status 200, but I like to redirect user using this params into external application.
  I like to do this using servlet.</p>