论坛首页 入门讨论版

有没有人用httpclient登陆过google,我试了半天都得不到cookies,大家帮我看看。

浏览 495 次
该帖已经被评为新手帖
作者 正文
最后更新时间:2008-06-13 关键字: httpclient
lient.getHostConfiguration().setHost("www.google.com", 80, "http");

PostMethod postMethod = new PostMethod(
"/accounts/LoginAuth?continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN");
// 请求信息的参数
NameValuePair ie = new NameValuePair("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727; MEGAUPLOAD 1.0)");
NameValuePair Email = new NameValuePair("Email", "user");
NameValuePair Passwd = new NameValuePair("Passwd", "password");
NameValuePair ct = new NameValuePair("Content-Type",
"application/x-www-form-urlencoded");
NameValuePair cl = new NameValuePair("Accept-Language", "zh-cn");
NameValuePair accept = new NameValuePair(
"Accept",
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-  " +
"excel, application/vnd.ms-powerpoint, application/msword, */*");
NameValuePair content = new NameValuePair("Content-Length", "137");
NameValuePair referer = new NameValuePair(
"Referer",
"https://www.google.com/accounts/Login?continue=http://www.google.cn/&hl=zh-CN");
NameValuePair action = new NameValuePair(
"action",
"https://www.google.com/accounts/LoginAuth?continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN");
NameValuePair encode = new NameValuePair("Accept-Encoding",
"gzip, deflate");
NameValuePair host = new NameValuePair("Host", "www.google.com");
NameValuePair conn = new NameValuePair("Connection", "Keep-Alive");
NameValuePair cache = new NameValuePair("Cache-Control", "no-cache");
NameValuePair cookie = new NameValuePair("Cookie",
"GoogleAccountsLocale_session=zh_CN; PREF=ID=14b30dc6bc9025ff:TB=2:NW=1:TM=1211940408:LM=1212373468:DV=AA:S=atzcI-gCcD94m_Nu");
// 把参数加入请求
postMethod.setRequestBody(new NameValuePair[] { Email, Passwd, action, accept, referer, cl, ct, encode, ie, host, content, conn, cache, cookie});
// 执行请求
client.executeMethod(postMethod);
System.out.println(postMethod.getResponseBodyAsString());

// CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
// Cookie[] cookies1 = cookiespec.match("www.google.com", 80, "/" ,
// false , client.getState().getCookies());
//获得cookie
Header newCookieHeader = postMethod.getResponseHeader("Set-Cookie");
System.out.print(newCookieHeader.getValue());

//获得状态,判断是否要跳转
int state = postMethod.getStatusCode();

System.out.println("state = "+state);
//释放连接
postMethod.releaseConnection();
                
String newUrl = "";
                
if ((state == HttpStatus.SC_MOVED_TEMPORARILY)
¦ ¦ (state == HttpStatus.SC_MOVED_PERMANENTLY)
¦ ¦ (state == HttpStatus.SC_SEE_OTHER)
¦ ¦ (state == HttpStatus.SC_TEMPORARY_REDIRECT)){
Header header = postMethod.getResponseHeader("location");
                        
if(header != null){
//获得跳转页面地址
newUrl = header.getValue();
                                
System.out.println("newUrl: "+newUrl);
                                
if(newUrl == null ¦ ¦ newUrl.equals("")){
newUrl = "/";
}
//以get方式请求跳转页面
GetMethod getMethod = new GetMethod(newUrl);
//执行请求
client.executeMethod(getMethod);
getMethod.releaseConnection();
}else{
System.out.println("连接出现问题!");
}
}else{
System.out.println("没有跳转!");
}

System.out.println("------------- END ---------------");

另外,就是如果登陆成功了,得到了cookies,应该如何保存它,并且在下次访问google的使用使用这个cookies,代替登陆的过程。


POST /accounts/LoginAuth?continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*
Referer: https://www.google.com/accounts/Login?continue=http://www.google.cn/&hl=zh-CN
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727; MEGAUPLOAD 1.0)
Host: www.google.com
Content-Length: 137
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: GoogleAccountsLocale_session=zh_CN; PREF=ID=14b30dc6bc9025ff:TB=2:NW=1:TM=1211940408:LM=1212373468:DV=AA:S=atzcI-gCcD94m_Nu

continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN&Email=xxx&Passwd=xxx&PersistentCookie=yes&rmShown=1&signIn=%E7%99%BB%E5%BD%95

GET /accounts/LoginAuth?continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN HTTP/1.1
User-Agent: Internet Explorer
Host: www.google.com
Cookie: GoogleAccountsLocale_session=zh_CN; PREF=ID=14b30dc6bc9025ff:TB=2:NW=1:TM=1211940408:LM=1212373468:DV=AA:S=atzcI-gCcD94m_Nu

HTTP/1.1 302 Moved Temporarily
Set-Cookie: GoogleAccountsLocale_session=zh_CN
Set-Cookie: SID=DQAAAHkAAADIDxkO2dhcjIzVROlYSisdKu0nhr7P3xuT5NqZOHcUW5x6VULVItMgjH7bNfUqP5waRuqIWdQHloGACiZ0Q-ovWVOxRoo7LV2VcWryCbA2R8PK8_ymgL2ullHikkp-o_VB4BBl7qn6qxxh6wIAbibd6kwd56Tm-wnGjfYg555Q7Q;Domain=.google.com;Path=/;Expires=Sun, 10-Jun-2018 13:35:21 GMT
Set-Cookie: LSID=EXPIRED;Domain=.google.com;Path=/;Expires=Mon, 01-Jan-1990 00:00:00 GMT
Set-Cookie: LSID=EXPIRED;Path=/;Expires=Mon, 01-Jan-1990 00:00:00 GMT
Set-Cookie: LSID=EXPIRED;Domain=www.google.com;Path=/accounts;Expires=Mon, 01-Jan-1990 00:00:00 GMT
Set-Cookie: LSID=DQAAAHwAAAAH6m-Sc17bnk14XBq33lxMAe_Zka2b3uGUBVEFhXy9Csi3SaMXMYlBCFXHcTZ8g8VPjPgAYgLxbJgGydV3Qj1glqcfPJHrzXXJJomjK7W69FIsDGZg1wD-QK-eD6xaG5L8qTKMK1op83NdVXaC6cZQjs1xV3h_3_jZN55GGVXlRA;Path=/accounts;Expires=Sun, 10-Jun-2018 13:35:21 GMT;Secure
Set-Cookie: GAUSR=xxx@gmail.com;Path=/accounts;Expires=Sun, 10-Jun-2018 13:35:21 GMT;Secure
Content-Type: text/html; charset=UTF-8
Location: https://www.google.com/accounts/CheckCookie?continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN&chtml=LoginDoneHtml
Content-Encoding: gzip
Date: Thu, 12 Jun 2008 13:35:21 GMT
Expires: Thu, 12 Jun 2008 13:35:21 GMT
Cache-Control: private, max-age=0
Content-Length: 237
Server: GFE/1.3

-----------------------------------------------------------------------------------------------------------

public static void loginGoogle() {
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://www.google.com/");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestProperty("User-Agent",
"Internet Explorer");
httpurlconnection.setRequestProperty("Host", "www.google.com");
httpurlconnection.connect();
String cookie0 = httpurlconnection.getHeaderField("Set-Cookie");
httpurlconnection.disconnect();
cookie0 = "GoogleAccountsLocale_session=zh_CN; " + cookie0.substring(0, cookie0.indexOf(";"));
System.out.println(cookie0.toString());
url = new URL("http://www.google.com/accounts/Login");
String strPost = "continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN&Email=user&Passwd=password&PersistentCookie=yes&rmShown=1&signIn=%E7%99%BB%E5%BD%95";
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setFollowRedirects(true);
httpurlconnection.setInstanceFollowRedirects(true);
httpurlconnection.setDoOutput(true); // 需要向服务器写数据
httpurlconnection.setDoInput(true); //
httpurlconnection.setUseCaches(false); // 获得服务器最新的信息
httpurlconnection.setAllowUserInteraction(false);
httpurlconnection.setRequestMethod("POST");
httpurlconnection
.addRequestProperty(
"Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, *//*");
httpurlconnection
.setRequestProperty("Referer",
"https://www.google.com/accounts/Login?continue=http://www.google.cn/&hl=zh-CN");
httpurlconnection.setRequestProperty("Accept-Language", "zh-cn");
httpurlconnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpurlconnection.setRequestProperty("Accept-Encoding",
"gzip, deflate");
httpurlconnection
.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727; MEGAUPLOAD 1.0)");
httpurlconnection.setRequestProperty("Host", "www.google.com");
httpurlconnection.setRequestProperty("Content-Length", strPost
.length()
+ "");
httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
httpurlconnection.setRequestProperty("Cookie", "GoogleAccountsLocale_session=zh_CN; PREF=ID=14b30dc6bc9025ff:TB=2:NW=1:TM=1211940408:LM=1212373468:DV=AA:S=atzcI-gCcD94m_Nu");
httpurlconnection.setRequestProperty("Action", "https://www.google.com/accounts/LoginAuth?continue=http%3A%2F%2Fwww.google.cn%2F&hl=zh-CN");

httpurlconnection.getOutputStream().write(strPost.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
httpurlconnection.connect();
int code = httpurlconnection.getResponseCode();
System.out.println("code  " + code);

String cookie1 = httpurlconnection.getHeaderField("Set-Cookie");
System.out.println(cookie1.toString());
String local = httpurlconnection.getHeaderField("Location");
System.out.println(local.toString());
httpurlconnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
}

我也尝试了使用HttpURLConnection实现这个post过程,仍然得不到正确的cookies
   
最后更新时间:2008-06-15
up...........
   
0 请登录后投票
最后更新时间:2008-07-21
up>>>>>>
   
0 请登录后投票
论坛首页 入门讨论版

跳转论坛:
JavaEye推荐