java - python和php中提交html表单很简单,新手用java可以吗?

标签 java php python http

我制作了两个版本的脚本,用于提交 (https) 网页表单并收集结果。一个版本在php中使用Snoopy.class,另一个在python中使用urllib和urllib2。现在我想做一个java版本。

Snoopy 使 php 版本非常容易编写,并且它在我自己的 (OS X) 机器上运行良好。但是它分配了太多内存,并且在 pair.com 网络托管服务上运行时在同一点(在 curl 执行期间)被杀死。在 dreamhost.com 网络托管服务上运行良好。

所以我决定在调查可能导致内存问题的原因时尝试使用 python 版本,而 urllib 和 urllib2 使这变得非常容易。脚本运行良好。获取大约 70,000 条数据库记录,使用数百个表单提交,在大约 7 分钟内保存到大约 10MB 的文件。

研究如何使用 java 执行此操作,我感觉它不会像使用 php 和 python 时那样简单。 Java 中的表单提交不适合普通人吗?

我一天中的大部分时间都在试图弄清楚如何设置 Apache HttpClient。也就是说,在我放弃之前。如果我需要多花几天时间来解决这个问题,那么我想这将是另一个问题的主题。

HttpClient innovation.ch 不支持 https。

WebClient 看起来至少需要几天时间才能弄清楚。

因此,php 和 python 版本轻而易举。 java版也能简单几行搞定吗?如果没有,我会把它留到以后再说,因为我只是个新手。如果是这样,请哪位好心人能给我指点光明吗?

谢谢。

为了比较,两个版本的基本代码行:


python 版本

import urllib
import urllib2

submitVars['firstName'] = "John"
submitVars['lastName'] = "Doe"
submitUrl = "https URL of form action goes here"
referer = "URL of referring web page goes here"

submitVarsUrlencoded = urllib.urlencode(submitVars)
req = urllib2.Request(submitUrl, submitVarsUrlencoded)
req.add_header('Referer', referer)
response = urllib2.urlopen(req)
thePage = response.read()

php版本

require('Snoopy.class.php');
$snoopy = new Snoopy;

$submit_vars["first_name"] = "John";
$submit_vars["last_name"] = "Doe";
$submit_url = "https URL of form action goes here";
$snoopy->referer = "URL of referring web page goes here"; 

$snoopy->submit($submit_url,$submit_vars);
$the_page = $snoopy->results;

最佳答案

使用 HttpComponents http://hc.apache.org/ .你需要:

示例代码:

import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.HttpClient;

import java.util.ArrayList;
import java.util.List;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;

public class HttpClientTest {
    public static void main(String[] args) throws Exception {

        // request parameters
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("q", "quality"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        HttpPost httppost = new HttpPost("http://stackoverflow.com/search");
        httppost.setEntity(entity);

        // execute the request
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httppost);

        // display the response status code
        System.out.println(response.getStatusLine().getStatusCode());

        // display the response body
        HttpEntity responseEntity = response.getEntity();
        OutputStream out = new ByteArrayOutputStream();
        responseEntity.writeTo(out);
        System.out.println(out);
    }
}

将其保存到 HttpClientTest.java。将这个 java 文件 httpcore-4.0.1.jar 和 httpclient-4.0-alpha4.jar 放在同一目录中假设您安装了 sun java 1.6 jdk,编译它:

javac HttpClientTest.java -cp httpcore-4.0.1.jar;httpclient-4.0-alpha4.jar;commons-logging-1.1.1.jar 

执行它

java HttpClientTest.class -cp httpcore-4.0.1.jar;httpclient-4.0-alpha4.jar;commons-logging-1.1.1.jar 

我认为这在 java 中和在 php 或 python 中一样简单(您的示例)。在所有情况下,您都需要:

  • 配置的sdk
  • 一个库(有依赖)
  • 示例代码

关于java - python和php中提交html表单很简单,新手用java可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1116921/

相关文章:

java - Google Docs List API PDF 文件下载不工作 (Android)

php - CodeIgniter:未定义的属性:stdClass::$date

python - 在 Gevent 应用程序中记录日志

python - 使用值接近 0 的 math.isclose 函数

java - 为什么我的 java lambda 有一个虚拟赋值比没有它快得多?

java - 使用自动生成的映射解析 JSON 数组会引发 NullPointerException

php - 在 Web 应用程序中获取 Asterisk 事件

python - Celery 与 Django 中的 Redis 代理 : tasks successfully execute, 但仍然存在太多持久的 Redis key 和连接

Java SE 窗口显示 "Not Responing"。我的 try/catch 有问题吗?

php - 它确实显示了一个用户,但它显示了我在 table 上的所有用户,我只是在寻找一个带有 $_session 的用户