admin管理员组

文章数量:1336643

I want to submit form from a webpage which has several forms. I want to submit this form below in particular.

<form action="realDisplay.asp" method="post" name="Search" onSubmit="return validate(this); return submitForm();" target="_blank">
               <table width="98%" align="center" cellspacing="0" cellpadding="0" border="1" bordercolor="#FFFFFF">
              <tr>
                 <td width="127" class="style62" align="right">Parcel ID</td>
                <td width="286">
                  <div align="center">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
                      <tr>
                        <td align="left" class="style62">&nbsp;
                        <input name="rePID" size="15" maxlength="15" value="">
                         <br>
                          <font class="style65">( 12 123 12 123)</font></td>
                      </tr>
                    </table>
                  </div> 
                </td>
                </tr>
                <tr>
                 <td width="127" class="style62" align="right">Partial Parcel ID</td>
                <td width="286">
                  <div align="center">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
                      <tr>
                        <td align="left" class="style62">&nbsp;
                        <input name="rePartialPID" size="15" maxlength="15" value="">
                         <br>
                          <font class="style65">( 12 123)</font></td>
                      </tr>
                    </table>
                  </div> 
                </td>
                </tr>
              <tr>
                <td class="style62" align="right">Address</td>
                <td class="style62" align="left">&nbsp;
                  <input name="Address" size="38" maxlength="50" value="">
                </td>
              </tr>
               <tr>
                <td class="style62" align="right" nowrap="nowrap">Partial Street Name</td>
                <td class="style62" align="left">&nbsp;
                  <input name="streetName" size="38" maxlength="50" value="">
                </td>
              </tr>
              <tr>
                <td class="style62" align="right" nowrap="nowrap">Owner Name</td>
                <td class="style62" align="left">&nbsp;
                  <input name="OwnerName" size="30" maxlength="50" value="">   
                </td>
              </tr>
              <tr>
                <td colspan="2"><br /><font class="style64">Insert</font>
                  <font class="style65"><u>Either</u></b></font><font class="style64">&nbsp;a</font>:<br>
                 <br>
                 <table align="center" border="1" cellspacing="0" cellpadding="3" bgcolor="#E8E8E8">
                    <tr>
                      <td align="left"><img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
                      <td class="style75" align="left">
                      <a class="nav4" href="GlossaryTermWin.htm#ParcelID" onClick="NewWindow(this.href,'PARCELID','635','635','yes');return false;">
                     Parcel ID</a> , or Partial Parcel ID</td>
                    </tr>
                    <tr>
                    </tr>
                    <tr>
                      <td>
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
                      <td class="style75" align="left">Address (eg. 123 main), or</td>
                    </tr>
                    <tr>
                      <td class="style75" align="left">
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
                      <td class="style75">Partial Street Name (eg. main), or</td>
                    </tr>
                    <tr>
                      <td class="style75">
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
                      <td class="style75" align="left">Owner Name <br />(eg. LastName,FirstName <br />or
                              Partial Owner Name)</td>
                    </tr>
                </table> </td>
              </tr>
              <tr>
                 <td colspan="2">&nbsp;</td>
                    </tr>
              <tr>
                <td height="26" colspan="2" align="center">
                <input type="image" valign="top" name="Submit" value="Search" src="images/search.jpg" align="top" alt="Search by either the Parcel ID or Address or Owner Name that is associated by the real estate information.">
                <a href="javascript:document.forms[0].reset()" border="0"><img src="images/reset.jpg" align="top" border="0" alt="Reset the values on this page." onClick="ResetForm()"></a></td>
              </tr>
  </table>
</form></td>

My Java Code looks like this:

Document doc = Jsoup.connect("")
                   .data("rePID", "15 197 14 007")
                   .post();

I want to able to view the webpage that es up after I submit this form, and also view the text content of that page, and also want to get url as well.

What would I have to do in my Java code to make sure that submit request was submitted and view the content of next page after submitting the form.

I was able to submit form using Htmlunit, you can search the web and download the package.

import .gargoylesoftware.htmlunit.html.HtmlForm;
import .gargoylesoftware.htmlunit.html.HtmlPage;
import .gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import .gargoylesoftware.htmlunit.html.HtmlImageInput;
import .gargoylesoftware.htmlunit.html.HtmlTextInput;
import .gargoylesoftware.htmlunit.BrowserVersion;
import .gargoylesoftware.htmlunit.WebClient;

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
final HtmlPage searchPage = webClient.getPage(""); 

    final HtmlForm form = searchPage.getFormByName("Search"); 
    final HtmlTextInput textField = form.getInputByName("rePID");
    textField.setValueAttribute("15 197 14 007");
    final HtmlImageInput button = form.getInputByName("Submit"); 
    HtmlPage searchResultPage = (HtmlPage)button.click();

I want to submit form from a webpage which has several forms. I want to submit this form below in particular.

<form action="realDisplay.asp" method="post" name="Search" onSubmit="return validate(this); return submitForm();" target="_blank">
               <table width="98%" align="center" cellspacing="0" cellpadding="0" border="1" bordercolor="#FFFFFF">
              <tr>
                 <td width="127" class="style62" align="right">Parcel ID</td>
                <td width="286">
                  <div align="center">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
                      <tr>
                        <td align="left" class="style62">&nbsp;
                        <input name="rePID" size="15" maxlength="15" value="">
                         <br>
                          <font class="style65">( 12 123 12 123)</font></td>
                      </tr>
                    </table>
                  </div> 
                </td>
                </tr>
                <tr>
                 <td width="127" class="style62" align="right">Partial Parcel ID</td>
                <td width="286">
                  <div align="center">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
                      <tr>
                        <td align="left" class="style62">&nbsp;
                        <input name="rePartialPID" size="15" maxlength="15" value="">
                         <br>
                          <font class="style65">( 12 123)</font></td>
                      </tr>
                    </table>
                  </div> 
                </td>
                </tr>
              <tr>
                <td class="style62" align="right">Address</td>
                <td class="style62" align="left">&nbsp;
                  <input name="Address" size="38" maxlength="50" value="">
                </td>
              </tr>
               <tr>
                <td class="style62" align="right" nowrap="nowrap">Partial Street Name</td>
                <td class="style62" align="left">&nbsp;
                  <input name="streetName" size="38" maxlength="50" value="">
                </td>
              </tr>
              <tr>
                <td class="style62" align="right" nowrap="nowrap">Owner Name</td>
                <td class="style62" align="left">&nbsp;
                  <input name="OwnerName" size="30" maxlength="50" value="">   
                </td>
              </tr>
              <tr>
                <td colspan="2"><br /><font class="style64">Insert</font>
                  <font class="style65"><u>Either</u></b></font><font class="style64">&nbsp;a</font>:<br>
                 <br>
                 <table align="center" border="1" cellspacing="0" cellpadding="3" bgcolor="#E8E8E8">
                    <tr>
                      <td align="left"><img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
                      <td class="style75" align="left">
                      <a class="nav4" href="GlossaryTermWin.htm#ParcelID" onClick="NewWindow(this.href,'PARCELID','635','635','yes');return false;">
                     Parcel ID</a> , or Partial Parcel ID</td>
                    </tr>
                    <tr>
                    </tr>
                    <tr>
                      <td>
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
                      <td class="style75" align="left">Address (eg. 123 main), or</td>
                    </tr>
                    <tr>
                      <td class="style75" align="left">
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
                      <td class="style75">Partial Street Name (eg. main), or</td>
                    </tr>
                    <tr>
                      <td class="style75">
                      <img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
                      <td class="style75" align="left">Owner Name <br />(eg. LastName,FirstName <br />or
                              Partial Owner Name)</td>
                    </tr>
                </table> </td>
              </tr>
              <tr>
                 <td colspan="2">&nbsp;</td>
                    </tr>
              <tr>
                <td height="26" colspan="2" align="center">
                <input type="image" valign="top" name="Submit" value="Search" src="images/search.jpg" align="top" alt="Search by either the Parcel ID or Address or Owner Name that is associated by the real estate information.">
                <a href="javascript:document.forms[0].reset()" border="0"><img src="images/reset.jpg" align="top" border="0" alt="Reset the values on this page." onClick="ResetForm()"></a></td>
              </tr>
  </table>
</form></td>

My Java Code looks like this:

Document doc = Jsoup.connect("http://web.somewebsite.asp")
                   .data("rePID", "15 197 14 007")
                   .post();

I want to able to view the webpage that es up after I submit this form, and also view the text content of that page, and also want to get url as well.

What would I have to do in my Java code to make sure that submit request was submitted and view the content of next page after submitting the form.

I was able to submit form using Htmlunit, you can search the web and download the package.

import .gargoylesoftware.htmlunit.html.HtmlForm;
import .gargoylesoftware.htmlunit.html.HtmlPage;
import .gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import .gargoylesoftware.htmlunit.html.HtmlImageInput;
import .gargoylesoftware.htmlunit.html.HtmlTextInput;
import .gargoylesoftware.htmlunit.BrowserVersion;
import .gargoylesoftware.htmlunit.WebClient;

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
final HtmlPage searchPage = webClient.getPage("http://web.somewebsite.asp"); 

    final HtmlForm form = searchPage.getFormByName("Search"); 
    final HtmlTextInput textField = form.getInputByName("rePID");
    textField.setValueAttribute("15 197 14 007");
    final HtmlImageInput button = form.getInputByName("Submit"); 
    HtmlPage searchResultPage = (HtmlPage)button.click();
Share Improve this question edited Dec 21, 2012 at 2:25 user1701556 asked Dec 8, 2012 at 8:30 user1701556user1701556 472 gold badges2 silver badges8 bronze badges 5
  • take a look at last parameter stackoverflow./a/8986700/423868 I suppose you need to add submit button to your form and pass it as parameter – Georgy Gobozov Commented Dec 8, 2012 at 10:20
  • Thanks for pointing me in the right direction. My Java code looks like this: Document doc = Jsoup.connect("website./realSearch.asp") .data("rePID", "15 197 14 007") .data("Submit", "Search") .post(); How do I view the resulting webpage after submitting the form. – user1701556 Commented Dec 8, 2012 at 17:45
  • You can parse jsoup document that you get or you can String html = document.html(); – Georgy Gobozov Commented Dec 8, 2012 at 22:36
  • When I tried doc.html(); it showed the content of the webpage that from which I submitted the form. I was not able to get next page that es after submitting the form. – user1701556 Commented Dec 9, 2012 at 5:21
  • I was able to submit form using Htmlunit – user1701556 Commented Dec 21, 2012 at 2:15
Add a ment  | 

1 Answer 1

Reset to default 3

First of all, you will need to investigate the page itself. Chrome has nice build in feature to support it. Just press F12 and you will see quite few options and you can investigate almost everything related to particular HTML request. Newer versions of IE also have something similar. Firefox has great firebug extension.

You can see Cookies that are used by the server. Maybe you are missing them. Quite often most important one will be Set-Cookie. You will need to add it to Cookie Post request you will be sending in POST Html request.

In your case, you will also need to check java Script function SubmitForm. Please remember that JavaScript probably will not be called from your code. Your page you need to send request to will be "realDisplay.asp".

Sometime, browser send post request with some parameters in URL string, like with GET. Please check them too. Make your POST requests exactly the same as used by the browser.

Please check how long is the session while logged in via browser. To stay in session you will need to send some requests to the application. If they are not sent you will need to re-log in again.

There is another answer for using jsoup with cookies.

jsoup posting and cookie

Connection.Response res = Jsoup.connect("http://www.example./login.php")
.data("username", "myUsername", "password", "myPassword")
.method(Method.POST)
.execute();

Document doc = res.parse();

If you need more info please let me know.

本文标签: javahow to submit form using jsoupStack Overflow