本文一共使用了两个机器人:小黄鸡,小逗比机器人,二者相互调用避免程序出现异常。已经本人调试,完全能用!
第一个(小黄鸡机器人):
package com.web;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class SimsimiTools {
publicstatic void main(String[] args) {
try {
System.out.println(getSimsimiContentByNiuren("你是谁"));
} catch (Exception e) {
e.printStackTrace();
}
}
publicstatic String getSimsimiContentByNiuren(String params) {
StringBuffer bufferRes = new StringBuffer();
try {
URL realUrl = new URL(
"http://www.niurenqushi.com/app/simsimi/ajax.aspx");
HttpURLConnection conn = (HttpURLConnection) realUrl
.openConnection();
// 连接超时
conn.setConnectTimeout(25000);
// 读取超时 --服务器响应比较慢,增大时间
conn.setReadTimeout(25000);
HttpURLConnection.setFollowRedirects(true);
// 请求方式
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101Firefox/21.0");
conn.setRequestProperty("Accept", "*
publicstatic String removeNews(String sendMsgs) {
// 去除广告
if (sendMsgs.indexOf("sim") != -1) {
sendMsgs ="偶是小黄鸡,女,还木有男友,欢迎亲们调戏 O(∩_∩)O";
} else if (sendMsgs.indexOf("Database") != -1
|| sendMsgs.indexOf("Failed") != -1) {
int random = (int) (Math.random() * 5);
switch (random) {
case 1:
sendMsgs = "嗯";
break;
case 2:
sendMsgs = "聊天其它的吧";