我的问题与此类似: HTTPclient POST with problematic web site
我使用篡改数据来查找通过客户端和服务器的所有请求。
我最初执行了 get 请求来加载页面并获取动态生成的字段值。然后,我为相关表单的所有输入字段创建了命名值对列表并执行了 post req,但是它将我重定向到错误页面。
我尝试以 stackoverflow 上建议的所有可能方式设置 cookie 并处理 sslfactory:
但这对我不起作用。
我不知道出了什么问题,我什至在日志中没有收到任何错误。 我在这个问题上花了几天时间。
最佳答案
解决方案是使用相同的HttpClient,无需维护 cookie 或其他任何内容。 首先找到所有使用篡改数据发出的请求(get 或 post)。然后提取动态生成的隐藏字段值:
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(...) {
}
}
然后对于帖子使用此方法:
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 (...) {
}
}
就是这样。
关于java - Android DefaultHTTPClient 在 javascript 驱动的网站上通过 Post 请求填充表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6210033/