-7

I am trying to post few parameters to an external url while redirecting to it. I've tried httpclient methods , they post data but do not redirect to the external url. any ideas? Here's my code: try {

        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("https://paymentsite.com/pay") {
            @Override
            public boolean getFollowRedirects() {
                System.out.println("Overrriding the HttpClient Follow Redirect switch.");
                return true;
            }
        };;
        try {
            //httpClient
            postMethod.addParameter("amt","850");
            postMethod.addParameter("pid","155");
            httpClient.executeMethod(postMethod);

            if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
                //now i want the user to be redirected to the external site. with the post parameters...

            } else {
            }
        } catch (Exception e) {
            System.out.println("Exception : " + e.toString());
            e.printStackTrace();
        }

    } catch (Exception e) {
        System.out.println("exception main: " + e.toString());
        e.printStackTrace();
    }
hotplugin
  • 37
  • 8

2 Answers2

0

Your question style is generally frowned upon. You should've given us some code and told us what you've tried however this question is similar to what I'm guessing you are looking for and I'll attach the code answer of the accepted answer

Question Here Answer from @alan geleynse

String urlParameters = "param1=a&param2=b&param3=c";
String request = "http://example.com/index.php";
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();
Community
  • 1
  • 1
gcalex5
  • 1,091
  • 2
  • 13
  • 23
  • 1
    I've updated question with my code. I want to redirect to the external site with the parameters as post from backend. Your code works for posting, but not for redirecting with the post. – hotplugin Aug 31 '14 at 05:35
0

An HTTP POST request cannot be redirected.

10.3 Redirection 3xx

This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request. The action required MAY be carried out by the user agent without interaction with the user if and only if the method used in the second request is GET or HEAD.

Reference:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216