Kotlin
Kotlin

Reputation: 23

redirect and POST method

How do I POST method and redirect the parameters to the link ؟ Is there such a possibility?

link = http://92.42.51.91/CGGateway/Default.aspx

I currently send information and I have no problem But I can not redirect

When I send the information correctly, I can use the link

    URL url = new URL("http://62.68.645.32/Default.aspx");
    Map<String, Object> params = new LinkedHashMap<String, Object>();
    params.put("Timestamp", Timestamp);
    params.put("Callback", "google.com");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");

    resp.sendRedirect(Link);

Upvotes: 0

Views: 10160

Answers (2)

Roman Puchkovskiy
Roman Puchkovskiy

Reputation: 11865

Usually, when people say 'redirect', they mean HTTP code 302 redirection. Many implementations of 302 code in http clients make a redirection with GET only, regardless of the original request method, so it is not reliable to 'redirect' to a POST endpoint using this code.

A simple way to overcome this is a self-posting form:

<form name="autoform" action="..." method="POST">
    <input type="hidden" name="param1" value="value1">
    <input type="hidden" name="param2" value="value2">
    ...
</form>

And make it submit automatically in user's browser:

<script type="text/javascript">
    document.autoform.submit();
</script>

Another way is using code 307 which must preserve the original HTTP method:

response.setHeader("Location", response.encodeRedirectURL(url));
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);

In such a case you will not be able to control the exact parameters the browser sends to that url; instead, the browser will automatically send the same parameters it sent to your redirecting URL.

Upvotes: 2

Maurice Perry
Maurice Perry

Reputation: 9658

Not sure I understand you fully, however, I've written a class to build a query string that I can use for a get or a post:

public class QueryBuilder {
    private final String encoding;
    private final StringBuilder buf = new StringBuilder();

    public QueryBuilder(String encoding) {
        this.encoding = encoding;
    }

    public boolean isEmpty() {
        return buf.length() == 0;
    }

    public void add(String name, Object value) {
        try {
            if (value != null) {
                if (buf.length() > 0) {
                    buf.append('&');
                }
                buf.append(name);
                buf.append('=');
                buf.append(URLEncoder.encode(value.toString(), encoding));
            }
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("Unsupported encoding " + encoding);
        }
    }

    public String toURL(String url) {
        if (buf.length() == 0) {
            return url;
        } else {
            return url + '?' + buf.toString();
        }
    }

    @Override
    public String toString() {
        return buf.toString();
    }
}

To pass the parameters as a POST, you would do:

QueryBuilder query = new QueryBuilder("UTF-8");
query.add("Timestamp", Timestamp);
query.add("Callback", "google.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = cnt.getOutputStream();
try {
    out.write(query.toString().getBytes("UTF-8"));
} finally {
    out.close();
}
int stat = conn.getResponseCode();
if (stat < 200 || stat >= 300) {
    throw new IOException("HTTP Error " + stat + ": "
            + conn.getResponseMessage());
}
...

Upvotes: 0

Related Questions