gpt4 book ai didi

java - Apache HttpClient 4.2.1 登录成功后填写表单的POST请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:44 25 4
gpt4 key购买 nike

我正在编写一个可以登录站点并提交特定表单的应用程序。我可以成功登录,但是当我尝试发送另一个 POST 请求以提交另一个表单时,没有任何反应。

这是我的代码:

    try {
//log in to site
HttpPost httpPost = new HttpPost("http://mysite.ru");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("login", "guest"));
nvps.add(new BasicNameValuePair("password", "guest"));

httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

HttpResponse response = httpClient.execute(httpPost);

HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);

//all ok. i obtained cookie
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
ConnectionManager.printLog("- " + cookies.get(i).toString());
}
}

//then trying to fill another form in another page of this site
httpPost = new HttpPost("http://mysite.ru/?h[0][mode]=controllers&h[0][action]=add&h[1][mode]=controllers");

List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
nvps2.add(new BasicNameValuePair("owner", "0"));
nvps2.add(new BasicNameValuePair("imei", "123456789123456"));
nvps2.add(new BasicNameValuePair("password", "asdfghj"));
nvps2.add(new BasicNameValuePair("type_id", "6"));
nvps2.add(new BasicNameValuePair("remarks", ""));

httpPost.setEntity(new UrlEncodedFormEntity(nvps2));

response = httpClient.execute(httpPost);
//after filling this form, site must redirect me on another page.
entity = response.getEntity();
//but then I look on page I obtained, it's still the same page with form I tried to fill.
//It seems like I didn't post request.
String pageHTML = EntityUtils.toString(entity);
System.out.println(pageHTML);

EntityUtils.consume(entity);

} finally {
httpClient.getConnectionManager().shutdown();
}

第二种形式在类型上与第一种形式没有区别。

最佳答案

我解决了我的问题。在第二种形式(不是登录形式)中有提交按钮:

<form method="post">
<p>...<p>
<p>...<p>
<p>...<p>
<p>...<p>
<p>...<p>
<input type="submit" name="apply" value "Save">
</form>

要填写表格,保存并转到下一页,我应该在我的发布请求中再添加一个 ValuePair:

nvps2.add(new BasicNameValuePair("apply", "Save"));

不知道为什么我在填写授权表的时候不需要发送这样一个Button值来登录。但现在一切正常!

关于java - Apache HttpClient 4.2.1 登录成功后填写表单的POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12745710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com