java - 在 Java 中运行 Javascript 表单请求

标签 java javascript html post

我希望能够获取 POST 表单请求信息,该信息将返回以特定文本字段开头的公司名称。我想要执行此操作的网站如下:

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

我想获取“开头为”查询的结果。相反,我得到的是垃圾信息和“请启用 JavaScript 以查看页面内容”的内容。从我的结果来看。据我所知,可能存在两个问题之一。

1)我需要通过java启用javascript(如果可能)

2) 我没有正确发送请求,需要更改我的提交变量

如果第一个问题是我实现目标的最佳方法是什么?(另一种语言,如 php 等)。我一直无法找到解决此问题的简单方法。

如果问题 2 是我需要更改哪些变量?

以下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);

我尝试修改的表单代码如下:

搜索依据:

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

搜索文本:

  <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%;">

搜索类型:

 <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>

搜索按钮:

<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;">

谢谢!!!

最佳答案

请注意,HttpClient 最适合原始 HTTP 通信,而不适合支持 JavaScript 的无 GUI 浏览器。您可以使用selenium它对 javascript 或 htmlunit 有很好的支持

关于java - 在 Java 中运行 Javascript 表单请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27433269/

相关文章:

html - 如何对齐 <td rowspan ="value"> 值垂直居中?

html - 如何在单行中使用列表标签

java - 如何用连续的数字填充数组

javascript - jquery的图像预加载问题

javascript - 监听只有输入/返回键的事件(不是其他的)

javascript - Angular 5 : how to download pdf using Service worker

javascript - 需要有关 Javascript 的帮助....我认为

java - 使用 ffmpeg 将 flv 转换为 mp4

java - Selenium:WAITING元素消失

java - 初始化数组变量