JAVA Implementation of the 9th robot 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;

/**
* 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;
}

}

Related Article

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.