HttpPost 表单提交错误
HttpPost form submission error
我正在尝试使用 HttpClient 登录成绩数据库。我向它发送有效的 LogOnDetails.Username 和 LogOnDetails.Password 信息,但每当我提交它时,它总是发回错误。我不确定它发生在哪里,它可能登录成功然后挂断,或者它甚至可能没有进入。这是请求后输出的html:
这是我的代码:
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 用于登录表单,请告诉我。谢谢!
当表单提交时发送以下表单数据:
Form Data
Database:10
LogOnDetails.UserName:sadf
LogOnDetails.Password:sdf
添加以下内容:
list.add(new BasicNameValuePair("Database", "10")); // or 20
如你所知,我无法对此进行测试。
如果添加@robbmj 的补丁,您不会得到 500 错误页面,而是 302(已移动),这是一个好的开始:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/HomeAccess/">here</a>.</h2>
</body></html>
您可以建议 http-client 自行处理重定向,但根据 http-client 的版本,处理重定向的方式不同(它们总是重构此代码)。您使用的是哪个版本?
也许 302
意味着一切都成功了,我现在已经登录了。 ;)
Headers 返回:
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
我正在尝试使用 HttpClient 登录成绩数据库。我向它发送有效的 LogOnDetails.Username 和 LogOnDetails.Password 信息,但每当我提交它时,它总是发回错误。我不确定它发生在哪里,它可能登录成功然后挂断,或者它甚至可能没有进入。这是请求后输出的html:
这是我的代码:
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 用于登录表单,请告诉我。谢谢!
当表单提交时发送以下表单数据:
Form Data
Database:10
LogOnDetails.UserName:sadf
LogOnDetails.Password:sdf
添加以下内容:
list.add(new BasicNameValuePair("Database", "10")); // or 20
如你所知,我无法对此进行测试。
如果添加@robbmj 的补丁,您不会得到 500 错误页面,而是 302(已移动),这是一个好的开始:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/HomeAccess/">here</a>.</h2>
</body></html>
您可以建议 http-client 自行处理重定向,但根据 http-client 的版本,处理重定向的方式不同(它们总是重构此代码)。您使用的是哪个版本?
也许 302
意味着一切都成功了,我现在已经登录了。 ;)
Headers 返回:
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