在使用httpclient的postmethod,返回的状态码总是200,不能返回正确值,所以无法获得登录后的页面,请问该如何解决?代码如下: PostMethod authpost = new PostMethod("/login.jsp");
// Prepare login parameters
NameValuePair userid = new NameValuePair("name", "1");
NameValuePair password = new NameValuePair("Password", "1");
authpost.setRequestBody(
new NameValuePair[] {userid,password}); client.executeMethod(authpost); System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();
// See if we got any cookies
// The only way of telling whether logon succeeded is
// by finding a session cookie
Cookie[] logoncookies = cookiespec.match(
LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < logoncookies.length; i++) {
System.out.println("- " + logoncookies[i].toString());
}
}
// Usually a successful form-based login results in a redicrect to
// another url
int statuscode = authpost.getStatusCode();
String str=new String();
System.out.println("bian ma"+str.valueOf(statuscode));
System.out.println("lisu"+ authpost.getStatusLine().toString());
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
redirect.releaseConnection();
} else {
System.out.println("Invalid redirect");
System.exit(1);
}
}
// Prepare login parameters
NameValuePair userid = new NameValuePair("name", "1");
NameValuePair password = new NameValuePair("Password", "1");
authpost.setRequestBody(
new NameValuePair[] {userid,password}); client.executeMethod(authpost); System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();
// See if we got any cookies
// The only way of telling whether logon succeeded is
// by finding a session cookie
Cookie[] logoncookies = cookiespec.match(
LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < logoncookies.length; i++) {
System.out.println("- " + logoncookies[i].toString());
}
}
// Usually a successful form-based login results in a redicrect to
// another url
int statuscode = authpost.getStatusCode();
String str=new String();
System.out.println("bian ma"+str.valueOf(statuscode));
System.out.println("lisu"+ authpost.getStatusLine().toString());
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
redirect.releaseConnection();
} else {
System.out.println("Invalid redirect");
System.exit(1);
}
}
解决方案 »
- 求一道JAVA笔试题(为什么照代码敲进去都出错,提示在List那一行)
- 问一个关于RTTI转型的简单问题。
- 重写paint()方法后Frame背景图片不显示
- 我在用户session里存一个含有五个属性的bean对象 和 在session里只存一个字符串用户ID 这种性能差别有多大
- 如何用反射机制写一个整型数组?
- 向高手请教一个问题!
- 我的ie怎么不能看java的小应用程序
- 我想找个合作者并送给帮我的人初学者的礼物
- 一个问题解决了,另一个问题又来了。呀!websphere,我恨,恨,恨...!
- 怎么给一个CSV文件中的数据插入行号
- 如何使用第三方提供的***.jar式的类文件?--急急急.....
- execl中的日期如何导进数据库,高手指教
All response codes between 300 and 399 inclusive are redirect responses of some form. The most common redirect response codes are:301 Moved Permanently. HttpStatus.SC_MOVED_PERMANENTLY
302 Moved Temporarily. HttpStatus.SC_MOVED_TEMPORARILY
303 See Other. HttpStatus.SC_SEE_OTHER
307 Temporary Redirect. HttpStatus.SC_TEMPORARY_REDIRECT
我只有获得300-399之间的数才能重定向网页。所以200是不行的