HttpClient实现页面登录功能
package test;import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import dao.HttpClientDao;
public class HttpClientxcxxgl {
static final String LOGON_SITE = "139.16.24.7";
static final int LOGON_PORT = 8888;
public static void main(String[] args) throws Exception{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
HttpClientDao dao = new HttpClientDao();
//模拟登录页面login.action->main.action
PostMethod post = new PostMethod("http://139.16.24.7:8888/qysds/login.action");
NameValuePair name = new NameValuePair("name", "wyx3");
NameValuePair pass = new NameValuePair("password", "hbdswyx");
post.setRequestBody(new NameValuePair[]{name,pass});
int status = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
//查看cookie信息
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
boolean bLogin = false;
if (cookies.length == 0) {
dao.insertHtml("http://139.16.24.7:8888/qysds/login.action");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
System.out.println("登录成功");
bLogin = true;
}
}
if(bLogin){
//访问所需的页面main.action
GetMethod get = new GetMethod("/qysds/main.action");
client.executeMethod(get);
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
}
}
}
不知道哪儿出问题了。 HttpClient cookies cookie --------------------编程问答-------------------- 你还是把异常信息帖出来吧,要不人家怎么帮你分析。 --------------------编程问答--------------------
package test;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import dao.HttpClientDao;
public class HttpClientxcxxgl {
static final String LOGON_SITE = "139.16.24.7";
static final int LOGON_PORT = 8888;
public static void main(String[] args) throws Exception{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
HttpClientDao dao = new HttpClientDao();
//模拟登录页面login.action->main.action
PostMethod post = new PostMethod("http://139.16.24.7:8888/qysds/login.action");
NameValuePair name = new NameValuePair("name", "wyx3");
NameValuePair pass = new NameValuePair("password", "hbdswyx");
post.setRequestBody(new NameValuePair[]{name,pass});
int status = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
//查看cookie信息
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
boolean bLogin = false;
if (cookies.length == 0) {
dao.insertHtml("http://139.16.24.7:8888/qysds/login.action");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
System.out.println("登录成功");
bLogin = true;
}
}
if(bLogin){
//访问所需的页面main.action
GetMethod get = new GetMethod("/qysds/main.action");
client.executeMethod(get);
System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
}
}
}
补充:Java , Java SE