How to programmatically send a POST request to a JSF page without using an HTML form?

I have a very simple JSF bean as shown below:

import org.jboss.seam.annotations.Name;

@Name(Sample.NAME)
public class Sample {

    public static final String NAME="df";

    private String text = "text-test";

    public void sampleM(){
        System.out.println("Test: "+text);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

And the JSF form associated with this component:

<h:form id="sampleForm">
        <h:commandButton id="sampleButton" action="#{df.sampleM()}" value="ok" />
</h:form>

Now I would like to programmatically submit a POST request to this form.

According to my research, the key here are the POST parameters. Choosing the right ones gives the correct results (String "Test: text-test" is printed on the server console).

So the question is: How should I choose the POST data correctly?

The JSF form shown above creates this HTML form:

<form id="sampleForm" name="sampleForm" method="post" action="/pages/main/main.smnet" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="sampleForm" value="sampleForm" />
    <input id="sampleForm:sampleButton" type="submit" name="sampleForm:sampleButton" value="ok" />
    <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="j_id65" autocomplete="off" />
</form>

Thus, these parameters are root.

But how can I find out which parameters (name and value) will be sufficient for any other component?

: POST , HTML, "javax.faces.ViewState", .

+5
1

, , JSF - HTTP-, java.net.URLConnection Apache HttpComponents Client, ?

GET , HTTP- ( , JSESSIONID cookie) . HTTP- Set-Cookie GET, JSESSIONID Cookie POST. HTTP- , JSF "View Expired", - JSF HTTP 500 ViewExpiredException, - JSF .

JSF- CSRF, javax.faces.ViewState, GET. , name=value , , .

, GET HTML-

<form id="sampleForm" name="sampleForm" method="post" action="/pages/main/main.smnet" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="sampleForm" value="sampleForm" />
    <input id="sampleForm:sampleButton" type="submit" name="sampleForm:sampleButton" value="ok" />
    <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="j_id65" autocomplete="off" />
</form>

(Jsoup ) :

  • sampleForm=sampleForm
  • sampleForm:sampleButton=ok
  • javax.faces.ViewState=j_id65

, POST /pages/main/main.smnet ( JSESSIONID cookie!). , , () JSF- , . id="sampleButton" <h:commandButton>, JSF , sampleForm:j_id42. , , HTML-.

, / , API - . - Java EE, JSF HTML, JAX-RS REST.

. :

+8

All Articles