【问题标题】:Android DefaultHTTPClient filling a form by Post request on javascript driven websiteAndroid DefaultHTTPClient 在 javascript 驱动的网站上通过 Post 请求填写表单
【发布时间】:2011-06-02 03:23:51
【问题描述】:

我的问题与此类似: HTTPclient POST with problematic web site

我使用篡改数据来查找通过客户端和服务器的所有请求。

我最初执行 get 请求以加载页面并获取动态生成的字段值。然后,我为相关表单的所有输入字段创建了命名值对列表并执行了 post req,但是它将我重定向到错误页面。

我尝试以 stackoverflow 上建议的所有可能方式设置 cookie 和处理 sslfactory:

但这对我不起作用。

我不知道是什么问题,我什至没有在日志中看到任何错误。 我在这个问题上花了几天时间。

【问题讨论】:

  • 我终于自己解决了这个问题。使用 HTTPCLeint 本身。我没有正确提取隐藏字段值。
  • 您能否将您的解决方案发布为答案,以便我们将其从未回答列表中删除?谢谢。

标签: java javascript android httpclient http-post


【解决方案1】:

解决方案是使用相同的 HttpClient,无需维护 cookie 或其他任何东西。 首先找到所有使用篡改数据发出的请求(获取或发布)。然后提取动态生成的隐藏字段值:

public static String getPage(String sURL) throws HttpException {
    HttpGet method = new HttpGet(sURL);

    // method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    // new DefaultHttpMethodRetryHandler(3, false));

    try {
        /*
        * int statusCode = client.execute(method); if (statusCode !=
        * HttpStatus.SC_OK) { System.err.println("Method failed: " +
        * method.getStatusLine()); }
        */HttpResponse res;
        res = client.execute(method);

        BasicResponseHandler myHandler = new BasicResponseHandler();
        String content = myHandler.handleResponse(res);
        //extract dynamic parameters here
        return content;
    } catch(...) {

    }
}

那么post使用这个方法:

public static String postPage1(list of parameters to be passed in the post form) throws HttpException {
    BasicNameValuePair[] data = {
        new BasicNameValuePair("field name", "param1"),
        ......
    };
    HttpPost post = new HttpPost(sURL);

    // post.setRequestBody(data);
    try {
        post.setEntity(new UrlEncodedFormEntity(Arrays.asList(data)));

        HttpResponse res;
        res = client.execute(post);
        int statusCode;

        BasicResponseHandler myHandler = new BasicResponseHandler();
        String content = myHandler.handleResponse(res);
        return content;
    } catch (...) {

    }
}

就是这样。

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2012-02-14
    相关资源
    最近更新 更多