开发者

Android DefaultHTTPClient filling a form by Post request on javascript driven website

My problem is similar to this: HTTPclient POST with problematic web site

I used tamper data to find all the requ开发者_开发技巧est going through the client and server.

I initially executed the get request to load the page and get the dynamically generated field values. Then I created the list of named value pairs for all the input fields of the concerned form and executed post req, however it redirects me to an error page.

I tried setting cookie and handling sslfactory in all possible ways suggested on stackoverflow:

  • Trusting all certificates using HttpClient over HTTPS
  • Android HttpClient persistent cookies

but it's not working for me.

I don't know what's the problem, I don't even get any error in logs. I spent a couple of days on this single issue.


The solution was using the same HttpClient without any need of maintaining cookies or anything else. Firstly find all the request(get or post) those are made using tamper data. Then extract the dynamically generated hidden field values:

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(...) {

    }
}

Then for post use this method:

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 (...) {

    }
}

That's it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜