简体   繁体   English

在Java中运行Javascript表单请求

[英]Running Javascript Form Request within Java

I want to be able to get POST form request information that would return the name of corporations that begin with a certain text field. 我希望能够获得POST表单请求信息,该信息将返回以特定文本字段开头的公司名称。 The site I want to do this from is below: 我要从中执行此操作的站点如下:

http://corp.sec.state.ma.us/corpweb/CorpSearch/CorpSearch.aspx http://corp.sec.state.ma.us/corpweb/CorpSearch/CorpSearch.aspx

I want to get results for the "Begins with" query. 我想获取“开头为”查询的结果。 Instead getting back results I get back junk and something that says "Please enable JavaScript to view the page content." 取而代之的是,我得到的是垃圾邮件,上面写着“请启用JavaScript来查看页面内容”。 from my results. 从我的结果。 From what I see believe there is possibly one of two problems. 从我看来,相信可能存在两个问题之一。

1) I need to enable javascript(if possible) through java 1)我需要通过Java启用javascript(如果可能)

2) I didn't send the request properly and need to change my submit variables 2)我没有正确发送请求,需要更改我的提交变量

If number 1 is the problem what is the best approach for me to take to accomplish my goal?(another language like php, etc). 如果数字1是问题,那么对我来说,实现目标的最佳方法是什么?(另一种语言,例如php等)。 I have been unable to find an easy solution to this problem. 我一直无法找到解决此问题的简便方法。

If number 2 is the problem what variables do I need to change? 如果数字2是问题,我需要更改哪些变量?

The following java code and html below: 以下Java代码和html如下:

HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(
                "http://corp.sec.state.ma.us/corpweb/CorpSearch/CorpSearch.aspx");
        httpPost.setHeader("Connection", "keep-alive");
        httpPost.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
        httpPost.setHeader("Accept",
                " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpPost.setHeader("Accept-Language", "en-us,en;q=0.5");
        httpPost.setHeader("Host", "mysite.com");

        ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
        nameValuePair.add(new BasicNameValuePair("ctl00$MainContent$btnSearch" , ""));
        nameValuePair.add(new BasicNameValuePair("ctl00$MainContent$ddBeginsWithEntityName" , "B"));
        nameValuePair.add(new BasicNameValuePair("ctl00$MainContent$CorpSearch" , "rdoByEntityName"));
        nameValuePair.add(new BasicNameValuePair(
                "ctl00$MainContent$txtEntityName", "google"));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        HttpResponse response = client.execute(httpPost);

The Form code I am trying to modify is below: 我尝试修改的表单代码如下:

Search By: 搜索依据:

<input id="MainContent_rdoByEntityName" type="radio" name="ctl00$MainContent$CorpSearch" value="rdoByEntityName" checked="checked">

Search Text: 搜索文字:

  <input name="ctl00$MainContent$txtEntityName" type="text" value="intelligent" maxlength="175" id="MainContent_txtEntityName" class="p3" onkeyup="javascript: textChangedEntityName(this);" onmousedown="javascript: textChangedEntityName(this);" style="width:90%;">

Search Type: 搜索类型:

 <select name="ctl00$MainContent$ddBeginsWithEntityName" id="MainContent_ddBeginsWithEntityName" class="p4" style="width:88%;">
        <option selected="selected" value="B">Begins with</option>
        <option value="M">Exact match</option>
        <option value="F">Full text</option>
        <option value="S">Soundex</option>

    </select>

Search Button: 搜索按钮:

<input type="submit" name="ctl00$MainContent$btnSearch" value="Search Corporations" onclick="javascript:return loadMaxHeightWidth();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btnSearch&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="MainContent_btnSearch" class="Button" style="width:200px;">

Thank you!!! 谢谢!!!

Please note that HttpClient is best for raw HTTP communication and not for GUI-less browser with javascript support. 请注意,HttpClient最适合原始HTTP通信,而不适合具有JavaScript支持的无GUI浏览器。 You can use selenium which has very good support for javascript or htmlunit 您可以使用 ,它对javascript或htmlunit都有很好的支持

Try out the HTMLUnit library. 试用HTMLUnit库。 It can execute javascript and perform actions as if a user is clicking or filling out forms. 它可以执行javascript并执行操作,就像用户单击或填写表单一样。 Its easy to use and to learn. 它易于使用和学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM