以这个论坛为例:http://www.qiluyiyou.com,我现在想要用Java实现登录后获取返回的html代码,这是我的Java代码:package org.shaw;import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.methods.PostMethod;public class BaiyouBBS
{
static final String LOGON_SITE = "www.qiluyiyou.com"; static final int LOGON_PORT = 80; public Cookie[] login(String name, String pas)
{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
PostMethod post = new PostMethod("http://www.qiluyiyou.com/logging.php?action=login");
NameValuePair loginfield = new NameValuePair("loginfield", "username");
NameValuePair username = new NameValuePair("username", name);
NameValuePair password = new NameValuePair("password", pas);
NameValuePair referer = new NameValuePair("referer", "http://www.qiluyiyou.com/index.php");
NameValuePair questionid = new NameValuePair("questionid", "0");
NameValuePair answer = new NameValuePair("answer", "");
NameValuePair cookietime = new NameValuePair("cookietime", "2592000");
post.setRequestBody(new NameValuePair[] { loginfield, username, password, referer, questionid, answer, cookietime });
try
{
client.executeMethod(post);
}
catch (HttpException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String responseString = null;
try
{
responseString = new String(
post.getResponseBodyAsString().getBytes(
"gbk"));
System.out.println(responseString);
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
return null;
} public static void main(String[] args)
{
new BaiyouBBS().login("zzzxxxzzz", "zzzxxxzzz");
}}结果每次返回的html代码都是登录页面的代码,本人百思不得其解。求高人写一个可以实现上述功能的Demo,本人感激不尽!分不多,只有30,全都用了,求各位高手解答!
import java.io.UnsupportedEncodingException;
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.methods.PostMethod;public class BaiyouBBS
{
static final String LOGON_SITE = "www.qiluyiyou.com"; static final int LOGON_PORT = 80; public Cookie[] login(String name, String pas)
{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
PostMethod post = new PostMethod("http://www.qiluyiyou.com/logging.php?action=login");
NameValuePair loginfield = new NameValuePair("loginfield", "username");
NameValuePair username = new NameValuePair("username", name);
NameValuePair password = new NameValuePair("password", pas);
NameValuePair referer = new NameValuePair("referer", "http://www.qiluyiyou.com/index.php");
NameValuePair questionid = new NameValuePair("questionid", "0");
NameValuePair answer = new NameValuePair("answer", "");
NameValuePair cookietime = new NameValuePair("cookietime", "2592000");
post.setRequestBody(new NameValuePair[] { loginfield, username, password, referer, questionid, answer, cookietime });
try
{
client.executeMethod(post);
}
catch (HttpException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String responseString = null;
try
{
responseString = new String(
post.getResponseBodyAsString().getBytes(
"gbk"));
System.out.println(responseString);
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
post.releaseConnection();
return null;
} public static void main(String[] args)
{
new BaiyouBBS().login("zzzxxxzzz", "zzzxxxzzz");
}}结果每次返回的html代码都是登录页面的代码,本人百思不得其解。求高人写一个可以实现上述功能的Demo,本人感激不尽!分不多,只有30,全都用了,求各位高手解答!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货