求助:java模拟登录新浪微博(httpclient,webdirver,httpunit)
至今我用了几种方法,都不能如愿。如httpclient,webdirver,httpunit等。前两个都是登录不进去,取不到cookie。而httpunit倒是登录进去了,但是保存不了session,即再访问其他网页的时候,提示未登录。
以下贴几个代码,请达人指教。
httpclient方法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
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;
/**
* @author ibm
*
*/
public class LoginWeiBo {
/**
* @param args
*/
static final String LOGON_SITE = "login.sina.com.cn";
static final int LOGON_PORT = 80;
static final String loginurl = "/sso/login.php?client=ssologin.js(v1.3.9)";
static final String loginparematername = "loginname";
static final String loginparematerpass = "password";
static final String username = "****";
static final String password = "*****";
static final String getUrl = "/member/my.php?entry=sso";
public static void main(String[] args) throws Exception {
HttpClient client = imitateLogin(LOGON_SITE, LOGON_PORT, loginurl, loginparematername, loginparematerpass, username, password);
// 访问所需的页面
imitateGetUrl(client, getUrl);
}
//模拟等录
private static HttpClient imitateLogin(String LOGON_SITE, int LOGON_PORT,
String loginurl,String loginparematername,String loginparematerpass,String username,String password) throws IOException, HttpException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
// 模拟登录页面
PostMethod post = new PostMethod(loginurl);
NameValuePair name = new NameValuePair(loginparematername,username );
NameValuePair pass = new NameValuePair(loginparematerpass,password );
post.setRequestBody(new NameValuePair[] { name, pass });
int status = client.executeMethod(post);
System.out.println(status);
post.releaseConnection();
// 查看cookie信息
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false,
client.getState().getCookies());
if (cookies != null)
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
}
}
return client;
}
//模拟等录 后获取所需要的页面
private static void imitateGetUrl(HttpClient client, String getUrl)
throws IOException, HttpException {
GetMethod post2 = new GetMethod(getUrl);
client.executeMethod(post2);
System.out.println(post2.getURI());
/*InputStream is = post2.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"GBK"));
String s = br.readLine();
while(s != null ){
System.out.println(s);
s = br.readLine();
}*/
//System.out.println(post2.getResponseBodyAsString());
post2.releaseConnection();
}
}
后台打印
200
None
http://login.sina.com.cn/?r=%2Fmember%2Fmy.php%3Fentry%3Dsso
==================================================================
webdriver方法
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
/**
* @author ibm
*
*/
public class LoginTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HtmlUnitDriver driver = new HtmlUnitDriver();
//driver.setJavascriptEnabled(true);
driver.get("http://t.sina.com.cn");
WebElement ln = driver.findElement(By.id("loginname"));
System.out.println(ln.toString());
ln.sendKeys("******");
WebElement pas = driver.findElement(By.id("password"));
System.out.println(pas.toString());
pas.sendKeys("*********");
WebElement submit = driver.findElement(By.id("login_submit_btn"));
System.out.println(submit.toString());
submit.click();
//submit.submit();http://tdrd.org/p/disparticle.php?boardName=RUCexpress&ID=137290
System.out.println(driver.getPageSource());
// driver.get("http://t.sina.com.cn/1849038335");
// System.out.println(driver.getPageSource());
}
}
查看cosole信息,可以未登录成功
===============================================
httpunit方法
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class HtmlUnitDemo {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
WebClient client = new WebClient();
HtmlPage page = client.getPage("http://t.sina.com.cn/");
System.out.println(page.asText());
HtmlTextInput ln = (HtmlTextInput) page.getElementById("loginname");
HtmlPasswordInput pwd = (HtmlPasswordInput) page.getElementById("password");
ln.setText("******");
pwd.setText("**********");
HtmlAnchor btn = (HtmlAnchor) page.getElementById("login_submit_btn");
HtmlPage page2 = btn.click();
System.out.println("\n\n\n\n\n");
System.out.println(page2.asText());
//HtmlPage page3 = client.getPage("http://t.sina.com.cn/1849038335");
//System.out.println(page3.asText());
client.closeAllWindows();
}
}
已经登录,但是若吧第一个打印:System.out.println(page.asText());注释掉,那么就会变成没有登录,不知为什么。 --------------------编程问答-------------------- http://blog.csdn.net/yodlove/archive/2010/10/13/5938022.aspx 这是登录人人网的 --------------------编程问答-------------------- 回复yodlove
其实这个帖子我看过,我也尝试做了其他一些简单的论坛登录,如我们学校的bbs,是可以成功的。
但是不知为何碰到新浪微博,就是成功不了,郁闷。
谢谢你的解答 --------------------编程问答--------------------
回复yodlove
其实这个帖子我看过,我也尝试做了其他一些简单的论坛登录,如我们学校的bbs,是可以成功的。
但是不知为何碰到新浪微博,就是成功不了,郁闷。
谢谢你的解答 --------------------编程问答--------------------
我吧我的代码完成贴上了,你若有时间,帮我看看吧,非常感谢 --------------------编程问答-------------------- --------------------编程问答-------------------- 最后解决了吗兄弟? --------------------编程问答-------------------- 额,我试着用你httpunit的方法登录了,失败了。。。 --------------------编程问答-------------------- 最后解决了没,我现在也遇到这个问题了 --------------------编程问答-------------------- 我用httpclient登录成功了 --------------------编程问答-------------------- HttpURLConnection完全可以满足需要,就是要看你如何获得Cookie了,以及其他附带条件。
一般情况下是无法直接获得Cookie的,可以同过JNI调用WIN API获取Cookie,然后在request的时候添加上去。
// Cookie.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
void getCookie()
{
LPSTR url = "http://www.xxxxx.com";
TCHAR buffer [16384];
DWORD bufferSize = sizeof(buffer)/sizeof(buffer[0]);
BOOL bRes = InternetGetCookie(url, "", buffer, &bufferSize);
printf(buffer);
printf("\n");
}
int main(int argc, char* argv[])
{
getCookie();
return 0;
}
将以上代码封装为dll供java调用。
可以利用firebug监控每次请求的的参数,Cookie,SESSIONID等信息。
只要是Http请求就能模仿。
lz的分太少了啊。 --------------------编程问答-------------------- 关注。 --------------------编程问答--------------------
9楼的朋友,你是如何做的到?能否看下你的代码?谢谢 --------------------编程问答-------------------- 继续关注。 --------------------编程问答-------------------- 不是有新浪微博的sdk吗?还搞这么土的方法干什么,最后还要自己处理一堆垃圾数据,模拟js。 --------------------编程问答-------------------- 提供2条线索:MetaSeeker,Heritrix --------------------编程问答-------------------- 你的做好没有,我现在在做这个,做好的话给点意见啊!
补充:Java , Web 开发