因此,我尝试使用 Apache 的 HttpClient v.4 登录网站(在此示例中为 myspace),但我不确定在此过程中哪里出错了,当我测试此代码时,登录后 Cookie 与初始登录 cookie,但不应该这样。
我在网上四处查看是否有其他人尝试过,但我还没有找到适合我的好资源。
我正在使用这个例子:apache HttpClient to access facebook ( Facebook)
try{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.myspace.com/auth/form");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
HttpPost httpost = new HttpPost("http://www.myspace.com/auth/form");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "someEmail"));
nvps.add(new BasicNameValuePair("password", "somePassword"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
//System.out.println("Double check we've got right page " + EntityUtils.toString(entity));
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
httpclient.getConnectionManager().shutdown();
}
catch(Exception e){e.printStackTrace();}
示例饼干:
[version: 0][name: MSCOUNTRY][value: US][domain: .myspace.com][path: /][expiry: Tue Jun 12 23:22:15 EDT 2012]
[version: 0][name: MSCulture][value: IP=XXX.XXX.XXXX&IPCulture=en-US&PreferredCulture=en-US&PreferredCulturePending=&Country=VVM=&ForcedExpiration=0&timeZone=0&myStuffDma=&myStuffMarket=&USRLOC=RandomUserLocString==&UserFirstVisit=1][domain: .myspace.com][path: /][expiry: Tue Jun 12 23:22:15 EDT 2012]
[version: 0][name: SessionDDF2][value: RandomStringHere==][domain: .myspace.com][path: /][expiry: Sat Jun 05 23:22:15 EDT 2032]