Just so that it is clear I think I need to explain what I am trying to do a bit better.
I need to redirect the user’s browser from the server they are on to a new server that runs my vaadin application.
At the moment I am using the following
This works just fine and the user is now on the new server and I have passed in the data and the called application retrieves it.
I want to do exactly the same thing except I don’t want to pass the data params on the URL.
I need to re-direct the users browser to the new application on a different server but I want to pass a JSON structure in the body of the request.
I can use httpClient to create the POST request and I get a response from the Vaadin application but how do I trigger the equivalent of the “window.open” piece so that the user browser re-directed to the new site as well and it is set up to engage with the vaadin application on the new server.
doItButton.addClickListener(e-> {
makePostRequest();
// UI.getCurrent().getPage().executeJs("window.open(\"" + LINK + "\", \"_self\");");
});
/**
* Create the post body.
* @return the the post body string
*/
private String createPostBody() {
final JsonObject l_payRequest = Json.createObjectBuilder()
.add("bu", "M1")
.add("cmt", "ABC")
.build();
MLOGGER.debug("Request: " + l_payRequest.toString());
return l_payRequest.toString();
}
/**
* makePostRequest.
* @return boolean 200 OK = true else false
*/
public final boolean makePostRequest() {
int l_iHTTPResponse = 0;
try {
final HttpClient l_httpclient = HttpClientBuilder.create().build();
final HttpPost l_httppost = new HttpPost("http://wc.sybernet.ie:8075/myapp-1.0.0/app");
final StringEntity l_postbody = new StringEntity(createPostBody());
l_postbody.setContentType("application/json");
l_httppost.setEntity(l_postbody);
//Execute and get the response.
final HttpResponse response = l_httpclient.execute(l_httppost);
MLOGGER.info("HTTP response " + response.getStatusLine().getStatusCode());
l_iHTTPResponse = response.getStatusLine().getStatusCode();
if (l_iHTTPResponse == HttpURLConnection.HTTP_OK) {
final String l_sResponse = EntityUtils.toString(response.getEntity());
// System.out.println("response: " + l_sResponse);
} else {
throw new Exception("makePayment(): Did not get 200 back from executeMethod");
}
} catch (Exception e) {
MLOGGER.error("MakeSSLPostRequest() Exception : " + e.getMessage());
}
return (l_iHTTPResponse == HttpURLConnection.HTTP_OK);
}
Oh thats a very different situation that was originally asked. Using a Client here will not help you. But neither will setting the location of the page, as that will not make a POST request.
I can only think of one way to navigate in a browser with a POST request and that is [submitting a form]
(https://stackoverflow.com/q/1651197/3441504). I think it should be possible to add hidden input fields in the form holding the values you want to post, and make your button the submit button of the form.