HttpPost表单提交错误

时间:2015-03-12 23:00:52

标签: java html forms http-post

我正在尝试使用HttpClient登录成绩数据库。我发送有效的LogOnDetails.Username和LogOnDetails.Password信息,但每当我提交它时,一致地发回错误。我不知道它发生了什么,它可能成功登录然后挂断,或者甚至可能没有进入。这里是请求后输出的html:

output

这是我的代码:

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://home-access.cfisd.net/HomeAccess/Account/LogOn");
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(new BasicNameValuePair("LogOnDetails.Username", "s491670"));
    list.add(new BasicNameValuePair("LogOnDetails.Password","qrrp4ji6t"));
    post.setEntity(new UrlEncodedFormEntity(list));

    HttpResponse response = client.execute(post);
    BufferedReader file = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    PrintWriter pw = new PrintWriter(new FileWriter(new File("output.txt")));
    String line = null;
    while((line = file.readLine())!=null)
        pw.println(line);
    pw.close();
如果有人能对此有所了解,我会永远爱他们。如果需要登录表单的HTML,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:1)

当表单提交以下表单时,会发送数据:

Form Data
Database:10
LogOnDetails.UserName:sadf
LogOnDetails.Password:sdf

添加以下内容:

list.add(new BasicNameValuePair("Database", "10")); // or 20

如你所知,我无法测试。

答案 1 :(得分:1)

如果您添加@ robbmj补丁,则不会获得500错误页面,而是302(移动),这是一个良好的开端:

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/HomeAccess/">here</a>.</h2>
</body></html>

您可以建议http客户端自行处理重定向,但根据http-client的版本,处理重定向的方式不同(他们总是重构此代码)。你使用的是哪个版本?

也许302表示它全部成功,我现在已登录。 ;)

标题返回:

Cache-Control : private
Content-Type : text/html; charset=utf-8
Location : /HomeAccess/
Server : Microsoft-IIS/7.5
X-AspNetMvc-Version : 4.0
X-AspNet-Version : 4.0.30319
Set-Cookie : ASP.NET_SessionId=hvjw3jqjoaa5ohofaaxu4od1; path=/; HttpOnly
Set-Cookie : .AuthCookie=; expires=Tue, 12-Oct-1999 05:00:00 GMT; path=/; HttpOnly
Set-Cookie : .AuthCookie=0863B972684CC784E4D9D5594354B6F08FF6FF7225836F01A9715D0ABA633042946B032987F7926588610F5FB7C18757CE759338B75E341DF56DB3FB71BC326B3D6E49EA94EEE43B39FCC84BB98F236CA0D63CE668E14434169C6B835FA671DD; path=/; HttpOnly
X-Powered-By : ASP.NET
Date : Thu, 12 Mar 2015 23:37:12 GMT
Content-Length : 129