Search code examples
c#asp.netasp.net-mvcjsftrinidad

Is it possible to send POST data from a JSF app to aspx .NET?


In our JSF app(MyFaces Trinidad) we have a link clicking on that will launch a application running using aspx (http://crd-dev:1890/Timeandreviews/Login.aspx). Since we have authenticated user already, we have to pass the user id to the ASPX application. They dont want to receive this s a GET param in URL, they need it as POST data. Is that possible to send it as POST data? If possible how to send in JSF and how to read it in ASPX.

I have tried setting like below in JSF (.XHTML page)

ExternalContext ext = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletRequest httpRequest = (HttpServletRequest) ext.getRequest();
    HttpSession httpSession = (HttpSession) ext.getSession(true);
    httpRequest.setAttribute("USER_HTREQ", "TST_USR");
    httpSession.setAttribute("USER_HTSES", "TST_USR");
    ext.getRequestMap().put("USER_REQMP", "TST_USR");
    try {
        ext.redirect("http://crd-dev:1890/Timeandreviews/Login.aspx");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

and tried reading in aspx as Method 1:

string[] keys = Request.Form.AllKeys;
for (int i= 0; i < keys.Length; i++) 
{
   Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
}

Method 2: var oSR = new StreamReader(Request.InputStream); string sContent = oSR.ReadToEnd();

But both of them dint work. Please help


Solution

  • Since you don't only need to post data, but also need to redirect the user, the Apache HttpClient is not going to help you.

    The simplest solution is just create a form with hidden inputs, fill them and post the form.

    <form method="post" id="form-id"
          action="http://crd-dev:1890/Timeandreviews/Login.aspx">
      <input type="hidden" name="input-id" id="input-id"/>
      etc.
    </form>
    

    You can execute Javascripts from your bean by adding it like this:

    String script = "document.getElementById('input-id').value = 'value';"
                  + "document.getElementById('form-id').submit();";
    FacesContext fctx = FacesContext.getCurrentInstance();  
    ExtendedRenderKitService erks =
        Service.getRenderKitService(fctx, ExtendedRenderKitService.class);
    erks.addScript(fctx, script);