(若发在这个版不合适,请告知转到哪里合适,谢谢)
至今我用了几种方法,都不能如愿。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());注释掉,那么就会变成没有登录,不知为什么。
同时该方法会出现n多错误警告信息,关于javascript的,所以我觉得这个程序肯定很有问题
--
修改:slvu FROM 218.106.181.*
FROM 218.106.181.*