public void login(String email,String pwd){
httpClient = new HttpClient();
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
String url = "http://www.kaixin001.com/login/login.php";
PostMethod postMethod = new PostMethod(url);
NameValuePair[] data = { new NameValuePair("email", email),
new NameValuePair("password", pwd),
new NameValuePair("url", "/home/") };
postMethod.setRequestBody(data);
try {
statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { Header locationHeader = postMethod.getResponseHeader("location");
postMethod.releaseConnection();
if (locationHeader != null){
String cookieUrl = locationHeader.getValue();
GetMethod getMethod = new GetMethod(cookieUrl);
//就在这个地方登陆失败
httpClient.executeMethod(getMethod);
getMethod.releaseConnection();
}
}
postMethod.releaseConnection();
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}
=================================================================================
statusCode 是302 ,不过重定向时,总是出异常,有人做过吗? 是不是我的cookie没有做处理?
httpClient = new HttpClient();
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
String url = "http://www.kaixin001.com/login/login.php";
PostMethod postMethod = new PostMethod(url);
NameValuePair[] data = { new NameValuePair("email", email),
new NameValuePair("password", pwd),
new NameValuePair("url", "/home/") };
postMethod.setRequestBody(data);
try {
statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { Header locationHeader = postMethod.getResponseHeader("location");
postMethod.releaseConnection();
if (locationHeader != null){
String cookieUrl = locationHeader.getValue();
GetMethod getMethod = new GetMethod(cookieUrl);
//就在这个地方登陆失败
httpClient.executeMethod(getMethod);
getMethod.releaseConnection();
}
}
postMethod.releaseConnection();
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}
=================================================================================
statusCode 是302 ,不过重定向时,总是出异常,有人做过吗? 是不是我的cookie没有做处理?
www.kaixin.com的我写出来了,她和www.kaixin001.com不一样
public static void login(String email,String pwd){ HttpClient httpClient = new HttpClient();
//去掉cookie设置,采用httpclient的默认cookie设置 String url = "http://www.kaixin001.com/login/login.php"; PostMethod postMethod = new PostMethod(url);
NameValuePair[] data = { new NameValuePair("email", email),
new NameValuePair("password", pwd),
new NameValuePair("url", "/home/") }; postMethod.setRequestBody(data); try {
int statusCode = httpClient.executeMethod(postMethod);
System.out.println("statusCode--"+statusCode);
//Cookie[] cookies=httpClient.getState().getCookies();
if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { Header locationHeader = postMethod.getResponseHeader("location");
postMethod.releaseConnection();
if (locationHeader != null){
String cookieUrl = locationHeader.getValue();
System.out.println("cookieUrl--"+cookieUrl);
String r_url="http://www.kaixin001.com"+cookieUrl;//修改302后的url
System.out.println("r_url--"+r_url);
GetMethod getMethod = new GetMethod(r_url);
//httpClient.getState().addCookies(cookies);
int status=httpClient.executeMethod(getMethod);
System.out.println("status---"+status);
System.out.println(getMethod.getResponseBodyAsString());
getMethod.releaseConnection();
}
}
postMethod.releaseConnection();
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}
这可是我花了半个晚上搞定的,呵呵!