方式一 :
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.Test;
public class HttpClientLogin {
public static void main(String[] args){
//登陆 Url
String loginUrl = "http://v57.demo.dedecms.com/dede/login.php";
//需登陆后访问的 Url
String dataUrl = "http://v57.demo.dedecms.com/dede/index.php";
HttpClient httpClient = new HttpClient();
//模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
PostMethod postMethod = new PostMethod(loginUrl);
//设置登陆时要求的信息,一般就用户名和密码,验证码自己处理了
NameValuePair[] data = {
new NameValuePair("userid","admin"),
new NameValuePair("pwd", "admin"),
new NameValuePair("gotopage", ""),
new NameValuePair("dopost", "login"),
new NameValuePair("adminstyle", "newdedecms")
};
postMethod.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
postMethod.setRequestBody(data);
try {
//设置 HttpClient 接收 Cookie,用与浏览器一样的策略
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.executeMethod(postMethod);
//输出登录成功页面
System.out.println(postMethod.getResponseBodyAsString());
System.out.println("======================================");
//获得登陆后的 Cookie
Cookie[] cookies=httpClient.getState().getCookies();
String tmpcookies= "";
for(Cookie c:cookies){
tmpcookies += c.toString()+";";
}
//进行登陆后的操作
GetMethod getMethod = new GetMethod(dataUrl);
//每次访问需授权的网址时需带上前面的 cookie 作为通行证
getMethod.setRequestHeader("Cookie",tmpcookies);
System.out.println("tmpcookies【"+tmpcookies+"】");
//你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
//例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
postMethod.setRequestHeader("Referer", "http://v57.demo.dedecms.com/dede/login.php");
postMethod.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
httpClient.executeMethod(getMethod);
//打印出返回数据,检验一下是否成功
String text = getMethod.getResponseBodyAsString();
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
}
}
}
java模拟登录dedecms 登录成功后 回话不存在 访问其他页面提示 登录
方式2 Htmlunit
package tv.zjj.admintools.dedecms;
import java.net.URL;
import java.util.Arrays;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
public class HtmlunitUtil {
public static WebRequest request;
private final static String LOGIN_URL="/login.php";
private final static String ADD_URL="/article_add.php?channelid=1";
private final static String ADD_ACTION_URL="/article_add.php?channelid=1&dopost=save";
public static void main(String[] args) {
WebClient client = login("http://v57.demo.dedecms.com/dede/", "admin", "admin", "gbk");
System.out.println(client);
}
public static WebClient login(String host, String uerName, String password, String charset){
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
try {
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setActiveXNative(true);
webClient.getOptions().setAppletEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setTimeout(30000);
request=new WebRequest(new URL(host+LOGIN_URL),HttpMethod.POST);
request.setCharset("gbk");
request.setRequestParameters(Arrays.asList(
new NameValuePair("userid",uerName),
new NameValuePair("pwd", password),
new NameValuePair("gotopage", ""),
new NameValuePair("dopost", "login"),
new NameValuePair("adminstyle", "newdedecms")
));
HtmlPage p = webClient.getPage(request);
if (p.getBody().getTextContent().indexOf("成功登录") > 0) {
request = new WebRequest(new URL("http://v57.demo.dedecms.com/dede/index.php"),HttpMethod.GET);
request.setAdditionalHeader("Referer", "http://v57.demo.dedecms.com/dede/index.php");
//System.setProperty("apache.commons.httpclient.cookiespec", CookiePolicy.BROWSER_COMPATIBILITY);
p = webClient.getPage(request);
String content=p.asText();
System.out.println(":::"+content);
return webClient;
} else {
return null;
}
} catch (Exception e) {
System.out.println(e.toString());
}
return null;
}
public static String doGet(WebClient client,String url ,String charset){
try {
request = new WebRequest(new URL(url),HttpMethod.GET);
request.setCharset(charset);
HtmlPage p = client.getPage(request);
return p.getBody().asText();
} catch (Exception e) {
}
return null;
}
}
htmlunit 方式也不行