Discuz!NT 自动发送短消息 Java部分代码
获取用户名单的,我没发上来。
真有需要的可以加我QQ 15524841
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.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class BBS {
static final String LOGON_SITE = "bbs.***0.com";
static final int LOGON_PORT = 80;
@SuppressWarnings("deprecation")
public Cookie[] login(String name, String pas) {
//登陆模块
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
PostMethod post = new PostMethod("http://bbs.***.com/login.aspx");
NameValuePair username = new NameValuePair("username", name);
NameValuePair password = new NameValuePair("password", pas);
post.setRequestBody(new NameValuePair[] { username, password});
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("gb2312"));
} 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();
// 判断是否登陆成功
if (responseString.lastIndexOf("登录成功, 返回登录前页面") > -1) {
String newUrl = "http://bbs.***0.com/index.aspx";
// 实施转跳
GetMethod get = new GetMethod(newUrl);
get.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;)");
get.setRequestHeader("Cookie", cookies.toString());
try {
client.executeMethod(get);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(get.getResponseBodyAsString().lastIndexOf("退出"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
get.releaseConnection();
//提交短消息
post = new UTF8PostMethod("http://bbs.***0.com/usercppostpm.aspx");
NameValuePair names = new NameValuePair("msgto","接收人ID");
NameValuePair biaoti = new NameValuePair("subject","标题");
NameValuePair neirong = new NameValuePair("message","内容");
NameValuePair sendmsg = new NameValuePair("sendmsg","%E7%AB%8B%E5%8D%B3%E5%8F%91%E9%80%81");
post.setRequestBody(new NameValuePair[] { names, biaoti,neirong,sendmsg});
post.setRequestHeader("Referer", "http://bbs.***0.com/usercppostpm.aspx");
post.setRequestHeader("Accept-Language", "zh-cn");
try {
System.out.println("aaa");
client.getState().addCookies(cookies);
client.executeMethod(post);
responseString = new String(post.getResponseBodyAsString().getBytes("gb2312"));
System.out.println(responseString);
} catch (HttpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return client.getState().getCookies();
}
return null;
}
public static void main(String[] args) {
new BaiyouBBS().login("论坛账号", "论坛密码");
}
}
discuz 论坛消息有一套工具吗?
页:
[1]