我有这个代码,编译并运行没有问题:
package isitup;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.CookieStore;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecFactory;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BrowserCompatSpec;
import org.apache.http.params.HttpParams;
public class Test {
public static void main(String args[])
{
try
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.cashialize.com/wp-login.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
EntityUtils.consume(entity);
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (Cookie cooky : cookies) {
System.out.println("- " + cooky.toString());
}
}
HttpPost httpost = new HttpPost("http://www.cashialize.com/wp-login.php");
List <NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("log", "test"));
nvps.add(new BasicNameValuePair("pwd", "test"));
nvps.add(new BasicNameValuePair("rememberme", "forever"));
nvps.add(new BasicNameValuePair("redirect_to", "http://www.articlepub.com/wp-admin/"));
nvps.add(new BasicNameValuePair("testcookie", "1"));
nvps.add(new BasicNameValuePair("wp-submit", "Log In"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
EntityUtils.consume(entity);
}
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (Cookie cooky : cookies) {
CookieStore cookieStore = new BasicCookieStore();
// Bind custom cookie store to the local context
httpclient.setCookieStore(cookieStore);
CookieSpecFactory csf = new CookieSpecFactory() {
public CookieSpec newInstance(HttpParams params) {
return new BrowserCompatSpec() {
@Override
public void validate(Cookie cookie, CookieOrigin origin)
throws MalformedCookieException {
// allow all cookies
}
};
}
};
httpclient.getCookieSpecs().register("easy", csf);
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "easy");
}
}
HttpGet httpGet = new HttpGet("http://www.articlepub.com/wp-admin/profile.php");
response = httpclient.execute(httpGet);
entity = response.getEntity();
// System.out.println("Page Contents: " + EntityUtils.toString(entity));
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
EntityUtils.consume(entity);
}
System.out.println("Post get cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (Cookie cooky : cookies) {
System.out.println("- " + cooky.toString());
}
}
httpclient.getConnectionManager().shutdown();
} catch (Exception e) {
System.out.println(e);
}
}
}
问题是我无法让用户登录。我很确定我在cookies设置中遗漏了一些内容,但我无法发现它,因为这是我第一次登录尝试。
控制台输出似乎也很好 - 唯一的问题是我无法发现cookie。这是:
Login form get: HTTP/1.1 200 OK
Initial set of cookies:
None
Login form get: HTTP/1.1 302 Found
Post logon cookies:
None
Login form get: HTTP/1.1 200 OK
Post get cookies:
None
请帮我解决这个噩梦吧!我很确定我错过了一些非常小的东西,但我无法弄明白。
答案 0 :(得分:1)
我认为您需要在执行之前在客户端上配置cookie存储。您也可以使用cookie管理器配置HttpContext。
尝试查看此文档:http://hc.apache.org/httpcomponents-client-4.2.x/tutorial/html/statemgmt.html