JAVA Implementation of bean chatbot Interface

Source: Internet
Author: User

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;

/**
* Little Bean Robot
*/
Public class xiaodoumachine {

Public static void main (string [] ARGs ){
Try {
System. Out. println (getxiaodoumsg ("Haha "));
} Catch (exception e ){
E. printstacktrace ();
}
}

Public static string getxiaodoumsg (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://xiao.douqq.com/bot/chat.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://xiao.douqq.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 mini bean robot interface! "+ E. getmessage ());
// Call the 9th API
Return xiaojiumachine. getxiaojiumsg (MSG );
} Finally {
Httpclient. getconnectionmanager (). Shutdown ();
}

// Call the 9th API
If (sbres. tostring (). Equals ("") | sbres. tostring (). Equals ("why ")){
Return xiaojiumachine. getxiaojiumsg (MSG );
}

String finalres = removenews (sbres. tostring ());
System. Out. println ("reply to the little bean ROBOT:" + finalres );
Return finalres;
}

/**
* Shield the advertisement in the current interface
*
* @ Return
*/
Public static string removenews (string sendmsgs ){
If (sendmsgs. indexof ("xiaodouqqcom ")! =-1 ){
Sendmsgs = "If lunjia doesn't know about government officials ~ ";
} Else if (sendmsgs. indexof ("simsimi2 ")! =-1 ){
Sendmsgs = "If lunjia doesn't know about government officials ~ ";
} Else if (sendmsgs. indexof ("douqq ")! =-1 ){
Sendmsgs = "If lunjia doesn't know about government officials ~ ";
}
Sendmsgs = sendmsgs. replaceall ("silly", "SB"). replaceall ("Xiao JIU", "Mao Xiao ")
. Replaceall ("", ""). replaceall ("", "")
. Replaceall ("Xiao Qian", "Mao xiaojiao ");
Return sendmsgs;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.