81,122
社区成员




public String jumpInstruction() throws Exception {
httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
try {
String newUrl = "http://192.168.1.10/wiki/index.php?user-login";
String Referer = " http://192.168.1.10/wiki/index.php?user-login";
String ContentType = "application/x-www-form-urlencoded";
postMethod = new PostMethod(newUrl);
postMethod.setRequestHeader("Referer", Referer);
postMethod.setRequestHeader("Content-Type", ContentType);
NameValuePair[] data = {
new NameValuePair("username","wxl"),
new NameValuePair("password","wxl"),
new NameValuePair("submit","%E7%99%BB%E5%BD%95")// 这里我用FireFox做重放时,里面只要有newUrl、Referer、ContentType这三项就可以重放登录成功
};
postMethod.setRequestBody(data);
int code = httpClient.executeMethod(postMethod);
Cookie[] cookies = httpClient.getState().getCookies();
for(Cookie obj : cookies){
httpClient.getState().addCookies(cookies);
System.out.println(obj.getName() + "=" + obj.getValue());// 这里Cookie可以正常写入
}
System.out.println(cookies.length + " cookie.length-----"); // 这里返回的始终是200,不知道为什么?
} catch (HttpException e) {
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
postMethod.releaseConnection();
}
return null;
}