⑴ 如何用java实现模拟登录Discuz!论坛并下载返回的html代码
package org.shaw;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
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) {
try {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
PostMethod post = new PostMethod("/logging.php?action=login");
client.executeMethod(post);
String responseString = new String(post.getResponseBody(), "gbk");
post.releaseConnection();
String formhash = getFormHash(responseString);
System.out.println(formhash);
post = new PostMethod("/logging.php?action=login&");
NameValuePair[] params = new NameValuePair[11];
params[0] = new NameValuePair("loginfield", "username");
params[1] = new NameValuePair("username", name);
params[2] = new NameValuePair("password", pas);
params[3] = new NameValuePair("referer", "index.php");
params[4] = new NameValuePair("questionid", "0");
params[5] = new NameValuePair("answer", "");
params[6] = new NameValuePair("cookietime", "2592000");
params[7] = new NameValuePair("formhash", formhash);
params[8] = new NameValuePair("loginmode", "");
params[9] = new NameValuePair("loginsubmit", "true");
params[10] = new NameValuePair("styleid", "");
post.setRequestBody(params);
client.executeMethod(post);
// responseString = new String(post.getResponseBody(), "gbk");
post.releaseConnection();
GetMethod get = new GetMethod("/index.php");
client.executeMethod(get);
responseString = new String(get.getResponseBody(), "gbk");
get.releaseConnection();
System.out.println(responseString);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getFormHash(String htmlContent) {
try {
int start = htmlContent.indexOf("name=\"formhash\"");
start = htmlContent.indexOf("value=\"", start) + 7;
int end = htmlContent.indexOf("\"", start);
String formhash = htmlContent.substring(start, end);
return formhash;
} catch (RuntimeException e) {
throw e;
}
}
public static void main(String[] args) {
new BaiyouBBS().login("zzzxxxzzz", "zzzxxxzzz");
}
}
⑵ org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
首先,第一步,安装好apache和
第三步,配置我们自己的程序,也就是客户端。
1.拷贝开源项目里的jar包https://code.google.com/p/discuz-ucenter-api-for-java/downloads/list
2.配置一个用于与UCenter对接的servlet
复制代码 代码如下:
<servlet>
<servlet-name>connect_discuz</servlet-name>
<servlet-class>com.fivestars.interfaces.bbs.api.UC</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>connect_discuz</servlet-name>
<url-pattern>/api/uc.php</url-pattern>
</servlet-mapping>
3.把配置文件(config.properties)复制到src目录下
复制代码 代码如下:
#
# ================================================
# * Discuz! Ucenter API for JAVA
# ================================================
# UC comunication settings
#
#
#uc server url如果是全新安装的模式,ucenter可以随discuz一起安装,路径是
UC_API = http://{discuz根目录}/uc_server
#uc ip address
UC_IP = 127.0.0.1
#key
UC_KEY = yunstudio
#appid
UC_APPID = 2
#connect mode: default value is ""
UC_CONNECT =
4.把开源项目中的demo(jsp_demo.jsp)复制到应用的根目录中。原来作者的jsp文件编码和文件头都有点小问题,所以我稍微的修改了一下。
复制代码 代码如下:
<%
/**
* ================================================
* Discuz! Ucenter API for JAVA
* ================================================
* JSP 调用示例
*
* 更多信息:http://code.google.com/p/discuz-ucenter-api-for-java/
*/
%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.fivestars.interfaces.bbs.util.XMLHelper"%>
<%@ page import="com.fivestars.interfaces.bbs.client.Client"%>
<%
Client uc = new Client();
String result = uc.uc_user_login("admin", "yun");
LinkedList<String> rs = XMLHelper.uc_unserialize(result);
if(rs.size()>0){
int $uid = Integer.parseInt(rs.get(0));
String $username = rs.get(1);
String $password = rs.get(2);
String $email = rs.get(3);
if($uid > 0) {
response.addHeader("P3P"," CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"");
out.println("登录成功");
out.println($username);
out.println($password);
out.println($email);
String $ucsynlogin = uc.uc_user_synlogin($uid);
out.println("登录成功"+$ucsynlogin);
//本地登陆代码
//TODO ... ....
Cookie auth = new Cookie("auth", uc.uc_authcode($password+" "+$uid, "ENCODE"));
auth.setMaxAge(31536000);
//auth.setDomain("localhost");
response.addCookie(auth);
Cookie user = new Cookie("uchome_loginuser", $username);
response.addCookie(user);
} else if($uid == -1) {
out.println("用户不存在,或者被删除");
} else if($uid == -2) {
out.println("密码错");
} else {
out.println("未定义");
}
}else{
out.println("Login failed");
System.out.println(result);
}
%>
⑶ 请求有没有什么java开源的论坛,像php的discuz这种。不是什么java论坛网站哈,之前问别人给我说csdn之类的
java软件开源社区
http://www.oschina.net/project/lang/19/java
Java开源大全
www.open-open.com.com