模拟输入用户名和口令进行登录 java应用程序直接进入126邮箱,失败了!
我的代码如下:
static final String LOGON_SITE = "localhost";//我的服务器地址
static final int LOGON_PORT = 8080;//我的服务器端口
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
//模拟登录页面
PostMethod post = new PostMethod("http://www.126.com");
NameValuePair name = new NameValuePair("user", "xxxxx");
NameValuePair pass = new NameValuePair("password", "xxxxx");
post.setRequestBody(new NameValuePair[]{name,pass});
int status = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
}
}
GetMethod get = new GetMethod("http://www.126.com");
client.executeMethod(get);
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
}
在控制台显示的还是www.126.com 登录页面的html脚本: