Package com. iask. webchat. chatmachine;
Import java.net. urlencoder;
Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. httpstatus;
Import org. Apache. http. Client. httpclient;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. entity. stringentity;
Import org. Apache. http. impl. Client. defaulthttpclient;
Import org. Apache. http. Params. coreconnectionpnames;
Import org. Apache. http. util. entityutils;
/**
* Xiaojiu Robot
*
* @ Author Li Zheng
*
*/
Public class xiaojiumachine {
Public static void main (string [] ARGs ){
Try {
System. Out. println (getxiaojiumsg ("Haha "));
} Catch (exception e ){
E. printstacktrace ();
}
}
Public static string getxiaojiumsg (string MSG ){
// Read the result webpage
Stringbuffer sbres = new stringbuffer ();
Httpclient = new defaulthttpclient (); // create an httpclient object
Try {
// Request timeout
Httpclient. getparams (). setparameter (
Coreconnectionpnames. connection_timeout, 20000 );
// Read timeout
Httpclient. getparams (). setparameter (
Coreconnectionpnames. so_timeout, 20000 );
Httppost = new httppost (
Http://www.xiaojo.com/bot/chata.php ");
String parmastr = "chat =" + urlencoder. encode (MSG, "UTF-8 ");
// System. Out. println (parmastr );
Stringentity reqentity = new stringentity (parmastr );
Reqentity. setcontenttype ("application/X-WWW-form-urlencoded ");
Httppost. setheader ("User-Agent ",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; digext )");
Httppost. setheader ("Referer", "http://www.xiaojo.com /");
Httppost. setentity (reqentity );
Httpresponse Responce = httpclient.exe cute (httppost); // get the Responce object
Int resstatu = Responce. getstatusline (). getstatuscode (); // return code
If (resstatu = httpstatus. SC _ OK) {// 200 if the instance is normal, the other instance is incorrect.
// Obtain the corresponding entity
Httpentity entity = Responce. getentity ();
String html = new string (entityutils. tostring (Entity). getbytes (
"ISO-8859-1"), "UTF-8"); // get HTML source code
Sbres. append (html. Trim ());
}
} Catch (exception e ){
System. Out. println ("An error occurred while calling the xiaojiu robot interface! "+ E. getmessage ());
// Call the interface
Return simsimitools. getsimsimicontentbyniuren (MSG );
} Finally {
Httpclient. getconnectionmanager (). Shutdown ();
}
// The returned result is null.
If (sbres. tostring (). Equals ("")
| Sbres. tostring (). indexof ("Description ")! =-1 ){
Return "If lunjia doesn't understand the government ~ Whether to transfer to the issue release page http: // localhost: 8087/iask /";
}
String finalres = removenews (sbres. tostring ());
System. Out. println ("Xiao JIU robot reply:" + finalres );
Return finalres;
}
/**
* Shield the advertisement in the current interface
*
* @ Return
*/
Public static string removenews (string sendmsgs ){
Sendmsgs = sendmsgs. replaceall ("silly", "SB ");
Sendmsgs = sendmsgs. replaceall ("John", "Tom ");
Sendmsgs = sendmsgs. replaceall ("xiaodou", "Mao xiaojiao ");
Return sendmsgs;
}
}