Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open webpage in users preferred browser as a post request in Java

Tags:

java

http

post

Okay so I know how to send a HTTP POST request to a website in code, and I know how to open a url in a browser (HTTP GET). But how do open a HTTP POST in a browser.

EDIT:More Details: The website allows you to open a page to add a new entry, you can seed that page with information by POSTing that information. I have that information in my Java Desktop application, so from my Java application I want to open the add new entry page in the users preferred browser seeded with the known information.

EDIT: SO I tried Lees answer, and managed to construct a webpage with a submit button, when you click on it it goes to the final page with fields filled in, but I cant get javascript to work so that it goes there automtically without needing to press the submit button. Here is the webpage I generate

<html><head>
<script type = "text/javascript">
function onLoad() 
{
    document.getElementById('form').submit();
}
</script>
</head>
<body>
<form name="form" action="http://test.musicbrainz.org/release/add" method="post">
<input name="name" type="hidden" value="Porcupine"/>
<input type="submit" value="send">
</form>
</body>
like image 864
Paul Taylor Avatar asked Feb 07 '11 21:02

Paul Taylor


People also ask

Can browser handle post request?

It will allow you to specify the action verb (GET, POST, PUT, DELETE, etc) as well as request contents. If you're trying to test a very specific request from a link but with POST instead, then you can monitor the requests your browser is making and reissue it only with the modified POST action.

How do I make a browser request?

The browser sends an HTTP request message to the server, asking it to send a copy of the website to the client (you go to the shop and order your goods). This message, and all other data sent between the client and the server, is sent across your internet connection using TCP/IP.

What is a request in browser?

A web request can also be known as an HTTP request which is the protocol that interacts between the client and the server. A client will type in a URL address prompting the web request to the server. The client or web browser will then connect to the server that it's seeking information and data from.


2 Answers

As there is no way of POSTing via the address bar, you need to serve a page that contains a HTML form and submits that. You can try formulating a javascript: URI (like a favelet) that builds a HTML form and posts it. But I'm not 100% sure that JavaScript URI's are associated with your preferred browser by the OS. So I'm going to err on the side of "you can't".

But you can GET a page with a form on it and JavaScript that POSTs it. Providing JavaScript is enabled.

like image 77
Lee Kowalkowski Avatar answered Nov 10 '22 14:11

Lee Kowalkowski


I assume that since you are needing a "POST" you have data to send along. I feel like we don't have all of the information to answer this properly. I'm guessing you know, on the back-end, what the data is that you want posted. I'm also guessing that you don't want a user to have to submit anything manually. I can imagine that you have a user on a page, in the back-end, you have an httpclient that is posting the data for you. On your page, you could have javascript call your servlet (or other) to post the data and provide the output to a new window (or other).

like image 33
DaShaun Avatar answered Nov 10 '22 12:11

DaShaun