如何在HttpWebRequest的帮助下向JSF发送POST请求

本文关键字:JSF 发送 POST 请求 帮助 HttpWebRequest | 更新日期: 2023-09-27 18:18:15

我发送body login=login&login:loginName={0}&login:password={1}&login:clientCode=&login:j_idt171=&javax.faces.ViewState=stateless的post requestHttpWebRequest的帮助下。在此之前,我正在做GET请求,以获得JSESSIONID。Web表单看起来像这样:

<form id="login" name="login" method="post" action="/csologin/login.jsf">
<input type="hidden" name="login" value="login">
<div id="login:j_idt122" class="ui-messages ui-widget" aria-live="polite"></div><table border="0" cellpadding="2" cellspacing="0" width="580px">
    <tbody>
        <tr>
            <td class="ecol1"><label id="login:j_idt125" class="ui-outputlabel ui-widget fbold" for="login:loginName">Username<span class="ui-outputlabel-rfi">*</span></label></td>
            <td class="ecol2"><input id="login:loginName" name="login:loginName" type="text" autocomplete="off" size="40" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" role="textbox" aria-disabled="false" aria-readonly="false" aria-multiline="false"></td>
            <td class="ecol3"><div id="login:j_idt126" aria-live="polite" class="ui-message"></div></td>
        </tr>
    </tbody>
</table>
<table border="0" cellpadding="2" cellspacing="0" width="580px">
    <tbody>
        <tr>
            <td class="ecol1"><label id="login:j_idt132" class="ui-outputlabel ui-widget fbold" for="login:password">Password<span class="ui-outputlabel-rfi">*</span></label></td>
            <td class="ecol2"><input id="login:password" name="login:password" type="password" class="ui-inputfield ui-password ui-widget ui-state-default ui-corner-all ui-state-hover" autocomplete="off" size="40" role="textbox" aria-disabled="false" aria-readonly="false" aria-multiline="false"></td>
            <td class="ecol3"><div id="login:j_idt133" aria-live="polite" class="ui-message"></div></td>
        </tr>
    </tbody>
</table>
<table border="0" cellpadding="2" cellspacing="0" width="580px">
    <tbody>
        <tr>
            <td class="ecol1"><label id="login:j_idt135" class="ui-outputlabel ui-widget fbold" for="login:clientCode">Client Code</label></td>
            <td class="ecol2"><input id="login:clientCode" name="login:clientCode" type="text" maxlength="32" size="40" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" role="textbox" aria-disabled="false" aria-readonly="false" aria-multiline="false"></td>
            <td class="ecol3"><div id="login:j_idt136" aria-live="polite" class="ui-message"></div></td>
        </tr>
    </tbody>
</table>
<br><div class="text-center">
    <button id="login:j_idt139" name="login:j_idt139" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="barWaitDialog.show();" type="submit" role="button" aria-disabled="false"><span class="ui-button-text ui-c">Login</span></button>
    <!-- &lt;p:commandButton value="Clear" onclick="clearVisibleTextFields();" /&gt;  --><button id="login:j_idt141" name="login:j_idt141" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="PrimeFaces.ab({source:'login:j_idt141',oncomplete:function(xhr,status,args){clearVisibleTextFields();;}});return false;" type="submit" role="button" aria-disabled="false"><span class="ui-button-text ui-c">Clear</span></button><button id="login:j_idt142" name="login:j_idt142" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="submit" role="button" aria-disabled="false"><span class="ui-button-text ui-c">Cancel</span></button>
</div><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="stateless" autocomplete="off">

没有经过身份验证,它返回一个重定向到登录页面。我做错了什么?

如何在HttpWebRequest的帮助下向JSF发送POST请求

基本上,你没有按下想要的按钮。你需要包括name=value对提交按钮,你想"按"编程。这通常是在服务器端用来区分一个按钮是否被按下,如果是,是哪个。

在HTML表单中有三个提交按钮,简化如下:
<button name="login:j_idt139" type="submit">Login</button>
<button name="login:j_idt141" type="submit">Clear</button>
<button name="login:j_idt142" type="submit">Cancel</button>

看来JSF开发人员没有给这些按钮一个显式的ID,所以使用了一个自动生成的ID。这个自动生成的ID依赖于JSF组件树中按钮组件的位置。因此,如果JSF开发人员更改了视图组合(添加/移动/删除组件),那么很可能HTML标记中按钮的最终ID也会更改。你需要修改你的查询字符串。查询字符串中的参数login:j_idt171=可能需要更改为login:j_idt139=

为了使它更健壮,你可以尝试联系网站管理员/开发人员,并要求给这些按钮一个显式的ID在<p:commandButton id="login">,这样你就可以始终使用参数login:login=而不用担心自动生成的ID。如果这确实不是一个选项,那么您基本上需要使用HTML解析器来查找登录按钮的ID。