static final String LOGON_SITE = "xxx.com";
static final int LOGON_PORT = 80;
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
PostMethod post = new PostMethod("/terminal/system/login.action?loginName=XXX&password=XXX&oem=terminal");
// 设置登陆需要的几个参数
// NameValuePair name = new NameValuePair("loginName","XXX");
// NameValuePair pass = new NameValuePair("password","XXX");
// NameValuePair em = new NameValuePair("oem","terminal");
// post.setRequestBody(new NameValuePair[]{name,pass,oem});
client.executeMethod(post);
System.out.println(post.getStatusLine().toString());
post.releaseConnection();
// 检查是否重定向
int statuscode = post.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
// 读取新的URL地址
Header header = post.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(redirect);
System.out.println("Redirect:" + redirect.getStatusLine().toString());
redirect.releaseConnection();
} else
System.out.println("Invalid redirect");
}
HttpClient登陆网站参考
最新推荐文章于 2019-08-01 17:07:30 发布