Use Java to implement the simsimi Interface

Source: Internet
Author: User

Package com. iask. webchat. chatmachine;

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;

/**
* Little Yellow Chicken Robot
*/
Public class simsimitools {

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

/**
* Little Yellow Chicken Robot
*
* @ Author ghma
*/
Public static 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 ();
// Connection timeout
Conn. setconnecttimeout (25000 );
// Read time-out-server response is slow and time-consuming
Conn. setreadtimeout (25000 );

Httpurlconnection. setfollowredirects (true );
// Request Method
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/20100101 Firefox/21.0 ");
Conn. setrequestproperty ("accept ","*/*");
Conn. setrequestproperty ("Referer ",
Http://www.niurenqushi.com/app/simsimi ");
Conn. Connect ();
// Obtain the output stream corresponding to the urlconnection object
Outputstreamwriter out = new outputstreamwriter (
Conn. getoutputstream ());
// Send Request Parameters
Out. Write ("TXT =" + urlencoder. encode (Params, "UTF-8 "));
Out. Flush ();
Out. Close ();

Inputstream in = conn. getinputstream ();
Bufferedreader READ = new bufferedreader (New inputstreamreader (in,
"UTF-8 "));
String valuestring = NULL;
While (valuestring = read. Readline ())! = NULL ){
Bufferres. append (valuestring );
}
In. Close ();
If (Conn! = NULL ){
// Close the connection
Conn. Disconnect ();
}
} Catch (exception e ){
System. Out. println ("An error occurred while calling the chicken interface! "+ E. getmessage ());
Return xiaodoumachine. getxiaodoumsg (Params );
}

If (bufferres. tostring (). Equals ("why ")){
Return xiaodoumachine. getxiaodoumsg (Params );
}

String finalres = removenews (bufferres. tostring ());
System. Out. println ("xiaohuang chicken robot reply:" + finalres );
Return finalres;
}

/**
* Shield the advertisement in the current interface
*
* @ Return
*/
Public static string removenews (string sendmsgs ){
// Remove Advertisement
If (sendmsgs. indexof ("simsimi2 ")! =-1 ){
Sendmsgs = "I'm a little donkey, female. I have a boyfriend, and I welcome nanhua students to play O (Xiao _ Xiao) O ";
} Else if (sendmsgs. indexof ("Database ")! =-1
| Sendmsgs. indexof ("failed ")! =-1 ){
Int random = (INT) (math. Random () * 5 );
Switch (random ){
Case 1:
Sendmsgs = "hmm ";
Break;
Case 2:
Sendmsgs = "chat with others ";
Break;
Case 3:
Sendmsgs = "hmm ";
Break;
Case 4:
Sendmsgs = "";
Break;
Case 5:
Sendmsgs = "amount ";
Break;
Default:
Sendmsgs = "hmm ";
Break;
}
} Else if (sendmsgs. indexof ("unauthorized access ")! =-1 ){
Sendmsgs = "Why can't I understand what you mean? [cry]. Can we change the topic! ";
} Else if (sendmsgs. indexof ("you can teach me to answer ")! =-1 ){
Sendmsgs = "okay ";
}
// Replace part of the content
Sendmsgs = sendmsgs. replaceall ("silly", "SB ");
Sendmsgs = sendmsgs. replaceall ("John", "Tom ");
// Sendmsgs = sendmsgs. replaceall ("", "");
Sendmsgs = sendmsgs
. Replaceall (
"Little Bean robot web edition address: http://xiao.douqq.com QQ personality network http://www.xiugexing.com ",
"If lunjia doesn't know about government officials ~ ");
Sendmsgs = sendmsgs. replaceall ("xiaodou", "Mao xiaojiao ");
Sendmsgs = sendmsgs. replaceall ("", "lunjia ");
Sendmsgs = sendmsgs. replaceall ("Lin Chen love you QQ personality network http://www.xiugexing.com ",
"If lunjia doesn't know about government officials ~ ");
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.